001/*
002 * Copyright (C) 2019 Michael N. Lipp (http://www.mnl.de)
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package de.mnl.osgi.lf4osgi.core;
018
019import java.util.Map;
020import java.util.Optional;
021import java.util.concurrent.ConcurrentHashMap;
022import java.util.function.BiFunction;
023
024import org.osgi.framework.Bundle;
025
026/**
027 * A simple cache for loggers used by a bundle.
028 * 
029 * @param <T> the type of the loggers in the group
030 */
031public class DefaultLoggerGroup<T> implements LoggerGroup {
032
033    private final Bundle bundle;
034
035    private final Map<String, T> loggers;
036    private final BiFunction<LoggerGroup, String, T> loggerSupplier;
037
038    /**
039     * Instantiates a new bundle group.
040     *
041     * @param bundle the bundle
042     * @param loggerSupplier the default logger supplier used
043     *     by {@link #computeIfAbsent(String)}
044     */
045    public DefaultLoggerGroup(Bundle bundle,
046            BiFunction<LoggerGroup, String, T> loggerSupplier) {
047        this.bundle = bundle;
048        this.loggerSupplier = loggerSupplier;
049        loggers = new ConcurrentHashMap<>();
050    }
051
052    /**
053     * Returns the bundle that this group is associated with.
054     * 
055     * @return the bundle
056     */
057    public final Bundle getBundle() {
058        return bundle;
059    }
060
061    /**
062     * Checks if the logger with the provided name already exists.
063     *
064     * @param name the name
065     * @return true, if successful
066     */
067    public boolean hasLogger(String name) {
068        return loggers.containsKey(name);
069    }
070
071    /**
072     * Gets the logger if it exists.
073     *
074     * @param name the name
075     * @return the logger
076     */
077    public Optional<T> logger(String name) {
078        return Optional.ofNullable(loggers.get(name));
079    }
080
081    /**
082     * Gets the logger with the specified name. If it is not already in
083     * this group, create it using the provided supplier.
084     *
085     * @param name the name
086     * @param supplier the supplier
087     * @return the logger
088     */
089    public T computeIfAbsent(String name,
090            BiFunction<LoggerGroup, String, T> supplier) {
091        return loggers.computeIfAbsent(name, n -> supplier.apply(this, n));
092    }
093
094    /**
095     * Gets the logger with the specified name.If it is not already in
096     * this group, create it using the default supplier.
097     *
098     * @param name the name
099     * @return the logger
100     */
101    public T computeIfAbsent(String name) {
102        return loggers.computeIfAbsent(name,
103            n -> loggerSupplier.apply(this, n));
104    }
105
106    /**
107     * Put the logger in the group if it isn't already known.
108     *
109     * @param name the name
110     * @param logger the logger
111     */
112    public void putIfAbsent(String name, T logger) {
113        loggers.computeIfAbsent(name, k -> logger);
114    }
115}