KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > realtime > LocalReference


1 package javolution.realtime;
2
3 import j2me.io.Serializable;
4 import javolution.lang.Reference;
5 import javolution.util.FastMap;
6
7 /**
8  * <p> This class represents a reference whose setting is local to the current
9  * {@link LocalContext}; setting outside of any {@link LocalContext} scope
10  * affects the reference default value (equivalent to {@link #setDefault}).
11  * For example:<pre>
12  * import org.jscience.physics.units.*;
13  * public class Length {
14  * private static final LocalReference&lt;Unit&gt; OUTPUT_UNIT
15  * = new LocalReference&lt;Unit&gt;(SI.METER); // Default value.
16  *
17  * // Sets of output unit for Length instances.
18  * public static showAs(Unit unit) {
19  * OUTPUT_UNIT.set(unit);
20  * }
21  * public Text toText() {
22  * return QuantityFormat.getInstance().format(this, OUTPUT_UNIT.get());
23  * }
24  * }
25  * ...
26  * public static void main(String[] args) {
27  * // Sets the default length output unit to Inches.
28  * Length.showAs(NonSI.INCH); // Affects all threads.
29  * }
30  * ...
31  * Vector result ... // Shows result with Length stated in kilometers.
32  * LocalContext.enter();
33  * try {
34  * Length.showAs(SI.KILO(SI.METER)); // Affects the local thread only.
35  * System.out.println(result);
36  * } finally {
37  * LocalContext.exit();
38  * }</pre></p>
39  *
40  * <p> Accessing a local reference is fast and is performed without internal
41  * synchronization (through the use of thread-safe {@link
42  * javolution.util fast collection} classes).
43  * Default setting are inherited by all threads (volatile), local settings
44  * are inherited by {@link ConcurrentThread concurrent threads} spawned
45  * from within the same {@link LocalContext}.</p>
46  *
47  */

48 public class LocalReference/*<T>*/ implements Reference/*<T>*/, Serializable {
49
50     /**
51      * Holds the default value for this variable.
52      */

53     private volatile Object/*T*/ _defaultValue;
54
55     /**
56      * Default constructor (default referent is <code>null</code>).
57      */

58     public LocalReference() {
59         this(null);
60     }
61
62     /**
63      * Creates a local reference having the specified default value.
64      *
65      * @param defaultValue the default value or root value of this variable.
66      */

67     public LocalReference(Object/*T*/ defaultValue) {
68         _defaultValue = defaultValue;
69     }
70
71     
72     /**
73      * Returns the local value for this reference.
74      * The first outer {@link LocalContext} is searched first, then
75      * all outer {@link LocalContext} are recursively searched up to the
76      * global root context which contains the default value.
77      *
78      * @return the context-local value.
79      */

80     public Object/*T*/ get() {
81         for (Context ctx = Context.currentContext(); ctx != null; ctx = ctx
82                 .getOuter()) {
83             if (ctx instanceof LocalContext) {
84                 Object value = ((LocalContext)ctx)._references.get(this);
85                 if (value != null) {
86                     return (Object/*T*/) value;
87                 }
88             }
89         }
90         // Not found, returns default value.
91
return _defaultValue;
92     }
93
94     /**
95      * Sets the local value (referent) for this reference.
96      *
97      * @param value the new local value or <code>null</code> to inherit
98      * the outer value.
99      */

100     public void set(Object/*T*/ value) {
101         for (Context ctx = Context.currentContext(); ctx != null; ctx = ctx
102                 .getOuter()) {
103             if (ctx instanceof LocalContext) {
104                 FastMap references = ((LocalContext)ctx)._references;
105                 synchronized (references) { // Setting have to be synchronized.
106
references.put(this, value);
107                 }
108                 return;
109             }
110         }
111         // No local context, sets default value.
112
_defaultValue = value;
113     }
114
115     /**
116      * Returns the default value for this reference.
117      *
118      * @return the defaultValue.
119      */

120     public Object/*T*/ getDefault() {
121         return _defaultValue;
122     }
123
124     /**
125      * Sets the default value for this reference.
126      *
127      * @param defaultValue the root value.
128      */

129     public void setDefault(Object/*T*/ defaultValue) {
130         _defaultValue = defaultValue;
131     }
132 }
Popular Tags