001/*
002 * ConcurrentCache.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.concurrent.ConcurrentHashMap;
022
023/**
024 * The <code>ConcurrentCache</code> interface is used to represent a 
025 * cache that will store key value pairs. This implementation is
026 * backed by a <code>ConcurrentHashMap</code> for best performance.
027 * 
028 * @author Niall Gallagher
029 */
030public class ConcurrentCache<T> extends ConcurrentHashMap<Object, T> implements Cache<T> {
031   
032   /**
033    * Constructor for the <code>ConcurrentCache</code> object. This
034    * is an implementation of a cache that uses the conventional
035    * concurrent hash map from the Java collections API.
036    */
037   public ConcurrentCache() {
038      super();
039   }
040   
041   /**
042    * This method is used to insert a key value mapping in to the
043    * cache. The value can later be retrieved or removed from the
044    * cache if desired. If the value associated with the key is 
045    * null then nothing is stored within the cache.
046    * 
047    * @param key this is the key to cache the provided value to
048    * @param value this is the value that is to be cached
049    */
050   public void cache(Object key, T value) {
051      put(key, value);
052   }
053
054   /**
055    * This is used to exclusively take the value mapped to the 
056    * specified key from the cache. Invoking this is effectively
057    * removing the value from the cache.
058    * 
059    * @param key this is the key to acquire the cache value with
060    * 
061    * @return this returns the value mapped to the specified key 
062    */
063   public T take(Object key) {
064      return remove(key);
065   }
066
067   /**
068    * This method is used to get the value from the cache that is
069    * mapped to the specified key. If there is no value mapped to
070    * the specified key then this method will return a null.
071    * 
072    * @param key this is the key to acquire the cache value with
073    * 
074    * @return this returns the value mapped to the specified key 
075    */
076   public T fetch(Object key) {
077      return get(key);
078   }
079
080   /**
081    * This is used to determine whether the specified key exists
082    * with in the cache. Typically this can be done using the 
083    * fetch method, which will acquire the object. 
084    * 
085    * @param key this is the key to check within this segment
086    * 
087    * @return true if the specified key is within the cache
088    */
089   public boolean contains(Object key) {
090      return containsKey(key);
091   }
092}