J avolution v3.7 (J2SE 1.5+)

javolution.realtime
Class LocalReference<T>

java.lang.Object
  extended by javolution.realtime.LocalReference<T>
All Implemented Interfaces:
java.io.Serializable, Reference<T>

public class LocalReference<T>
extends java.lang.Object
implements Reference<T>, java.io.Serializable

This class represents a reference whose setting is local to the current LocalContext; setting outside of any LocalContext scope affects the reference default value (equivalent to setDefault(T)). For example:

     public class Foo {
         public static final LocalReference<TextFormat<Foo>> FORMAT 
             = new LocalReference<TextFormat<Foo>>(DEFAULT_FORMAT);
             
         public Text toString() {
              return FORMAT.get().format(this).toString();
         }     
     }
     ...
     LocalContext.enter();
     try {
        Foo.FORMAT.set(localFormat);
        ... // This thread displays Foo instances using localFormat. 
     } finally {
        LocalContext.exit(); // Reverts to previous format.
     }

Accessing/setting a local reference is fast and does not require any form of synchronization. Local settings are inherited by concurrent threads spawned from within the same LocalContext.

See Also:
Serialized Form

Constructor Summary
LocalReference()
          Default constructor (default referent is null).
LocalReference(T defaultValue)
          Creates a local reference having the specified default value.
 
Method Summary
 T get()
          Returns the local value for this reference.
 T getDefault()
          Returns the default value for this reference.
 T getLocal()
          Returns the local (non-inherited) value for this reference.
 void set(T value)
          Sets the local value (referent) for this reference.
 void setDefault(T defaultValue)
          Sets the default value for this reference.
 java.lang.String toString()
          Returns the string representation of the current value of this reference.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LocalReference

public LocalReference()
Default constructor (default referent is null).


LocalReference

public LocalReference(T defaultValue)
Creates a local reference having the specified default value.

Parameters:
defaultValue - the default value or root value of this variable.
Method Detail

get

public final T get()
Returns the local value for this reference. The first outer LocalContext is searched first, then all outer LocalContext are recursively searched up to the global root context which contains the default value.

Specified by:
get in interface Reference<T>
Returns:
the context-local value.

set

public void set(T value)
Sets the local value (referent) for this reference.

Specified by:
set in interface Reference<T>
Parameters:
value - the new local value or null to inherit the outer value.

getDefault

public T getDefault()
Returns the default value for this reference.

Returns:
the defaultValue.

getLocal

public T getLocal()
Returns the local (non-inherited) value for this reference.

Returns:
the local value or null if none (value to be inherited or not set).

setDefault

public void setDefault(T defaultValue)
Sets the default value for this reference.

Parameters:
defaultValue - the root value.

toString

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

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

J avolution v3.7 (J2SE 1.5+)

Copyright © 2006 Javolution.