Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: ognl/Evaluation.java


1   //--------------------------------------------------------------------------
2   //  Copyright (c) 1998-2004, Drew Davidson and Luke Blanshard
3   //  All rights reserved.
4   //
5   //  Redistribution and use in source and binary forms, with or without
6   //  modification, are permitted provided that the following conditions are
7   //  met:
8   //
9   //  Redistributions of source code must retain the above copyright notice,
10  //  this list of conditions and the following disclaimer.
11  //  Redistributions in binary form must reproduce the above copyright
12  //  notice, this list of conditions and the following disclaimer in the
13  //  documentation and/or other materials provided with the distribution.
14  //  Neither the name of the Drew Davidson nor the names of its contributors
15  //  may be used to endorse or promote products derived from this software
16  //  without specific prior written permission.
17  //
18  //  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  //  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  //  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  //  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  //  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  //  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  //  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25  //  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  //  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  //  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28  //  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29  //  DAMAGE.
30  //--------------------------------------------------------------------------
31  package ognl;
32  
33  import java.util.*;
34  
35  /**
36      An <code>Evaluation</code> is and object that holds a node being evaluated
37      and the source from which that node will take extract its
38      value.  It refers to child evaluations that occur as
39      a result of the nodes' evaluation.
40   */
41  public class Evaluation extends Object
42  {
43      private SimpleNode          node;
44      private Object              source;
45      private boolean             setOperation;
46      private Object              result;
47      private Throwable           exception;
48      private Evaluation          parent;
49      private Evaluation          next;
50      private Evaluation          previous;
51      private Evaluation          firstChild;
52      private Evaluation          lastChild;
53  
54      /**
55          Constructs a new "get" <code>Evaluation</code> from the node and source given.
56       */
57      public Evaluation(SimpleNode node, Object source)
58      {
59          super();
60          this.node = node;
61          this.source = source;
62      }
63  
64      /**
65          Constructs a new <code>Evaluation</code> from the node and source given.
66          If <code>setOperation</code> is true this <code>Evaluation</code> represents
67          a "set" as opposed to a "get".
68       */
69      public Evaluation(SimpleNode node, Object source, boolean setOperation)
70      {
71          this(node, source);
72          this.setOperation = setOperation;
73      }
74  
75      /**
76          Returns the <code>SimpleNode</code> for this <code>Evaluation</code>
77       */
78      public SimpleNode getNode()
79      {
80          return node;
81      }
82  
83      /**
84          Sets the node of the evaluation.  Normally applications do not need to
85          set this.  Notable exceptions to this rule are custom evaluators that
86          choose between navigable objects (as in a multi-root evaluator where
87          the navigable node is chosen at runtime).
88       */
89      public void setNode(SimpleNode value)
90      {
91          node = value;
92      }
93  
94      /**
95          Returns the source object on which this Evaluation operated.
96       */
97      public Object getSource()
98      {
99          return source;
100     }
101 
102     /**
103         Sets the source of the evaluation.  Normally applications do not need to
104         set this.  Notable exceptions to this rule are custom evaluators that
105         choose between navigable objects (as in a multi-root evaluator where
106         the navigable node is chosen at runtime).
107      */
108     public void setSource(Object value)
109     {
110         source = value;
111     }
112 
113     /**
114         Returns true if this Evaluation represents a set operation.
115      */
116     public boolean isSetOperation()
117     {
118         return setOperation;
119     }
120 
121     /**
122         Marks the Evaluation as a set operation if the value is true, else
123         marks it as a get operation.
124      */
125     public void setSetOperation(boolean value)
126     {
127         setOperation = value;
128     }
129 
130     /**
131         Returns the result of the Evaluation, or null if it was a set operation.
132      */
133     public Object getResult()
134     {
135         return result;
136     }
137 
138     /**
139         Sets the result of the Evaluation.  This method is normally only used
140         interally and should not be set without knowledge of what you are doing.
141      */
142     public void setResult(Object value)
143     {
144         result = value;
145     }
146 
147     /**
148         Returns the exception that occurred as a result of evaluating the
149         Evaluation, or null if no exception occurred.
150      */
151     public Throwable getException()
152     {
153         return exception;
154     }
155 
156     /**
157         Sets the exception that occurred as a result of evaluating the
158         Evaluation.  This method is normally only used interally and
159         should not be set without knowledge of what you are doing.
160      */
161     public void setException(Throwable value)
162     {
163         exception = value;
164     }
165 
166     /**
167         Returns the parent evaluation of this evaluation.  If this returns
168         null then it is is the root evaluation of a tree.
169      */
170     public Evaluation getParent()
171     {
172         return parent;
173     }
174 
175     /**
176         Returns the next sibling of this evaluation.  Returns null if
177         this is the last in a chain of evaluations.
178      */
179     public Evaluation getNext()
180     {
181         return next;
182     }
183 
184     /**
185         Returns the previous sibling of this evaluation.  Returns null if
186         this is the first in a chain of evaluations.
187      */
188     public Evaluation getPrevious()
189     {
190         return previous;
191     }
192 
193     /**
194         Returns the first child of this evaluation.  Returns null if
195         there are no children.
196      */
197     public Evaluation getFirstChild()
198     {
199         return firstChild;
200     }
201 
202     /**
203         Returns the last child of this evaluation.  Returns null if
204         there are no children.
205      */
206     public Evaluation getLastChild()
207     {
208         return lastChild;
209     }
210 
211     /**
212         Gets the first descendent.  In any Evaluation tree this will the
213         Evaluation that was first executed.
214      */
215     public Evaluation getFirstDescendant()
216     {
217         if (firstChild != null) {
218             return firstChild.getFirstDescendant();
219         }
220         return this;
221     }
222 
223     /**
224         Gets the last descendent.  In any Evaluation tree this will the
225         Evaluation that was most recently executing.
226      */
227     public Evaluation getLastDescendant()
228     {
229         if (lastChild != null) {
230             return lastChild.getLastDescendant();
231         }
232         return this;
233     }
234 
235     /**
236         Adds a child to the list of children of this evaluation.  The
237         parent of the child is set to the receiver and the children
238         references are modified in the receiver to reflect the new child.
239         The lastChild of the receiver is set to the child, and the
240         firstChild is set also if child is the first (or only) child.
241      */
242     public void addChild(Evaluation child)
243     {
244         if (firstChild == null) {
245             firstChild = lastChild = child;
246         } else {
247             if (firstChild == lastChild) {
248                 firstChild.next = child;
249                 lastChild = child;
250                 lastChild.previous = firstChild;
251             } else {
252                 child.previous = lastChild;
253                 lastChild.next = child;
254                 lastChild = child;
255             }
256         }
257         child.parent = this;
258     }
259 
260     /**
261         Reinitializes this Evaluation to the parameters specified.
262      */
263     public void init(SimpleNode node, Object source, boolean setOperation)
264     {
265         this.node = node;
266         this.source = source;
267         this.setOperation = setOperation;
268         result = null;
269         exception = null;
270         parent = null;
271         next = null;
272         previous = null;
273         firstChild = null;
274         lastChild = null;
275     }
276 
277     /**
278         Resets this Evaluation to the initial state.
279      */
280     public void reset()
281     {
282         init(null, null, false);
283     }
284 
285     /**
286         Produces a String value for the Evaluation.  If compact is
287         true then a more compact form of the description only including
288         the node type and unique identifier is shown, else a full
289         description including source and result are shown.  If showChildren
290         is true the child evaluations are printed using the depth string
291         given as a prefix.
292      */
293     public String toString(boolean compact, boolean showChildren, String depth)
294     {
295         String      stringResult;
296 
297         if (compact) {
298             stringResult = depth + "<" + node.getClass().getName() + " " + System.identityHashCode(this) + ">";
299         } else {
300             String      ss = (source != null) ? source.getClass().getName() : "null",
301                         rs = (result != null) ? result.getClass().getName() : "null";
302 
303             stringResult = depth + "<" + node.getClass().getName() + ": [" + (setOperation ? "set" : "get") + "] source = " + ss + ", result = " + result + " [" + rs + "]>";
304         }
305         if (showChildren) {
306             Evaluation  child = firstChild;
307 
308             stringResult += "\n";
309             while (child != null) {
310                 stringResult += child.toString(compact, depth + "  ");
311                 child = child.next;
312             }
313         }
314         return stringResult;
315     }
316 
317     /**
318         Produces a String value for the Evaluation.  If compact is
319         true then a more compact form of the description only including
320         the node type and unique identifier is shown, else a full
321         description including source and result are shown.  Child
322         evaluations are printed using the depth string given as a prefix.
323      */
324     public String toString(boolean compact, String depth)
325     {
326         return toString(compact, true, depth);
327     }
328 
329     /**
330         Returns a String description of the Evaluation.
331      */
332     public String toString()
333     {
334         return toString(false, "");
335     }
336 }