You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by jb...@apache.org on 2009/12/25 16:48:20 UTC
svn commit: r893873 -
/incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/Cachetable.java
Author: jbellis
Date: Fri Dec 25 15:48:19 2009
New Revision: 893873
URL: http://svn.apache.org/viewvc?rev=893873&view=rev
Log:
clean up generics, formatting of Cachetable. patch by gabriele renzi; reviewed by jbellis for CASSANDRA-654
Modified:
incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/Cachetable.java
Modified: incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/Cachetable.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/Cachetable.java?rev=893873&r1=893872&r2=893873&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/Cachetable.java (original)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/utils/Cachetable.java Fri Dec 25 15:48:19 2009
@@ -22,24 +22,26 @@
import org.apache.log4j.Logger;
-public class Cachetable<K,V> implements ICachetable<K,V>
+public class Cachetable<K, V> implements ICachetable<K, V>
{
- private class CacheableObject<V>
+ private class CacheableObject
{
private V value_;
private long age_;
-
+
CacheableObject(V o)
{
value_ = o;
age_ = System.currentTimeMillis();
}
+ @Override
public boolean equals(Object o)
{
return value_.equals(o);
}
+ @Override
public int hashCode()
{
return value_.hashCode();
@@ -48,49 +50,52 @@
V getValue()
{
return value_;
- }
-
+ }
+
boolean isReadyToDie(long expiration)
{
- return ( (System.currentTimeMillis() - age_) > expiration );
+ return ((System.currentTimeMillis() - age_) > expiration);
}
}
-
+
private class CacheMonitor extends TimerTask
{
private long expiration_;
-
+
CacheMonitor(long expiration)
{
expiration_ = expiration;
}
-
+
+ @Override
public void run()
- {
- Map<K,V> expungedValues = new HashMap<K,V>();
- synchronized(cache_)
+ {
+ Map<K, V> expungedValues = new HashMap<K, V>();
+ synchronized (cache_)
{
Enumeration<K> e = cache_.keys();
- while( e.hasMoreElements() )
- {
+ while (e.hasMoreElements())
+ {
K key = e.nextElement();
- CacheableObject<V> co = cache_.get(key);
- if ( co != null && co.isReadyToDie(expiration_) )
+ CacheableObject co = cache_.get(key);
+ if (co != null && co.isReadyToDie(expiration_))
{
V v = co.getValue();
- if(null != v)
+ if (null != v)
+ {
expungedValues.put(key, v);
- cache_.remove(key);
+ }
+ cache_.remove(key);
}
}
}
-
+
/* Calling the hooks on the keys that have been expunged */
- Set<K> keys = expungedValues.keySet();
- for ( K key : keys )
- {
+ Set<K> keys = expungedValues.keySet();
+ for (K key : keys)
+ {
V value = expungedValues.get(key);
- ICacheExpungeHook<K,V> hook = hooks_.remove(key);
+ ICacheExpungeHook<K, V> hook = hooks_.remove(key);
if (hook != null)
{
hook.callMe(key, value);
@@ -102,58 +107,60 @@
}
expungedValues.clear();
}
- }
+ }
- private ICacheExpungeHook<K,V> globalHook_;
- private Hashtable<K, CacheableObject<V>> cache_;
- private Map<K, ICacheExpungeHook<K,V>> hooks_;
+ private ICacheExpungeHook<K, V> globalHook_;
+ private Hashtable<K, CacheableObject> cache_;
+ private Map<K, ICacheExpungeHook<K, V>> hooks_;
private Timer timer_;
private static int counter_ = 0;
- private static Logger logger_ = Logger.getLogger(Cachetable.class);
+ private static final Logger LOGGER = Logger.getLogger(Cachetable.class);
private void init(long expiration)
{
- if ( expiration <= 0 )
+ if (expiration <= 0)
+ {
throw new IllegalArgumentException("Argument specified must be a positive number");
+ }
- cache_ = new Hashtable<K, CacheableObject<V>>();
- hooks_ = new Hashtable<K, ICacheExpungeHook<K,V>>();
- timer_ = new Timer("CACHETABLE-TIMER-" + (++counter_), true);
+ cache_ = new Hashtable<K, CacheableObject>();
+ hooks_ = new Hashtable<K, ICacheExpungeHook<K, V>>();
+ timer_ = new Timer("CACHETABLE-TIMER-" + (++counter_), true);
timer_.schedule(new CacheMonitor(expiration), expiration, expiration);
}
-
+
/*
- * Specify the TTL for objects in the cache
- * in milliseconds.
- */
+ * Specify the TTL for objects in the cache
+ * in milliseconds.
+ */
public Cachetable(long expiration)
{
- init(expiration);
- }
-
+ init(expiration);
+ }
+
/*
- * Specify the TTL for objects in the cache
- * in milliseconds and a global expunge hook. If
- * a key has a key-specific hook installed invoke that
- * instead.
- */
- public Cachetable(long expiration, ICacheExpungeHook<K,V> global)
+ * Specify the TTL for objects in the cache
+ * in milliseconds and a global expunge hook. If
+ * a key has a key-specific hook installed invoke that
+ * instead.
+ */
+ public Cachetable(long expiration, ICacheExpungeHook<K, V> global)
{
init(expiration);
- globalHook_ = global;
+ globalHook_ = global;
}
-
+
public void shutdown()
{
timer_.cancel();
}
-
+
public void put(K key, V value)
- {
- cache_.put(key, new CacheableObject<V>(value));
+ {
+ cache_.put(key, new CacheableObject(value));
}
- public void put(K key, V value, ICacheExpungeHook<K,V> hook)
+ public void put(K key, V value, ICacheExpungeHook<K, V> hook)
{
put(key, value);
hooks_.put(key, hook);
@@ -162,8 +169,8 @@
public V get(K key)
{
V result = null;
- CacheableObject<V> co = cache_.get(key);
- if ( co != null )
+ CacheableObject co = cache_.get(key);
+ if (co != null)
{
result = co.getValue();
}
@@ -172,9 +179,9 @@
public V remove(K key)
{
- CacheableObject<V> co = cache_.remove(key);
+ CacheableObject co = cache_.remove(key);
V result = null;
- if ( co != null )
+ if (co != null)
{
result = co.getValue();
}
@@ -193,7 +200,7 @@
public boolean containsValue(V value)
{
- return cache_.containsValue( new CacheableObject<V>(value) );
+ return cache_.containsValue(new CacheableObject(value));
}
public boolean isEmpty()
@@ -204,5 +211,5 @@
public Set<K> keySet()
{
return cache_.keySet();
- }
+ }
}