1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.betwixt.expression;
18
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Enumeration;
22 import java.util.Iterator;
23 import java.util.Map;
24
25 import org.apache.commons.collections.iterators.ArrayIterator;
26 import org.apache.commons.collections.iterators.EnumerationIterator;
27
28 /*** <p><code>IteratorExpression</code> returns an iterator over the current context.</p>
29 *
30 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31 * @version $Revision: 1.8 $
32 */
33 public class IteratorExpression implements Expression {
34
35 /*** Use this <code>Expression</code> to perform initial evaluation*/
36 private Expression expression;
37
38 /***
39 * Construct <code>IteratorExpression</code> using given expression for initial evaluation.
40 * @param expression this expression will be evaluated and the result converted to an
41 * iterator.
42 */
43 public IteratorExpression(Expression expression) {
44 this.expression = expression;
45 }
46
47 /***
48 * Returns an interator over the current context
49 * @see org.apache.commons.betwixt.expression.Expression
50 */
51 public Object evaluate(Context context) {
52
53 Object value = expression.evaluate( context );
54
55
56
57 if ( value instanceof Iterator ) {
58
59 return (Iterator) value;
60
61 } else if ( value instanceof Collection ) {
62
63 Collection collection = (Collection) value;
64 return collection.iterator();
65
66 } else if ( value instanceof Map ) {
67
68 Map map = (Map) value;
69 return map.entrySet().iterator();
70
71 } else if ( value instanceof Enumeration ) {
72
73 return new EnumerationIterator( (Enumeration) value );
74
75 } else if ( value != null ) {
76
77 Class type = value.getClass();
78 if ( type.isArray() ) {
79 return new ArrayIterator( value );
80 }
81 }
82
83
84
85 return Collections.EMPTY_LIST.iterator();
86 }
87
88 /***
89 * Do nothing
90 * @see org.apache.commons.betwixt.expression.Expression
91 */
92 public void update(Context context, String newValue) {
93
94 }
95
96 /***
97 * Returns something useful for logging
98 * @return string useful for logging
99 */
100 public String toString() {
101 return "IteratorExpression [expression=" + expression + "]";
102 }
103 }