Class LocalReference<T>
- java.lang.Object
-
- javolution37.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 anyLocalContextscope affects the reference default value (equivalent tosetDefault(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 threadsspawned from within the sameLocalContext.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description LocalReference()Default constructor (default referent isnull).LocalReference(T defaultValue)Creates a local reference having the specified default value.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description Tget()Returns the local value for this reference.TgetDefault()Returns the default value for this reference.TgetLocal()Returns the local (non-inherited) value for this reference.voidset(T value)Sets the local value (referent) for this reference.voidsetDefault(T defaultValue)Sets the default value for this reference.java.lang.StringtoString()Returns the string representation of the current value of this reference.
-
-
-
Constructor Detail
-
LocalReference
public LocalReference()
Default constructor (default referent isnull).
-
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 outerLocalContextis searched first, then all outerLocalContextare recursively searched up to the global root context which contains the default value.
-
set
public void set(T value)
Sets the local value (referent) for this reference.
-
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
nullif 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:
toStringin classjava.lang.Object- Returns:
String.valueOf(this.get())
-
-
DMelt 3.0 © DataMelt by jWork.ORG