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;
023import org.osgi.framework.Bundle;
024
025/**
026 * A simple cache for loggers used by a bundle.
027 * 
028 * @param <T> the type of the loggers in the group
029 */
030public class DefaultLoggerGroup<T> implements LoggerGroup {
031
032    private final Bundle bundle;
033
034    private final Map<String, T> loggers;
035    private final BiFunction<LoggerGroup, String, T> loggerSupplier;
036
037    /**
038     * Instantiates a new bundle group.
039     *
040     * @param bundle the bundle
041     * @param loggerSupplier the default logger supplier used
042     *     by {@link #computeIfAbsent(String)}
043     */
044    public DefaultLoggerGroup(Bundle bundle,
045            BiFunction<LoggerGroup, String, T> loggerSupplier) {
046        this.bundle = bundle;
047        this.loggerSupplier = loggerSupplier;
048        loggers = new ConcurrentHashMap<>();
049    }
050
051    /**
052     * Returns the bundle that this group is associated with.
053     * 
054     * @return the bundle
055     */
056    public final Bundle getBundle() {
057        return bundle;
058    }
059
060    /**
061     * Checks if the logger with the provided name already exists.
062     *
063     * @param name the name
064     * @return true, if successful
065     */
066    public boolean hasLogger(String name) {
067        return loggers.containsKey(name);
068    }
069
070    /**
071     * Gets the logger if it exists.
072     *
073     * @param name the name
074     * @return the logger
075     */
076    public Optional<T> logger(String name) {
077        return Optional.ofNullable(loggers.get(name));
078    }
079
080    /**
081     * Gets the logger with the specified name. If it is not already in
082     * this group, create it using the provided supplier.
083     *
084     * @param name the name
085     * @param supplier the supplier
086     * @return the logger
087     */
088    public T computeIfAbsent(String name,
089            BiFunction<LoggerGroup, String, T> supplier) {
090        return loggers.computeIfAbsent(name, n -> supplier.apply(this, n));
091    }
092
093    /**
094     * Gets the logger with the specified name.If it is not already in
095     * this group, create it using the default supplier.
096     *
097     * @param name the name
098     * @return the logger
099     */
100    public T computeIfAbsent(String name) {
101        return loggers.computeIfAbsent(name,
102            n -> loggerSupplier.apply(this, n));
103    }
104
105    /**
106     * Put the logger in the group if it isn't already known.
107     *
108     * @param name the name
109     * @param logger the logger
110     */
111    public void putIfAbsent(String name, T logger) {
112        loggers.computeIfAbsent(name, k -> logger);
113    }
114}