org.apache.camel.util.LRUWeakCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of camel-core Show documentation
Show all versions of camel-core Show documentation
The Core Camel Java DSL based router
/**
* 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.util;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
/**
* A Least Recently Used Cache which uses {@link java.lang.ref.WeakReference}.
*
* This implementation uses {@link java.lang.ref.WeakReference} for stored values in the cache, to support the JVM
* when it wants to reclaim objects for example during garbage collection. Therefore this implementation does
* not support all the {@link java.util.Map} methods.
*
* The following methods is only be be used:
*
* - containsKey - To determine if the key is in the cache and refers to a value
* - entrySet - To return a set of all the entries (as key/value paris)
* - get - To get a value from the cache
* - isEmpty - To determine if the cache contains any values
* - keySet - To return a set of the current keys which refers to a value
* - put - To add a value to the cache
* - putAll - To add values to the cache
* - remove - To remove a value from the cache by its key
* - size - To get the current size
* - values - To return a copy of all the value in a list
*
*
* The {@link #containsValue(Object)} method should not be used as it's not adjusted to check
* for the existence of a value without catering for the soft references.
*
* Notice that if the JVM reclaim memory the content of this cache may be garbage collected, without any
* eviction notifications.
*
* @see LRUCache
* @see LRUSoftCache
*/
public class LRUWeakCache extends LRUCache {
private static final long serialVersionUID = 1L;
public LRUWeakCache(int maximumCacheSize) {
super(maximumCacheSize);
}
public LRUWeakCache(int initialCapacity, int maximumCacheSize) {
super(initialCapacity, maximumCacheSize);
}
@Override
@SuppressWarnings("unchecked")
public V put(K key, V value) {
WeakReference put = new WeakReference(value);
WeakReference prev = (WeakReference) super.put(key, (V) put);
return prev != null ? prev.get() : null;
}
@Override
@SuppressWarnings("unchecked")
public V get(Object o) {
WeakReference ref = (WeakReference) super.get(o);
return ref != null ? ref.get() : null;
}
@Override
public void putAll(Map map) {
for (Entry entry : map.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
@Override
@SuppressWarnings("unchecked")
public V remove(Object o) {
WeakReference ref = (WeakReference) super.remove(o);
return ref != null ? ref.get() : null;
}
@Override
@SuppressWarnings("unchecked")
public Collection values() {
// return a copy of all the active values
Collection> col = (Collection>) super.values();
Collection answer = new ArrayList();
for (WeakReference ref : col) {
V value = ref.get();
if (value != null) {
answer.add(value);
}
}
return answer;
}
@Override
public int size() {
// only count as a size if there is a value
int size = 0;
for (V value : super.values()) {
WeakReference ref = (WeakReference) value;
if (ref != null && ref.get() != null) {
size++;
}
}
return size;
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public boolean containsKey(Object o) {
// must lookup if the key has a value, as we only regard a key to be contained
// if the value is still there (the JVM can remove the soft reference if it need memory)
return get(o) != null;
}
@Override
public Set> entrySet() {
Set> original = super.entrySet();
// must use a copy to avoid concurrent modifications and be able to get/set value using
// the soft reference so the returned set is without the soft reference, and thus is
// use able for the caller to use
Set> answer = new LinkedHashSet>(original.size());
for (final Entry entry : original) {
Entry view = new Entry() {
@Override
public K getKey() {
return entry.getKey();
}
@Override
@SuppressWarnings("unchecked")
public V getValue() {
WeakReference ref = (WeakReference) entry.getValue();
return ref != null ? ref.get() : null;
}
@Override
@SuppressWarnings("unchecked")
public V setValue(V v) {
V put = (V) new WeakReference(v);
WeakReference prev = (WeakReference) entry.setValue(put);
return prev != null ? prev.get() : null;
}
};
answer.add(view);
}
return answer;
}
@Override
public String toString() {
return "LRUWeakCache@" + ObjectHelper.getIdentityHashCode(this);
}
}