Coverage Report - net.sourceforge.rcache.Cache
 
Classes in this File Line Coverage Branch Coverage Complexity
Cache
N/A
N/A
0
Cache$Operation
100%
5/5
N/A
0
 
 1  
 /*
 2  
  * RCache - A collection of simple reference-based cache implementations.
 3  
  * Copyright (C) 2007  Rodrigo Ruiz
 4  
  *
 5  
  * This library is free software; you can redistribute it and/or
 6  
  * modify it under the terms of the GNU Lesser General Public
 7  
  * License as published by the Free Software Foundation; either
 8  
  * version 2.1 of the License, or (at your option) any later version.
 9  
  *
 10  
  * This library is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
  * Lesser General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU Lesser General Public
 16  
  * License along with this library; if not, write to the Free Software
 17  
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 18  
  *
 19  
  * Alternatively, the contents of this file may be used under the terms
 20  
  * of the Apache 2.0 license (the "Apache License"), in which case its
 21  
  * provisions are applicable instead of those above. If you wish to allow use
 22  
  * of your version of this file only* under the terms of the Apache License
 23  
  * and not to allow others to use your version of this file under the LGPL,
 24  
  * indicate your decision by* deleting the provisions above and replace them
 25  
  * with the notice and other provisions required by the Apache License. If
 26  
  * you do not delete the provisions above, a recipient may use your version of
 27  
  * this file under either the LGPL or the Apache License.
 28  
  */
 29  
 package net.sourceforge.rcache;
 30  
 
 31  
 /**
 32  
  * <p>Memory-Sensitive Cache.</p>
 33  
  *
 34  
  * <p>Cache implementations will probably be implemented as Maps and so, this
 35  
  * interface is maintained "compatible" with {@link java.util.Map}. However,
 36  
  * in most situations, the Map interface is more complex than needed.</p>
 37  
  *
 38  
  * <p>This interface offers a simpler option, more oriented to the actual goals
 39  
  * of a Cache.</p>
 40  
  *
 41  
  * @author Rodrigo Ruiz
 42  
  * @param <K> the type of keys maintained by this cache
 43  
  * @param <V> the type of cached values
 44  
  */
 45  
 public interface Cache<K, V> {
 46  
 
 47  
   /**
 48  
    * <p>The operations defined in the Cache interface.</p>
 49  
    *
 50  
    * <p>This enumeration can be used for any method needing to
 51  
    * target a specific operation by name.</p>
 52  
    */
 53  5
   enum Operation {
 54  
     /** get() operation. */
 55  1
     GET,
 56  
     /** put() operation. */
 57  1
     PUT,
 58  
     /** remove() operation. */
 59  1
     REMOVE,
 60  
     /** clear() operation. */
 61  1
     CLEAR
 62  
   };
 63  
 
 64  
   /**
 65  
    * Default cache initial capacity.
 66  
    */
 67  
   int DEFAULT_INITIAL_CAPACITY = 100;
 68  
 
 69  
   /**
 70  
    * Default cache load factor.
 71  
    */
 72  
   float DEFAULT_LOAD_FACTOR = 0.75f;
 73  
 
 74  
   /**
 75  
    * Default concurrency level. Appropriate for multiple readers and
 76  
    * a single writer.
 77  
    */
 78  
   int DEFAULT_CONCURRENCY_LEVEL = 1;
 79  
 
 80  
   /**
 81  
    * <p>Returns the value to which this cache maps the specified key.  Returns
 82  
    * <tt>null</tt> if the cache contains no entry for this key.  A return
 83  
    * value of <tt>null</tt> does not <i>necessarily</i> indicate that the
 84  
    * cache contains no entry for the key; it's also possible that the cache
 85  
    * explicitly maps the key to <tt>null</tt></p>.
 86  
    *
 87  
    * <p>Although this method should use <tt>K</tt> for the key type, it
 88  
    * is left as <tt>Object</tt> to keep this interface compatible with
 89  
    * {@link java.util.Map}.</p>
 90  
    *
 91  
    * @param key key whose associated value is to be returned.
 92  
    * @return the value to which this cache associates the specified key.
 93  
    */
 94  
   V get(Object key);
 95  
 
 96  
   /**
 97  
    * <p>Associates the specified value with the specified key in this cache,
 98  
    * but only if no value is associated yet. If the cache previously contained
 99  
    * an entry for this key, the old value is returned, but not replaced.</p>
 100  
    *
 101  
    * <p>This method is equivalent to the <tt>putIfAbsent</tt> from the
 102  
    * {@link java.util.concurrent.ConcurrentMap} class, as it is more
 103  
    * appropriate for a cache than the original one from {@link java.util.Map}.
 104  
    *
 105  
    * @param key key with which the specified value is to be associated.
 106  
    * @param value value to be associated with the specified key.
 107  
    *
 108  
    * @return the value associated with specified key. If the key was already
 109  
    *         associated with a value, the old value is returned. Otherwise,
 110  
    *         <tt>value</tt> is returned
 111  
    *
 112  
    * @throws ClassCastException if the class of the specified key or value
 113  
    *         prevents it from being stored in this cache.
 114  
    *
 115  
    * @throws IllegalArgumentException if some aspect of this key or value
 116  
    *         prevents it from being stored in this map.
 117  
    */
 118  
   V put(K key, V value);
 119  
 
 120  
   /**
 121  
    * <p>Removes the entry for this key from this cache if present.</p>
 122  
    *
 123  
    * <p>Although this method should use <tt>K</tt> for the key type, it
 124  
    * is left as <tt>Object</tt> to keep this interface compatible with
 125  
    * {@link java.util.Map}.</p>
 126  
    *
 127  
    * @param key key whose entry is to be removed from the cache.
 128  
    * @return previous value associated with specified key, or <tt>null</tt>
 129  
    *         if there was no entry for key.  (A <tt>null</tt> return can
 130  
    *         also indicate that the cache previously associated <tt>null</tt>
 131  
    *         with the specified key)
 132  
    */
 133  
   V remove(Object key);
 134  
 
 135  
   /**
 136  
    * <p>Removes all entries from this cache.</p>
 137  
    *
 138  
    * <p>This method must be handled very carefully, as it will break
 139  
    * the assumption of unique instances for cached factories.</p>
 140  
    */
 141  
   void clear();
 142  
 }