KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > mathematics > functions > Variable


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2006 - JScience (http://jscience.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package org.jscience.mathematics.functions;
10
11 import javolution.lang.Reference;
12 import javolution.context.LocalContext;
13
14 /**
15  * <p> This interface represents a symbol on whose value a {@link Function}
16  * depends. If the functions is not shared between multiple-threads the
17  * simple {@link Variable.Local} implementation can be used.
18  * For global functions (functions used concurrently by multiple threads)
19  * the {@link Variable.Global} implementation with
20  * {@link javolution.context.LocalContext context-local} settings is
21  * recommended.</p>
22  *
23  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
24  * @version 3.0, February 13, 2006
25  * @see Function#evaluate
26  */

27 public interface Variable<X> extends Reference<X> {
28
29     /**
30      * Returns the symbol for this variable.
31      *
32      * @return this variable's symbol.
33      */

34     String JavaDoc getSymbol();
35
36     /**
37      * This class represents a simple {@link Variable} implementation for
38      * functions not shared between threads (non static).
39      * Functions shared between multiple-threads should use a different
40      * type of variable such as {@link Variable.Global}.
41      */

42     public static class Local<X> implements Variable<X> {
43
44         /**
45          * Holds the reference value.
46          */

47         private X _value;
48
49         /**
50          * Holds the variable symbol.
51          */

52         private final String JavaDoc _symbol;
53
54         /**
55          * Creates a new local variable with a unique symbol.
56          *
57          * @param symbol the variable symbol.
58          */

59         public Local(String JavaDoc symbol) {
60             _symbol = symbol;
61         }
62
63         public String JavaDoc getSymbol() {
64             return _symbol;
65         }
66
67         public X get() {
68             return _value;
69         }
70
71         public void set(X arg0) {
72             _value = arg0;
73         }
74     }
75
76     /**
77      * This class represents a simple {@link Variable} implementation with
78      * {@link javolution.context.LocalContext context-local} values.
79      * Instances of this class can be set independently by multiple-threads
80      * as long as each concurrent thread executes within a
81      * {@link javolution.context.LocalContext LocalContext}. For example:[code]
82      * public abstract class Engine {
83      * public static final Variable.Global<Measure<AngularVelocity>> RPM
84      * = new Variable.Global<Measure<AngularVelocity>>("rpm");
85      * public abstract Function<Measure<AngularVelocity>, Measure<Torque>> getTorque();
86      * }
87      * ...
88      * LocalContext.enter();
89      * try {
90      * RPM.set(rpm);
91      * Measure<Torque> torque = myEngine.getTorque().evaluate();
92      * } finally {
93      * LocalContext.exit();
94      * }[/code]
95      * It should be noted that parameterized evaluations are performed within
96      * a local context. Therefore, the example
97      * above could also be rewritten:[code]
98      * Measure<Torque> torque = myEngine.getTorque().evaluate(rpm);
99      * [/code]
100      */

101     public static class Global<X> implements Variable<X> {
102
103         /**
104          * Holds the reference value.
105          */

106         private LocalContext.Reference<X> _value = new LocalContext.Reference<X>();
107
108         /**
109          * Holds the variable symbol.
110          */

111         private final String JavaDoc _symbol;
112
113         /**
114          * Creates a new global variable with a unique symbol.
115          *
116          * @param symbol the variable symbol.
117          */

118         public Global(String JavaDoc symbol) {
119             _symbol = symbol;
120         }
121
122         public String JavaDoc getSymbol() {
123             return _symbol;
124         }
125
126         public X get() {
127             return _value.get();
128         }
129
130         public void set(X arg0) {
131             _value.set(arg0);
132         }
133     }
134
135 }
Popular Tags