All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.camel.support.LRUCacheFactory Maven / Gradle / Ivy

There is a newer version: 4.6.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.support;

import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import java.util.function.Consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Factory to create {@link LRUCache} instances.
 */
public abstract class LRUCacheFactory {

    private static final Logger LOGGER = LoggerFactory.getLogger(LRUCacheFactory.class);

    private static volatile LRUCacheFactory instance;
    
    public static LRUCacheFactory getInstance() {
        if (instance == null) {
            synchronized (LRUCacheFactory.class) {
                if (instance == null) {
                    instance = createLRUCacheFactory();
                }
            }
        }
        return instance;
    }

    private static LRUCacheFactory createLRUCacheFactory() {
        try {
            ClassLoader classLoader = LRUCacheFactory.class.getClassLoader();
            URL url = classLoader.getResource("META-INF/services/org/apache/camel/lru-cache-factory");
            if (url != null) {
                Properties props = new Properties();
                try (InputStream is = url.openStream()) {
                    props.load(is);
                }
                String clazzName = props.getProperty("class");
                Class clazz = classLoader.loadClass(clazzName);
                Object factory = clazz.getDeclaredConstructor().newInstance();
                return (LRUCacheFactory) factory;
            }
        } catch (Throwable t) {
            LOGGER.warn("Error creating LRUCacheFactory", t);
        }
        return new DefaultLRUCacheFactory();
    }
    
    /**
     * Constructs an empty LRUCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param maximumCacheSize the max capacity.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public static  Map newLRUCache(int maximumCacheSize) {
        return getInstance().createLRUCache(maximumCacheSize);
    }

    /**
     * Constructs an empty LRUCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param maximumCacheSize the max capacity.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public static  Map newLRUCache(int maximumCacheSize, Consumer onEvict) {
        return getInstance().createLRUCache(maximumCacheSize, onEvict);
    }

    /**
     * Constructs an empty LRUCache instance with the
     * specified initial capacity, maximumCacheSize, and will stop on eviction.
     *
     * @param initialCapacity  the initial capacity.
     * @param maximumCacheSize the max capacity.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public static  Map newLRUCache(int initialCapacity, int maximumCacheSize) {
        return getInstance().createLRUCache(initialCapacity, maximumCacheSize);
    }

    /**
     * Constructs an empty LRUCache instance with the
     * specified initial capacity, maximumCacheSize,load factor and ordering mode.
     *
     * @param initialCapacity  the initial capacity.
     * @param maximumCacheSize the max capacity.
     * @param stopOnEviction   whether to stop service on eviction.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public static  Map newLRUCache(int initialCapacity, int maximumCacheSize, boolean stopOnEviction) {
        return getInstance().createLRUCache(initialCapacity, maximumCacheSize, stopOnEviction);
    }

    /**
     * Constructs an empty LRUSoftCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param maximumCacheSize the max capacity.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public static  Map newLRUSoftCache(int maximumCacheSize) {
        return getInstance().createLRUSoftCache(maximumCacheSize);
    }

    /**
     * Constructs an empty LRUSoftCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param initialCapacity  the initial capacity.
     * @param maximumCacheSize the max capacity.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public static  Map newLRUSoftCache(int initialCapacity, int maximumCacheSize) {
        return getInstance().createLRUSoftCache(initialCapacity, maximumCacheSize);
    }

    /**
     * Constructs an empty LRUSoftCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param initialCapacity  the initial capacity.
     * @param maximumCacheSize the max capacity.
     * @param stopOnEviction   whether to stop service on eviction.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public static  Map newLRUSoftCache(int initialCapacity, int maximumCacheSize, boolean stopOnEviction) {
        return getInstance().createLRUSoftCache(initialCapacity, maximumCacheSize, stopOnEviction);
    }

    /**
     * Constructs an empty LRUWeakCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param maximumCacheSize the max capacity.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public static  Map newLRUWeakCache(int maximumCacheSize) {
        return getInstance().createLRUWeakCache(maximumCacheSize);
    }

    /**
     * Constructs an empty LRUWeakCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param initialCapacity  the initial capacity.
     * @param maximumCacheSize the max capacity.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public static  Map newLRUWeakCache(int initialCapacity, int maximumCacheSize) {
        return getInstance().createLRUWeakCache(initialCapacity, maximumCacheSize);
    }

    /**
     * Constructs an empty LRUWeakCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param initialCapacity  the initial capacity.
     * @param maximumCacheSize the max capacity.
     * @param stopOnEviction   whether to stop service on eviction.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public static  Map newLRUWeakCache(int initialCapacity, int maximumCacheSize, boolean stopOnEviction) {
        return getInstance().createLRUWeakCache(initialCapacity, maximumCacheSize, stopOnEviction);
    }

    /**
     * Constructs an empty LRUCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param maximumCacheSize the max capacity.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public abstract  Map createLRUCache(int maximumCacheSize);

    /**
     * Constructs an empty LRUCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param maximumCacheSize the max capacity.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public abstract  Map createLRUCache(int maximumCacheSize, Consumer onEvict);

    /**
     * Constructs an empty LRUCache instance with the
     * specified initial capacity, maximumCacheSize, and will stop on eviction.
     *
     * @param initialCapacity  the initial capacity.
     * @param maximumCacheSize the max capacity.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public abstract  Map createLRUCache(int initialCapacity, int maximumCacheSize);

    /**
     * Constructs an empty LRUCache instance with the
     * specified initial capacity, maximumCacheSize,load factor and ordering mode.
     *
     * @param initialCapacity  the initial capacity.
     * @param maximumCacheSize the max capacity.
     * @param stopOnEviction   whether to stop service on eviction.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public abstract  Map createLRUCache(int initialCapacity, int maximumCacheSize, boolean stopOnEviction);

    /**
     * Constructs an empty LRUSoftCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param maximumCacheSize the max capacity.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public abstract  Map createLRUSoftCache(int maximumCacheSize);

    /**
     * Constructs an empty LRUSoftCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param initialCapacity  the initial capacity.
     * @param maximumCacheSize the max capacity.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public abstract  Map createLRUSoftCache(int initialCapacity, int maximumCacheSize);

    /**
     * Constructs an empty LRUSoftCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param initialCapacity  the initial capacity.
     * @param maximumCacheSize the max capacity.
     * @param stopOnEviction   whether to stop service on eviction.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public abstract  Map createLRUSoftCache(int initialCapacity, int maximumCacheSize, boolean stopOnEviction);

    /**
     * Constructs an empty LRUWeakCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param maximumCacheSize the max capacity.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public abstract  Map createLRUWeakCache(int maximumCacheSize);

    /**
     * Constructs an empty LRUWeakCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param initialCapacity  the initial capacity.
     * @param maximumCacheSize the max capacity.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public abstract  Map createLRUWeakCache(int initialCapacity, int maximumCacheSize);

    /**
     * Constructs an empty LRUWeakCache instance with the
     * specified maximumCacheSize, and will stop on eviction.
     *
     * @param initialCapacity  the initial capacity.
     * @param maximumCacheSize the max capacity.
     * @param stopOnEviction   whether to stop service on eviction.
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    public abstract  Map createLRUWeakCache(int initialCapacity, int maximumCacheSize, boolean stopOnEviction);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy