001/*
002 * LinkedCache.java July 2012
003 *
004 * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net>
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
015 * implied. See the License for the specific language governing 
016 * permissions and limitations under the License.
017 */
018
019package org.simpleframework.xml.util;
020
021import java.util.LinkedHashMap;
022import java.util.Map.Entry;
023
024/**
025 * The <code>LimitedCache</code> interface is used to represent a 
026 * cache that will store key value pairs. This implementation is
027 * backed by a <code>LinkedHashMap</code> so that only a specific
028 * number of elements can be stored in the cache at one time.
029 * 
030 * @author Niall Gallagher
031 */   
032public class LimitedCache<T> extends LinkedHashMap<Object, T> implements Cache<T> {
033   
034   /**
035    * This represents the capacity of this cache instance.
036    */
037   private final int capacity;
038   
039   /**
040    * Constructor of the <code>LimitedCache</code> object. This is
041    * used to create a cache with a fixed size. The strategy for
042    * this cache is least recently used. Any insert or fetch from
043    * the cache is considered to be a use.
044    */
045   public LimitedCache() {
046      this(50000);
047   }
048   
049   /**
050    * Constructor of the <code>LimitedCache</code> object. This is
051    * used to create a cache with a fixed size. The strategy for
052    * this cache is least recently used. Any insert or fetch from
053    * the cache is considered to be a use.
054    *
055    * @param capacity this is the capacity of the cache object
056    */
057   public LimitedCache(int capacity) {
058      this.capacity = capacity;
059   }
060
061   /**
062    * This method is used to insert a key value mapping in to the
063    * cache. The value can later be retrieved or removed from the
064    * cache if desired. If the value associated with the key is 
065    * null then nothing is stored within the cache.
066    * 
067    * @param key this is the key to cache the provided value to
068    * @param value this is the value that is to be cached
069    */
070   public void cache(Object key, T value) {
071      put(key, value);
072   }
073
074   /**
075    * This is used to exclusively take the value mapped to the 
076    * specified key from the cache. Invoking this is effectively
077    * removing the value from the cache.
078    * 
079    * @param key this is the key to acquire the cache value with
080    * 
081    * @return this returns the value mapped to the specified key 
082    */
083   public T take(Object key) {
084      return remove(key);
085   }
086
087   /**
088    * This method is used to get the value from the cache that is
089    * mapped to the specified key. If there is no value mapped to
090    * the specified key then this method will return a null.
091    * 
092    * @param key this is the key to acquire the cache value with
093    * 
094    * @return this returns the value mapped to the specified key 
095    */
096   public T fetch(Object key) {
097      return get(key);
098   }
099
100   /**
101    * This is used to determine whether the specified key exists
102    * with in the cache. Typically this can be done using the 
103    * fetch method, which will acquire the object. 
104    * 
105    * @param key this is the key to check within this segment
106    * 
107    * @return true if the specified key is within the cache
108    */
109   public boolean contains(Object key) {
110      return containsKey(key);
111   }
112   
113   /**
114    * This is used to remove the eldest entry from the cache.
115    * The eldest entry is removed from the cache if the size of
116    * the map grows larger than the maximum entries permitted.
117    *
118    * @param entry this is the eldest entry that can be removed
119    *
120    * @return this returns true if the entry should be removed
121    */ 
122   protected boolean removeEldestEntry(Entry<Object, T> entry) {
123      return size() > capacity;
124   }
125}