Coverage Report - org.simpleframework.xml.util.LimitedCache
 
Classes in this File Line Coverage Branch Coverage Complexity
LimitedCache
81%
9/11
50%
1/2
1
 
 1  
 /*
 2  
  * LinkedCache.java July 2012
 3  
  *
 4  
  * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 15  
  * implied. See the License for the specific language governing 
 16  
  * permissions and limitations under the License.
 17  
  */
 18  
 
 19  
 package org.simpleframework.xml.util;
 20  
 
 21  
 import java.util.LinkedHashMap;
 22  
 import java.util.Map.Entry;
 23  
 
 24  
 /**
 25  
  * The <code>LimitedCache</code> interface is used to represent a 
 26  
  * cache that will store key value pairs. This implementation is
 27  
  * backed by a <code>LinkedHashMap</code> so that only a specific
 28  
  * number of elements can be stored in the cache at one time.
 29  
  * 
 30  
  * @author Niall Gallagher
 31  
  */   
 32  
 public class LimitedCache<T> extends LinkedHashMap<Object, T> implements Cache<T> {
 33  
    
 34  
    /**
 35  
     * This represents the capacity of this cache instance.
 36  
     */
 37  
    private final int capacity;
 38  
    
 39  
    /**
 40  
     * Constructor of the <code>LimitedCache</code> object. This is
 41  
     * used to create a cache with a fixed size. The strategy for
 42  
     * this cache is least recently used. Any insert or fetch from
 43  
     * the cache is considered to be a use.
 44  
     */
 45  
    public LimitedCache() {
 46  2379
       this(50000);
 47  2379
    }
 48  
    
 49  
    /**
 50  
     * Constructor of the <code>LimitedCache</code> object. This is
 51  
     * used to create a cache with a fixed size. The strategy for
 52  
     * this cache is least recently used. Any insert or fetch from
 53  
     * the cache is considered to be a use.
 54  
     *
 55  
     * @param capacity this is the capacity of the cache object
 56  
     */
 57  2388
    public LimitedCache(int capacity) {
 58  2388
       this.capacity = capacity;
 59  2388
    }
 60  
 
 61  
    /**
 62  
     * This method is used to insert a key value mapping in to the
 63  
     * cache. The value can later be retrieved or removed from the
 64  
     * cache if desired. If the value associated with the key is 
 65  
     * null then nothing is stored within the cache.
 66  
     * 
 67  
     * @param key this is the key to cache the provided value to
 68  
     * @param value this is the value that is to be cached
 69  
     */
 70  
    public void cache(Object key, T value) {
 71  158
       put(key, value);
 72  158
    }
 73  
 
 74  
    /**
 75  
     * This is used to exclusively take the value mapped to the 
 76  
     * specified key from the cache. Invoking this is effectively
 77  
     * removing the value from the cache.
 78  
     * 
 79  
     * @param key this is the key to acquire the cache value with
 80  
     * 
 81  
     * @return this returns the value mapped to the specified key 
 82  
     */
 83  
    public T take(Object key) {
 84  0
       return remove(key);
 85  
    }
 86  
 
 87  
    /**
 88  
     * This method is used to get the value from the cache that is
 89  
     * mapped to the specified key. If there is no value mapped to
 90  
     * the specified key then this method will return a null.
 91  
     * 
 92  
     * @param key this is the key to acquire the cache value with
 93  
     * 
 94  
     * @return this returns the value mapped to the specified key 
 95  
     */
 96  
    public T fetch(Object key) {
 97  312
       return get(key);
 98  
    }
 99  
 
 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  0
       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  173
       return size() > capacity;
 124  
    }
 125  
 }