001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.util;
018
019/**
020 * A cache that uses a near optional LRU Cache using {@link java.lang.ref.WeakReference}.
021 * <p/>
022 * The Cache is implemented by Caffeine which provides an <a href="https://github.com/ben-manes/caffeine/wiki/Efficiency">efficient cache</a>.
023 * <p/>
024 * This implementation uses {@link java.lang.ref.WeakReference} for stored values in the cache, to support the JVM
025 * when it wants to reclaim objects for example during garbage collection. Therefore this implementation does
026 * not support <b>all</b> the {@link java.util.Map} methods.
027 * <p/>
028 * The following methods is <b>only</b> be be used:
029 * <ul>
030 *   <li>containsKey - To determine if the key is in the cache and refers to a value</li>
031 *   <li>entrySet - To return a set of all the entries (as key/value paris)</li>
032 *   <li>get - To get a value from the cache</li>
033 *   <li>isEmpty - To determine if the cache contains any values</li>
034 *   <li>keySet - To return a set of the current keys which refers to a value</li>
035 *   <li>put - To add a value to the cache</li>
036 *   <li>putAll - To add values to the cache</li>
037 *   <li>remove - To remove a value from the cache by its key</li>
038 *   <li>size - To get the current size</li>
039 *   <li>values - To return a copy of all the value in a list</li>
040 * </ul>
041 * <p/>
042 * The {@link #containsValue(Object)} method should <b>not</b> be used as it's not adjusted to check
043 * for the existence of a value without catering for the soft references.
044 * <p/>
045 * Notice that if the JVM reclaim memory the content of this cache may be garbage collected, without any
046 * eviction notifications.
047 * <p/>
048 * Use {@link LRUCacheFactory} to create a new instance (do not use the constructor).
049 *
050 * @see LRUCacheFactory
051 * @see LRUCache
052 * @see LRUSoftCache
053 */
054public class LRUWeakCache<K, V> extends LRUCache<K, V> {
055
056    public LRUWeakCache(int maximumCacheSize) {
057        this(16, maximumCacheSize);
058    }
059
060    public LRUWeakCache(int initialCapacity, int maximumCacheSize) {
061        super(initialCapacity, maximumCacheSize, false, false, true, false);
062    }
063
064    @Override
065    public String toString() {
066        return "LRUWeakCache@" + ObjectHelper.getIdentityHashCode(this);
067    }
068}