// (c) Tuomas J. Lukka package org.fenfire.util; import java.util.*; import org.fenfire.*; import org.fenfire.util.*; /** A useful building block class for function caches. * This class represents a single cache entry, which may * be valid or DIRTY at any given time. *

* Triggering chg() of the Obs interface on a FunctionCacheEntry * sets the value to DIRTY, and calls all associated Obses * (added through the addObs method). * The obses are removed after being triggered. */ public class FunctionCacheEntry implements Obs { static final public Object DIRTY = new Object(); /** The input this cache entry is for. * Stored here so that it may be used as the hash key. */ public final Object input; /** The value of the cache. * Either DIRTY or the real value. */ public Object value = DIRTY; /** The set of Obs object that observe this cache entry. */ private Set obses; public FunctionCacheEntry(Object input) { this.input = input; } public void addObs(Obs o) { if(obses == null) obses = new HashSet(); obses.add(o); } public void chg() { if(obses != null) { for(Iterator i = obses.iterator(); i.hasNext();) { ((Obs)i.next()).chg(); } obses = null; } value = DIRTY; } }