javolution.context
Class PersistentContext.Reference<T>

Object
  extended by PersistentContext.Reference<T>
All Implemented Interfaces:
Reference<T>
Enclosing class:
PersistentContext

public static class PersistentContext.Reference<T>
extends 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;
         }
     }

Version:
4.0, September 4, 2006
Author:
Jean-Marie Dautelle

Constructor Summary
PersistentContext.Reference(String id, T defaultValue)
          Creates a persistent reference identified by the specified name and having the specified default value.
 
Method Summary
 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).
 String toString()
          Returns the string representation of the current value of this reference.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PersistentContext.Reference

public PersistentContext.Reference(String id,
                                   T defaultValue)
Creates a persistent reference identified by the specified name and having the specified default value.

Parameters:
id - the unique identifier.
defaultValue - the default value.
Throws:
IllegalArgumentException - if the name is not unique.
Method Detail

get

public T get()
Description copied from interface: Reference
Returns the value this reference referes to.

Specified by:
get in interface Reference<T>
Returns:
the referent or null if not set.

set

public void set(T value)
Description copied from interface: Reference
Sets the value this reference referes to.

Specified by:
set in interface Reference<T>
Parameters:
value - the reference value.

setMinimum

public void setMinimum(T value)
Sets this reference to the specified value only if (value.compareTo(this.get()) > 0).

Parameters:
value - the minimum value for this reference.
Throws:
IllegalArgumentException - if the specified value is not Comparable or an Integer instance (J2ME).

setMaximum

public void setMaximum(T value)
Sets this reference to the specified value only if (value.compareTo(this.get()) < 0).

Parameters:
value - the maximum value for this reference.
Throws:
IllegalArgumentException - if the specified value is not Comparable or an Integer instance (J2ME).

toString

public String toString()
Returns the string representation of the current value of this reference.

Overrides:
toString in class Object
Returns:
String.valueOf(this.get())

notifyChange

protected void notifyChange()
Notifies this reference that its value has changed (for example a new persistent context has been loaded). The default implementation does nothing.



Copyright © 2005-2012 Javolution. All Rights Reserved.