1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.gridsystems.beanfilter;
18
19 import java.lang.reflect.Array;
20 import java.util.HashSet;
21
22
23
24
25
26
27
28 public final class ArrayValue extends EvalValue {
29
30
31
32
33 public enum ArrayType { INDEXED, ALL_ITEMS, ANY_ITEMS };
34
35
36
37
38 private ArrayType arrayType;
39
40
41
42
43 private EvalValue value;
44
45
46
47
48 private Integer index = null;
49
50
51
52
53
54
55
56
57
58
59 public ArrayValue(Token token, EvalValue value, Token arrayIndexType,
60 Class<?> parentClass) throws EvalException {
61 super(token.beginLine, token.beginColumn);
62
63 Class<?> valueClass = value.getClassValue(parentClass);
64 if (!valueClass.isArray()) {
65
66 throw new EvalException("FTR010", token.beginLine, token.beginColumn);
67 }
68
69 this.value = value;
70 switch (arrayIndexType.kind) {
71 case FilterParserConstants.NUMBER:
72 this.index = Integer.parseInt(arrayIndexType.image);
73 if (this.index < 0) {
74
75
76 throw new EvalException("FTR009", token.beginLine, token.beginColumn, index);
77 }
78 this.arrayType = ArrayType.INDEXED;
79 break;
80 case FilterParserConstants.ALLITEMS:
81 this.arrayType = ArrayType.ALL_ITEMS;
82 break;
83 case FilterParserConstants.ANYITEMS:
84 this.arrayType = ArrayType.ANY_ITEMS;
85 break;
86 default:
87 throw new EvalException("UNK000", "Unknown array indexation type "
88 + arrayIndexType.kind);
89 }
90 }
91
92
93
94
95 @Override public Object getValue(Object src) throws EvalException {
96 Object myarray = value.getValue(src);
97 if (myarray == null) {
98 return null;
99 }
100 if (index == null) {
101 HashSet<Object> hash;
102 hash = arrayToHash(myarray, this.arrayType);
103 return hash;
104 } else {
105 int count = Array.getLength(myarray);
106 int i = index.intValue();
107 if ((i < 0) || (i >= count)) {
108
109 return null;
110 }
111 Object obj = Array.get(myarray, i);
112 return processObject(obj);
113 }
114 }
115
116
117
118
119
120
121
122
123 public static HashSet<Object> arrayToHash(Object myarray, ArrayType arrayType) {
124 HashSet<Object> hash;
125 int count = Array.getLength(myarray);
126 if (arrayType == ArrayType.ALL_ITEMS) {
127 hash = new AllItemsHashSet(count);
128 } else {
129 hash = new AnyItemsHashSet(count);
130 }
131 for (int i = 0; i < count; i++) {
132 hash.add(processObject(Array.get(myarray, i)));
133 }
134 return hash;
135 }
136
137
138
139
140
141
142
143 private static Object processObject(Object obj) {
144 if (obj == null) {
145 return null;
146 }
147 if (obj instanceof Number) {
148 if (obj instanceof Long || obj instanceof Integer
149 || obj instanceof Short || obj instanceof Byte) {
150 return ((Number)obj).longValue();
151 } else {
152 return ((Number)obj).doubleValue();
153 }
154 }
155 return obj;
156 }
157
158
159
160
161 @Override public String toString() {
162 return value + "[" + ((index == null) ? "*" : index.toString()) + "]";
163 }
164
165
166
167
168 @Override
169 public Class<?> getClassValue(Class<?> parentClass) throws EvalException {
170 Class<?> valueClass = value.getClassValue(parentClass);
171 return valueClass.getComponentType();
172 }
173 }