[Schmitzm-commits] r1546 - trunk/schmitzm-core/src/main/java/de/schmitzm/lang
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Thu Mar 31 17:55:02 CEST 2011
Author: alfonx
Date: 2011-03-31 17:55:01 +0200 (Thu, 31 Mar 2011)
New Revision: 1546
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LimitedHashMap.java
Log:
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LimitedHashMap.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LimitedHashMap.java 2011-03-31 15:51:56 UTC (rev 1545)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LimitedHashMap.java 2011-03-31 15:55:01 UTC (rev 1546)
@@ -41,246 +41,277 @@
import de.schmitzm.data.event.ObjectTraceable;
/**
- * This class represents a {@link HashMap} which is limited to a number
- * of elements. When the maximum number of elements is exceeded, the
- * "oldest" element is removed.<br>
- * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
+ * This class represents a {@link HashMap} which is limited to a number of
+ * elements. When the maximum number of elements is exceeded, the "oldest"
+ * element is removed.<br>
+ *
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ * (University of Bonn/Germany)
* @version 1.0
*/
-public class LimitedHashMap<K,V> extends HashMap<K,V> implements ObjectTraceable {
-
- /**
- * Defines which element is removed when the maximum number of elements
- * is exceeded.
- */
- public enum TRUNC_METHOD {
- /** The oldest element in the map (with the oldest {@code put(.)}-call)
- * is removed. */
- OLDEST_PUT,
- /** The element with the oldest accessed (oldest {@code get(.)}-call)
- * is removed. */
- OLDEST_GET
- }
-
- /** Defines the behavior to truncate the map when the maximum number
- * of elements is exceeded. */
- protected TRUNC_METHOD truncMethod = null;
- /** Holds the maximum count of elements, the map can hold. */
- protected int maxLoad = 0;
- /** Holds the keys of the map in the order of age. */
- protected Vector<K> oldestKeys = new Vector<K>();
+public class LimitedHashMap<K, V> extends HashMap<K, V> implements
+ ObjectTraceable {
+ private static final long serialVersionUID = -5653826993038290630L;
- // Einfache Implementierung von ObjectTraceable, um die Methoden der
- // Listener-Verwaltung nicht doppelt implementieren zu muessen
- private AbstractObjectTraceable objectTraceableProxy = new AbstractObjectTraceable() {
- };
-
+ /**
+ * Defines which element is removed when the maximum number of elements is
+ * exceeded.
+ */
+ public enum TRUNC_METHOD {
+ /**
+ * The oldest element in the map (with the oldest {@code put(.)}-call)
+ * is removed.
+ */
+ OLDEST_PUT,
+ /**
+ * The element with the oldest accessed (oldest {@code get(.)}-call) is
+ * removed.
+ */
+ OLDEST_GET
+ }
- /**
- * Creates an empty map using the {@link TRUNC_METHOD#OLDEST_GET} method to remove objects when map load is exceeded
- * @param maxLoad max. number of elements the map can hold
- */
- public LimitedHashMap(int maxLoad) {
- this(maxLoad, TRUNC_METHOD.OLDEST_GET);
- }
+ /**
+ * Defines the behavior to truncate the map when the maximum number of
+ * elements is exceeded.
+ */
+ protected TRUNC_METHOD truncMethod = null;
+ /** Holds the maximum count of elements, the map can hold. */
+ protected int maxLoad = 0;
+ /** Holds the keys of the map in the order of age. */
+ protected Vector<K> oldestKeys = new Vector<K>();
- /**
- * Creates an empty map.
- * @param maxLoad max. number of elements the map can hold
- * @param truncMethod method applied if the maximum map load is exceeded
- */
- public LimitedHashMap(int maxLoad, TRUNC_METHOD truncMethod) {
- super(maxLoad);
- this.truncMethod = truncMethod;
- setMaxLoad(maxLoad);
- }
+ // Einfache Implementierung von ObjectTraceable, um die Methoden der
+ // Listener-Verwaltung nicht doppelt implementieren zu muessen
+ private AbstractObjectTraceable objectTraceableProxy = new AbstractObjectTraceable() {
+ };
- /**
- * Returns the maximum number of elements, the map can hold.
- */
- public int getMaxLoad() {
- return maxLoad;
- }
+ /**
+ * Creates an empty map using the {@link TRUNC_METHOD#OLDEST_GET} method to
+ * remove objects when map load is exceeded
+ *
+ * @param maxLoad
+ * max. number of elements the map can hold
+ */
+ public LimitedHashMap(int maxLoad) {
+ this(maxLoad, TRUNC_METHOD.OLDEST_GET);
+ }
- /**
- * Sets the maximum number of elements, the map can hold. If this number
- * is exceeded, the oldest element is removed.
- * @param maxLoad maximum number of elements, the map can hold
- * @see TRUNC_METHOD
- */
- public void setMaxLoad(int maxLoad) {
- this.maxLoad = maxLoad;
- truncate();
- }
-
- /**
- * Returns the behavior when truncating surplus elements.
- */
- public TRUNC_METHOD getTruncMethod() {
- return this.truncMethod;
- }
+ /**
+ * Creates an empty map.
+ *
+ * @param maxLoad
+ * max. number of elements the map can hold
+ * @param truncMethod
+ * method applied if the maximum map load is exceeded
+ */
+ public LimitedHashMap(int maxLoad, TRUNC_METHOD truncMethod) {
+ super(maxLoad);
+ this.truncMethod = truncMethod;
+ setMaxLoad(maxLoad);
+ }
- /**
- * Removes the oldest element from the map until the maximum element
- * count is reached.
- */
- protected void truncate() {
- while( size() > maxLoad ) {
- // determine the oldest element key (and remove it)
- K oldestKey = oldestKeys.remove(0);
- // remove element from map
- remove( oldestKey );
- }
- }
+ /**
+ * Returns the maximum number of elements, the map can hold.
+ */
+ public int getMaxLoad() {
+ return maxLoad;
+ }
- ///////////////////////////////////////////////////////////////////
- ///////////////// Ueberschreiben von Vector ///////////////////
- ///////////////////////////////////////////////////////////////////
- /**
- * Adds an element to the map and removes all surplus elements.
- * An {@link ObjectChangeEvent} is initiated if the map changes.
- * @param key element key
- * @param value element value
- * @return the former value mapped to {@code key} ({@code null} if there
- * was no such element)
- */
- @Override
- public V put(K key, V value) {
- int oldSize = size();
- V oldValue = super.put(key,value);
-
- // truncate the map before updating the oldest list, so that
- // the new element is ignored in this truncate
- truncate();
-
- // update the "oldest" list
- switch ( truncMethod ) {
- // OLDEST_PUT: the new element is the youngest, so add the key of the
- // new element at the end of the oldest list
- case OLDEST_PUT: oldestKeys.add(key);
- break;
-//MS-01.sc: Adding the new element to the top of the oldest-list causes that
-// it might be removed immediately on the next put/truncate!
-// Although this would be consequent, this is not practical.
-// Instead we put the new element at the end of the oldest-list
-// so that the element stays in the map at least "for a while".
-// // OLDEST_GET: the new element is not yet accessed by get, so add the key
-// // at the front of the oldest list
-// case OLDEST_GET: oldestKeys.add(0,key);
-// break;
- // OLDEST_GET: to keep the new element in the map at least "for a while",
- // it is added at the end of the oldest-list (like OLDEST_PUT)
- case OLDEST_GET: oldestKeys.add(key);
- break;
-//MS-01.ec
- }
-
- // fire an event if the map has changed
- if ( oldValue != value )
- fireEvent( new ObjectChangeEvent(new Invoker(this),oldValue,value) );
-
- return oldValue;
- }
+ /**
+ * Sets the maximum number of elements, the map can hold. If this number is
+ * exceeded, the oldest element is removed.
+ *
+ * @param maxLoad
+ * maximum number of elements, the map can hold
+ * @see TRUNC_METHOD
+ */
+ public void setMaxLoad(int maxLoad) {
+ this.maxLoad = maxLoad;
+ truncate();
+ }
- /**
- * Returns an element of the map.
- * @param key element key
- */
- @Override
- public V get(Object key) {
- V value = super.get(key);
-
- // update the oldest list if the truncate method is OLDEST_GET
- if ( oldestKeys.contains(key) && TRUNC_METHOD.OLDEST_GET.equals(truncMethod) ) {
- oldestKeys.remove(key); // remove from current position
- oldestKeys.add((K)key); // add to the end
- }
-
- return value;
- }
+ /**
+ * Returns the behavior when truncating surplus elements.
+ */
+ public TRUNC_METHOD getTruncMethod() {
+ return this.truncMethod;
+ }
- /**
- * Removes an element from the map and fires an {@link ObjectChangeEvent},
- * if the map has changed.
- * @param key element key
- * @return the former value mapped to {@code key} ({@code null} if there
- * was no such element)
- */
- @Override
- public V remove(Object key) {
- // remove element from map
- V oldValue = super.remove(key);
- // remove key from oldest list
- oldestKeys.remove(key);
+ /**
+ * Removes the oldest element from the map until the maximum element count
+ * is reached.
+ */
+ protected void truncate() {
+ while (size() > maxLoad) {
+ // determine the oldest element key (and remove it)
+ K oldestKey = oldestKeys.remove(0);
+ // remove element from map
+ remove(oldestKey);
+ }
+ }
- // fire change event if the list has changed
- if ( oldValue != null )
- fireEvent( new ObjectChangeEvent(new Invoker(this),oldValue,null) );
-
- return oldValue;
- }
+ // /////////////////////////////////////////////////////////////////
+ // /////////////// Ueberschreiben von Vector ///////////////////
+ // /////////////////////////////////////////////////////////////////
+ /**
+ * Adds an element to the map and removes all surplus elements. An
+ * {@link ObjectChangeEvent} is initiated if the map changes.
+ *
+ * @param key
+ * element key
+ * @param value
+ * element value
+ * @return the former value mapped to {@code key} ({@code null} if there was
+ * no such element)
+ */
+ @Override
+ public V put(K key, V value) {
+ int oldSize = size();
+ V oldValue = super.put(key, value);
- /**
- * Clears the map and fires an {@link GeneralObjectChangeEvent} if the list
- * changes.
- */
- @Override
- public void clear() {
- if ( isEmpty() )
- return;
- // clear the map
- super.clear();
- // clear the oldest list
- oldestKeys.clear();
-
- fireEvent(new GeneralObjectChangeEvent(this));
- }
+ // truncate the map before updating the oldest list, so that
+ // the new element is ignored in this truncate
+ truncate();
+ // update the "oldest" list
+ switch (truncMethod) {
+ // OLDEST_PUT: the new element is the youngest, so add the key of the
+ // new element at the end of the oldest list
+ case OLDEST_PUT:
+ oldestKeys.add(key);
+ break;
+ // MS-01.sc: Adding the new element to the top of the oldest-list causes
+ // that
+ // it might be removed immediately on the next put/truncate!
+ // Although this would be consequent, this is not practical.
+ // Instead we put the new element at the end of the oldest-list
+ // so that the element stays in the map at least "for a while".
+ // // OLDEST_GET: the new element is not yet accessed by get, so add the
+ // key
+ // // at the front of the oldest list
+ // case OLDEST_GET: oldestKeys.add(0,key);
+ // break;
+ // OLDEST_GET: to keep the new element in the map at least
+ // "for a while",
+ // it is added at the end of the oldest-list (like OLDEST_PUT)
+ case OLDEST_GET:
+ oldestKeys.add(key);
+ break;
+ // MS-01.ec
+ }
- ///////////////////////////////////////////////////////////////////
- //////////// Implementierung von ObjectTraceable //////////////
- ///////////////////////////////////////////////////////////////////
- /**
- * Adds a listener to the map, which is informed on map changes.
- */
- public void addObjectListener(ObjectListener listener) {
- this.objectTraceableProxy.addObjectListener(listener);
- }
+ // fire an event if the map has changed
+ if (oldValue != value)
+ fireEvent(new ObjectChangeEvent(new Invoker(this), oldValue, value));
- /**
- * Removes a listener from the map.
- */
- public void removeObjectListener(ObjectListener listener) {
- this.objectTraceableProxy.removeObjectListener(listener);
- }
+ return oldValue;
+ }
- /**
- * Checks, whether a listener is already connected to the map.
- */
- public boolean containsObjectListener(ObjectListener l) {
- return this.objectTraceableProxy.containsObjectListener(l);
- }
+ /**
+ * Returns an element of the map.
+ *
+ * @param key
+ * element key
+ */
+ @Override
+ public V get(Object key) {
+ V value = super.get(key);
- /**
- * Returns all listener of a special type.
- * @param type kind of listener (filter)
- */
- public ObjectListener[] getObjectListener(Class type) {
- return this.objectTraceableProxy.getObjectListener(type);
- }
+ // update the oldest list if the truncate method is OLDEST_GET
+ if (oldestKeys.contains(key)
+ && TRUNC_METHOD.OLDEST_GET.equals(truncMethod)) {
+ oldestKeys.remove(key); // remove from current position
+ oldestKeys.add((K) key); // add to the end
+ }
- /**
- * Fires an event to all connected listeners.
- */
- public void fireEvent(ObjectEvent e) {
- this.objectTraceableProxy.fireEvent(e);
- }
+ return value;
+ }
- /**
- * Fires an event to all listeners of a special type.
- */
- public void fireEvent(ObjectEvent e, Class c) {
- this.objectTraceableProxy.fireEvent(e, c);
- }
+ /**
+ * Removes an element from the map and fires an {@link ObjectChangeEvent},
+ * if the map has changed.
+ *
+ * @param key
+ * element key
+ * @return the former value mapped to {@code key} ({@code null} if there was
+ * no such element)
+ */
+ @Override
+ public V remove(Object key) {
+ // remove element from map
+ V oldValue = super.remove(key);
+ // remove key from oldest list
+ oldestKeys.remove(key);
+ // fire change event if the list has changed
+ if (oldValue != null)
+ fireEvent(new ObjectChangeEvent(new Invoker(this), oldValue, null));
+
+ return oldValue;
+ }
+
+ /**
+ * Clears the map and fires an {@link GeneralObjectChangeEvent} if the list
+ * changes.
+ */
+ @Override
+ public void clear() {
+ if (isEmpty())
+ return;
+ // clear the map
+ super.clear();
+ // clear the oldest list
+ oldestKeys.clear();
+
+ fireEvent(new GeneralObjectChangeEvent(this));
+ }
+
+ // /////////////////////////////////////////////////////////////////
+ // ////////// Implementierung von ObjectTraceable //////////////
+ // /////////////////////////////////////////////////////////////////
+ /**
+ * Adds a listener to the map, which is informed on map changes.
+ */
+ public void addObjectListener(ObjectListener listener) {
+ this.objectTraceableProxy.addObjectListener(listener);
+ }
+
+ /**
+ * Removes a listener from the map.
+ */
+ public void removeObjectListener(ObjectListener listener) {
+ this.objectTraceableProxy.removeObjectListener(listener);
+ }
+
+ /**
+ * Checks, whether a listener is already connected to the map.
+ */
+ public boolean containsObjectListener(ObjectListener l) {
+ return this.objectTraceableProxy.containsObjectListener(l);
+ }
+
+ /**
+ * Returns all listener of a special type.
+ *
+ * @param type
+ * kind of listener (filter)
+ */
+ public ObjectListener[] getObjectListener(Class type) {
+ return this.objectTraceableProxy.getObjectListener(type);
+ }
+
+ /**
+ * Fires an event to all connected listeners.
+ */
+ public void fireEvent(ObjectEvent e) {
+ this.objectTraceableProxy.fireEvent(e);
+ }
+
+ /**
+ * Fires an event to all listeners of a special type.
+ */
+ public void fireEvent(ObjectEvent e, Class c) {
+ this.objectTraceableProxy.fireEvent(e, c);
+ }
+
}
More information about the Schmitzm-commits
mailing list