|
J avolution v3.7 (J2SE 1.5+) | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjavolution.lang.PersistentReference<T>
public class PersistentReference<T>
This class represents a reference over an object which can be kept
persistent accross multiple program executions. Instances of this class
are typically used to hold global data time consuming to regenerate.
For example:
public class FastMap<K,V> implements Map<K, V> {
// Provides constructor for persistent maps.
public FastMap(String id) {
PersistentReference<FastMap<K,V>> ref = new PersistentReference<FastMap<K,V>>(id);
FastMap<K,V> persistentMap = ref.get();
if (persistentMap != null) this.putAll(persistentMap);
ref.set(this); // Sets this map as the persistent map.
}
}
...
// Persistent lookup table for units multiplications.
static FastMap<Unit, FastMap<Unit, Unit>> UNITS_MULT_LOOKUP
= new FastMap<Unit, FastMap<Unit, Unit>>("UNITS_MULT_LOOKUP").setShared(true);
Persistent references may also be used to hold optimum configuration
values set from previous executions. For example:
public Targets {
private static PersistentReference<Integer> CAPACITY
= new PersistentReference<Integer>(Targets#CAPACITY, 256);
private Target[] _targets = new Target[CAPACITY.get()];
private int _count;
public void add(Target target) {
if (_count == _targets.length) { // Ooops, resizes.
Target[] tmp = new Target[_count * 2];
System.arraycopy(_targets, 0, tmp, 0, _count);
_targets = tmp;
CAPACITY.setMinimum(_targets.length); // Persists.
}
_targets[_count++] target;
}
}
How persistent references are loaded/saved is application specific.
Although, the simplest way is to use Javolution xml serialization
facility. For example:
import javolution.xml.ObjectReader;
import javolution.xml.ObjectWriter;
public void main(String[]) {
// Loads persistent reference values at start-up.
Map values = new ObjectReader<Map>().read(
new FileInputStream("C:/persistent.xml"));
PersistentReference.putAll(values)
...
new ObjectWriter<Map>().write(PersistentReference.values(),
new FileOutputStream("C:/persistent.xml"));
}
| Constructor Summary | |
|---|---|
PersistentReference(java.lang.String id)
Creates a persistent reference having the specified unique identifier. |
|
PersistentReference(java.lang.String id,
T defaultValue)
Creates a persistent reference having the specified unique identifier and default value if not set. |
|
| Method Summary | |
|---|---|
T |
get()
Returns the value this reference referes to. |
java.lang.String |
id()
Returns the unique identifier for this persistent reference. |
static void |
put(java.lang.String id,
java.lang.Object value)
Sets the value of the referenced object associated to the specified id. |
static void |
putAll(java.util.Map values)
Overwrites the current values with the ones specified (equivalent to put(id, value) for each entry of the specified map). |
void |
set(T value)
Sets the value this reference referes to. |
void |
setMaximum(T value)
Sets this reference to the specified value only if (value.compareTo(this.get()) < 0). |
void |
setMinimum(T value)
Sets this reference to the specified value only if (value.compareTo(this.get()) > 0). |
java.lang.String |
toString()
Returns the string representation of the current value of this reference. |
static java.util.Map |
values()
Returns a thread-safe, unmodifiable view over the persistent references values (id to value mapping). |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public PersistentReference(java.lang.String id)
id - the unique identifier.
java.lang.IllegalArgumentException - if the identifier is not unique.
public PersistentReference(java.lang.String id,
T defaultValue)
id - the unique identifier.defaultValue - the default value if not set.
java.lang.IllegalArgumentException - if the identifier is not unique.| Method Detail |
|---|
public final java.lang.String id()
public T get()
Reference
get in interface Reference<T>null if not set.public void set(T value)
Reference
set in interface Reference<T>value - the reference value.public void setMinimum(T value)
(value.compareTo(this.get()) > 0).
value - the minimum value for this reference.
java.lang.IllegalArgumentException - if the specified value is not
Comparable or an Integer instance (J2ME).public void setMaximum(T value)
(value.compareTo(this.get()) < 0).
value - the maximum value for this reference.
java.lang.IllegalArgumentException - if the specified value is not
Comparable or an Integer instance (J2ME).public static java.util.Map values()
public static void put(java.lang.String id,
java.lang.Object value)
id - the object identifier.value - the value to use instead of the current value.public static void putAll(java.util.Map values)
put(id, value) for each entry of the specified map).
values - the new values (id to value mapping).public java.lang.String toString()
toString in class java.lang.ObjectString.valueOf(this.get())
|
J avolution v3.7 (J2SE 1.5+) | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||