Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » util » cache » [javadoc | source]
    1   package org.apache.lucene.util.cache;
    2   
    3   /**
    4    * Licensed to the Apache Software Foundation (ASF) under one or more
    5    * contributor license agreements.  See the NOTICE file distributed with
    6    * this work for additional information regarding copyright ownership.
    7    * The ASF licenses this file to You under the Apache License, Version 2.0
    8    * (the "License"); you may not use this file except in compliance with
    9    * the License.  You may obtain a copy of the License at
   10    *
   11    *     http://www.apache.org/licenses/LICENSE-2.0
   12    *
   13    * Unless required by applicable law or agreed to in writing, software
   14    * distributed under the License is distributed on an "AS IS" BASIS,
   15    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   16    * See the License for the specific language governing permissions and
   17    * limitations under the License.
   18    */
   19   
   20   import java.io.Closeable;
   21   
   22   /**
   23    * Base class for cache implementations.
   24    */
   25   public abstract class Cache<K,V> implements Closeable {
   26     
   27     /**
   28      * Simple Cache wrapper that synchronizes all
   29      * calls that access the cache. 
   30      */
   31     static class SynchronizedCache<K,V> extends Cache<K,V> {
   32       private Object mutex;
   33       private Cache<K,V> cache;
   34       
   35       SynchronizedCache(Cache<K,V> cache) {
   36         this.cache = cache;
   37         this.mutex = this;
   38       }
   39       
   40       SynchronizedCache(Cache<K,V> cache, Object mutex) {
   41         this.cache = cache;
   42         this.mutex = mutex;
   43       }
   44       
   45       @Override
   46       public void put(K key, V value) {
   47         synchronized(mutex) {cache.put(key, value);}
   48       }
   49       
   50       @Override
   51       public V get(Object key) {
   52         synchronized(mutex) {return cache.get(key);}
   53       }
   54       
   55       @Override
   56       public boolean containsKey(Object key) {
   57         synchronized(mutex) {return cache.containsKey(key);}
   58       }
   59       
   60       @Override
   61       public void close() {
   62         synchronized(mutex) {cache.close();}
   63       }
   64       
   65       @Override
   66       Cache<K,V> getSynchronizedCache() {
   67         return this;
   68       }
   69     }
   70     
   71     /**
   72      * Returns a thread-safe cache backed by the specified cache. 
   73      * In order to guarantee thread-safety, all access to the backed cache must
   74      * be accomplished through the returned cache.
   75      */
   76     public static <K,V> Cache<K,V> synchronizedCache(Cache<K,V> cache) {
   77       return cache.getSynchronizedCache();
   78     }
   79   
   80     /**
   81      * Called by {@link #synchronizedCache(Cache)}. This method
   82      * returns a {@link SynchronizedCache} instance that wraps
   83      * this instance by default and can be overridden to return
   84      * e. g. subclasses of {@link SynchronizedCache} or this
   85      * in case this cache is already synchronized.
   86      */
   87     Cache<K,V> getSynchronizedCache() {
   88       return new SynchronizedCache<K,V>(this);
   89     }
   90     
   91     /**
   92      * Puts a (key, value)-pair into the cache. 
   93      */
   94     public abstract void put(K key, V value);
   95     
   96     /**
   97      * Returns the value for the given key. 
   98      */
   99     public abstract V get(Object key);
  100     
  101     /**
  102      * Returns whether the given key is in this cache. 
  103      */
  104     public abstract boolean containsKey(Object key);
  105     
  106     /**
  107      * Closes the cache.
  108      */
  109     public abstract void close();
  110     
  111   }

Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » util » cache » [javadoc | source]