public static class PersistentContext.Reference<T> extends java.lang.Object implements Reference<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 a constructor for persistent maps.
public FastMap(String id) {
new PersistentContext<Map<K, V>.Reference(id, this) {
protected void notifyChange() {
FastMap.this.clear();
FastMap.this.putAll(this.get());
}
};
}
}
...
// Persistent lookup table for units multiplications.
static FastMap<Unit, FastMap<Unit, Unit>> UNITS_MULT_LOOKUP
= new FastMap<Unit, FastMap<Unit, Unit>>("UNITS_MULT_LOOKUP").shared();
Persistent references may also be used to hold optimum configuration
values set from previous executions. For example:
public Targets {
private static PersistentContext.Reference<Integer> CAPACITY
= new PersistentContext.Reference<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;
}
}
Constructor and Description |
---|
Reference(java.lang.String id,
T defaultValue)
Creates a persistent reference identified by the specified name and
having the specified default value.
|
Modifier and Type | Method and Description |
---|---|
T |
get()
Returns the value this reference referes to.
|
protected void |
notifyChange()
Notifies this reference that its value has changed (for example
a new persistent context has been loaded).
|
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.
|
public Reference(java.lang.String id, T defaultValue)
id
- the unique identifier.defaultValue
- the default value.java.lang.IllegalArgumentException
- if the name is not unique.public T get()
Reference
public void set(T value)
Reference
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 java.lang.String toString()
toString
in class java.lang.Object
String.valueOf(this.get())
protected void notifyChange()
Copyright © 2005 - 2007 Javolution.