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

com.mindoo.domino.jna.internal.SizeLimitedLRUCache Maven / Gradle / Ivy

There is a newer version: 0.9.53
Show newest version
package com.mindoo.domino.jna.internal;

import java.util.ArrayList;
import java.util.List;

import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
import com.googlecode.concurrentlinkedhashmap.EntryWeigher;

/**
 * Abstract cache class that implements an LRU algorithm. Uses RW lock
 * internally to support concurrent access across threads.
 * 
 * @author Karsten Lehmann
 *
 * @param  key type
 * @param  value type
 */
public abstract class SizeLimitedLRUCache {
	private ConcurrentLinkedHashMap m_cache;
	
	public SizeLimitedLRUCache(int maxSizeUnits) {
		EntryWeigher customWeigher = new EntryWeigher() {
			@Override public int weightOf(K key, V value) {
				return computeSize(key, value);
			}
		};
		m_cache = new ConcurrentLinkedHashMap.Builder()
			    .maximumWeightedCapacity(maxSizeUnits)
			    .weigher(customWeigher)
			    .build();
	}
	
	public List getKeys() {
		return new ArrayList(m_cache.keySet());
	}
	
	public void clear() {
		m_cache.clear();
	}
	
	public final long getCurrentCacheSizeInUnits() {
		return m_cache.weightedSize();
	}

	/**
	 * Implement this method to compute a size for the cache entry
	 * 
	 * @param key key
	 * @param value value
	 * @return size in units
	 */
	protected abstract int computeSize(K key, V value);
	
	/**
	 * Method to look up a cache entry
	 * 
	 * @param key key
	 * @return value or null if not found
	 */
	public V get(K key) {
		return m_cache.get(key);
	}
	
	/**
	 * Method to check whether the cache contains a key
	 * 
	 * @param key key
	 * @return true if value exists
	 */
	public boolean containsKey(K key) {
		return m_cache.containsKey(key);
	}
	
	/**
	 * Removes a key from the LRU cache
	 * 
	 * @param key key
	 * @return previously stored value or null
	 */
	public V remove(K key) {
		return m_cache.remove(key);
	}
	
	/**
	 * Adds an entry to the LRU cache
	 * 
	 * @param key key
	 * @param newValue value, if null we remove the cache entry
	 * @return previously stored value or null
	 */
	public V put(K key, V newValue) {
		if (newValue==null) {
			return remove(key);
		}
		else {
			return m_cache.put(key, newValue);
		}
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy