You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jcs-dev@jakarta.apache.org by hc...@apache.org on 2005/01/19 11:58:23 UTC

cvs commit: jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/core CacheEntry.java CacheManager.java CacheManagerUtils.java

hchar       2005/01/19 02:58:23

  Added:       auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/core
                        CacheEntry.java CacheManager.java
                        CacheManagerUtils.java
  Log:
  no message
  
  Revision  Changes    Path
  1.1                  jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/core/CacheEntry.java
  
  Index: CacheEntry.java
  ===================================================================
  /*
   * CacheEntry.java
   *
   * Created on 17 January 2005, 05:42
   */
  
  package net.sf.yajcache.core;
  
  import java.util.Map;
  
  /**
   *
   * @author Hanson Char
   */
  public class CacheEntry<V> implements Map.Entry<String,V> {
      private final String key;
      private V value;
      /** Creates a new instance of CacheEntry */
      public CacheEntry(String key, V val) {
          this.key = key;
          this.value = val;
      }
  
      public String getKey() {
          return key;
      }
  
      public V getValue() {
          return value;
      }
  
      public V setValue(V val) {
          V ret = this.value;
          this.value = val;
          return ret;
      }
      
  }
  
  
  
  1.1                  jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/core/CacheManager.java
  
  Index: CacheManager.java
  ===================================================================
  /*
   * CacheManager.java
   *
   * Created on 17 January 2005, 05:58
   */
  
  package net.sf.yajcache.core;
  
  import java.util.concurrent.ConcurrentHashMap;
  import java.util.concurrent.ConcurrentMap;
  
  
  import net.sf.yajcache.soft.SoftRefCache;
  
  /**
   * @author Hanson Char
   */
  public enum CacheManager {
      inst;
      // Cache name to Cache mapping.
      private final ConcurrentMap<String,ICache<?>> map = new ConcurrentHashMap<String, ICache<?>>();
      /** 
       * Returns the cache for the specified name and value type;  
       * Creates the cache if necessary.
       *
       * @throws ClassCastException if the cache already exists for an
       * incompatible value type.
       */
      public <V> ICache<V> getCache(String name, Class<V> valueType)
      {
          ICache c = this.map.get(name);
                 
          if (c == null)
              c = this.createCache(name, valueType);
          else
              CacheManagerUtils.inst.checkValueType(c, valueType);
          return c;
      }
      /** 
       * Returns an existing cache for the specified name; or null if not found.
       */
      public ICache getCache(String name) {
          return this.map.get(name);
      }
      /**
       * Removes the specified cache, if it exists.
       */
      public ICache removeCache(String name) {
          ICache c = this.map.remove(name);
          
          if (c != null) {
              c.clear();
          }
          return c;
      }
      /** 
       * Creates the specified cache if not already created.
       * 
       * @return either the cache created by the current thread, or
       * an existing cache created earlier by another thread.
       */
      private <V> ICache<V> createCache(String name, Class<V> valueType) {
          ICache<V> c = new SoftRefCache<V>(name, valueType);
          ICache old = this.map.putIfAbsent(name, c);
  
          if (old != null) {
              // race condition: cache already created by another thread.
              CacheManagerUtils.inst.checkValueType(old, valueType);
              c = old;
          }
          return c;
      }
      /**
       * This package private method is used soley to simluate a race condition 
       * during cache creation for testing purposes.
       */
      <V> ICache<V> testCreateCacheRaceCondition(String name, Class<V> valueType) 
      {
          return this.createCache(name, valueType);
      }
  }
  
  
  
  1.1                  jakarta-turbine-jcs/auxiliary-builds/jdk15/yajcache/cache/src/net/sf/yajcache/core/CacheManagerUtils.java
  
  Index: CacheManagerUtils.java
  ===================================================================
  /*
   * CacheManagerUtils.java
   *
   * Created on 19 January 2005, 00:05
   */
  
  package net.sf.yajcache.core;
  
  /**
   *
   * @author Hanson Char
   */
  enum CacheManagerUtils {
      inst;
      /** Checks the value type assignability of an existing cache. */
      void checkValueType(ICache c, Class<?> valueType) {
          if (!c.getValueType().isAssignableFrom(valueType))
              throw new ClassCastException("Cache " + c.getName()
                  + " of " + c.getValueType() 
                  + " already exists and cannot be used for " + valueType);
          return;
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-dev-help@jakarta.apache.org