1
1
package com .regnosys .rosetta .interpreternew .visitors ;
2
2
3
+ import java .lang .reflect .InvocationTargetException ;
3
4
import java .util .Arrays ;
4
5
import java .util .List ;
5
6
6
7
import com .regnosys .rosetta .interpreternew .values .RosettaInterpreterBaseValue ;
7
8
import com .regnosys .rosetta .interpreternew .values .RosettaInterpreterBooleanValue ;
8
9
import com .regnosys .rosetta .interpreternew .values .RosettaInterpreterError ;
9
10
import com .regnosys .rosetta .interpreternew .values .RosettaInterpreterErrorValue ;
10
- import com .regnosys .rosetta .interpreternew .values .RosettaInterpreterIntegerValue ;
11
11
import com .regnosys .rosetta .interpreternew .values .RosettaInterpreterListValue ;
12
- import com .regnosys .rosetta .interpreternew .values .RosettaInterpreterNumberValue ;
13
- import com .regnosys .rosetta .interpreternew .values .RosettaInterpreterStringValue ;
14
12
import com .regnosys .rosetta .rosetta .expression .ModifiableBinaryOperation ;
15
13
import com .regnosys .rosetta .rosetta .expression .RosettaExpression ;
16
14
import com .regnosys .rosetta .rosetta .interpreter .RosettaInterpreterValue ;
@@ -21,6 +19,17 @@ public class RosettaInterpreterComparisonOperationInterpreter extends
21
19
private static List <String > comparisonOperators =
22
20
Arrays .asList ("<" , "<=" , ">" , ">=" , "=" , "<>" );
23
21
22
+ private static List <String > comparableClasses =
23
+ Arrays .asList (
24
+ "com.regnosys.rosetta.interpreternew.values."
25
+ + "RosettaInterpreterBooleanValue" ,
26
+ "com.regnosys.rosetta.interpreternew.values."
27
+ + "RosettaInterpreterIntegerValue" ,
28
+ "com.regnosys.rosetta.interpreternew.values."
29
+ + "RosettaInterpreterNumberValue" ,
30
+ "com.regnosys.rosetta.interpreternew.values."
31
+ + "RosettaInterpreterStringValue" );
32
+
24
33
/**
25
34
* Interprets a comparison operation, evaluating the comparison between two operands.
26
35
*
@@ -77,32 +86,9 @@ private RosettaInterpreterBaseValue compareAny(RosettaInterpreterValue leftValue
77
86
//list vs list case:
78
87
if (leftValue instanceof RosettaInterpreterListValue
79
88
&& rightValue instanceof RosettaInterpreterListValue ) {
80
-
81
- //only way this is allowed is if rightValue has a length of 1
82
- // and left has length more than 1
83
- RosettaInterpreterListValue rgtList =
84
- (RosettaInterpreterListValue ) rightValue ;
85
- RosettaInterpreterListValue lfList =
86
- (RosettaInterpreterListValue ) leftValue ;
87
- if (rgtList .getExpressions ().size () == 1
88
- && lfList .getExpressions ().size () > 1 ) {
89
-
90
-
91
- //for all elements in left list, check if the comparison
92
- // between them and right-hand side is true
93
- boolean anyTrue = false ;
94
- for (RosettaInterpreterValue e : lfList .getExpressions ()) {
95
- anyTrue |= checkComparableTypes (e ,
96
- rgtList .getExpressions ().get (0 ),
97
- operator );
98
- }
99
- return new RosettaInterpreterBooleanValue (anyTrue );
100
- }
101
- else {
102
- return new RosettaInterpreterErrorValue (
103
- new RosettaInterpreterError (
104
- "cannot compare two lists" ));
105
- }
89
+ return new RosettaInterpreterErrorValue (
90
+ new RosettaInterpreterError (
91
+ "cannot compare two lists" ));
106
92
}
107
93
108
94
//list vs element case:
@@ -141,32 +127,9 @@ private RosettaInterpreterBaseValue compareAll(RosettaInterpreterValue leftValue
141
127
//list vs list case:
142
128
if (leftValue instanceof RosettaInterpreterListValue
143
129
&& rightValue instanceof RosettaInterpreterListValue ) {
144
-
145
- //only way this is allowed is if rightValue has a length of 1
146
- // and left has length more than 1
147
- RosettaInterpreterListValue rgtList =
148
- (RosettaInterpreterListValue ) rightValue ;
149
- RosettaInterpreterListValue lfList =
150
- (RosettaInterpreterListValue ) leftValue ;
151
- if (rgtList .getExpressions ().size () == 1
152
- && lfList .getExpressions ().size () > 1 ) {
153
-
154
-
155
- //for all elements in left list, check if the comparison
156
- // between them and right-hand side is true
157
- boolean allTrue = true ;
158
- for (RosettaInterpreterValue e : lfList .getExpressions ()) {
159
- allTrue &= checkComparableTypes (e ,
160
- rgtList .getExpressions ().get (0 ),
161
- operator );
162
- }
163
- return new RosettaInterpreterBooleanValue (allTrue );
164
- }
165
- else {
166
- return new RosettaInterpreterErrorValue (
167
- new RosettaInterpreterError (
168
- "cannot compare two lists" ));
169
- }
130
+ return new RosettaInterpreterErrorValue (
131
+ new RosettaInterpreterError (
132
+ "cannot compare two lists" ));
170
133
}
171
134
172
135
//list vs element case:
@@ -204,56 +167,52 @@ private boolean checkComparableTypes(RosettaInterpreterValue leftValue,
204
167
String operator ) {
205
168
int comparisonResult = 2 ;
206
169
207
- //compare integers
208
- if (leftValue instanceof RosettaInterpreterIntegerValue
209
- && rightValue instanceof RosettaInterpreterIntegerValue ) {
210
- RosettaInterpreterIntegerValue leftInt =
211
- (RosettaInterpreterIntegerValue ) leftValue ;
212
- RosettaInterpreterIntegerValue rightInt =
213
- (RosettaInterpreterIntegerValue ) rightValue ;
214
-
215
- comparisonResult = leftInt .compareTo (rightInt );
216
- }
217
170
218
- //compare booleans
219
- else if (leftValue instanceof RosettaInterpreterBooleanValue
220
- && rightValue instanceof RosettaInterpreterBooleanValue ) {
221
- RosettaInterpreterBooleanValue leftBool =
222
- (RosettaInterpreterBooleanValue ) leftValue ;
223
- RosettaInterpreterBooleanValue rightBool =
224
- (RosettaInterpreterBooleanValue ) rightValue ;
225
-
226
- comparisonResult = leftBool .compareTo (rightBool );
227
- }
171
+ boolean isComparable = false ;
228
172
229
- //compare strings
230
- else if (leftValue instanceof RosettaInterpreterStringValue
231
- && rightValue instanceof RosettaInterpreterStringValue ) {
232
- RosettaInterpreterStringValue leftString =
233
- (RosettaInterpreterStringValue ) leftValue ;
234
- RosettaInterpreterStringValue rightString =
235
- (RosettaInterpreterStringValue ) rightValue ;
236
-
237
- comparisonResult = leftString .compareTo (rightString );
238
- }
173
+ //left and right will be of type comparableClazz
174
+ Class <?> comparableClazz = null ;
239
175
240
- //compare numbers
241
- else if (leftValue instanceof RosettaInterpreterNumberValue
242
- && rightValue instanceof RosettaInterpreterNumberValue ) {
243
- RosettaInterpreterNumberValue leftNumber =
244
- (RosettaInterpreterNumberValue ) leftValue ;
245
- RosettaInterpreterNumberValue rightNumber =
246
- (RosettaInterpreterNumberValue ) rightValue ;
247
-
248
- comparisonResult = leftNumber .compareTo (rightNumber );
176
+ try {
177
+ for (String clazzString : comparableClasses ) {
178
+ Class <?> clazz = Class .forName (clazzString );
179
+
180
+ //check that both expressions are of the same type
181
+ boolean isInstance = (clazz .isInstance (leftValue )
182
+ && clazz .isInstance (rightValue ));
183
+ if (isInstance ) {
184
+ comparableClazz = clazz ;
185
+ }
186
+ isComparable |= isInstance ;
187
+ }
188
+ } catch (ClassNotFoundException e ) {
189
+ // TODO Auto-generated catch block
190
+ System .out .println ("Not a class" );
191
+ e .printStackTrace ();
249
192
}
250
193
251
-
194
+ if (isComparable ) {
195
+ try {
196
+ //compare the two expressions
197
+ comparisonResult = (int ) comparableClazz
198
+ .getDeclaredMethod ("compareTo" ,comparableClazz )
199
+ .invoke (leftValue , rightValue );
200
+ } catch (IllegalAccessException | IllegalArgumentException
201
+ | InvocationTargetException | NoSuchMethodException
202
+ | SecurityException e ) {
203
+ // TODO Auto-generated catch block
204
+ e .printStackTrace ();
205
+ }
206
+ }
207
+
252
208
return compareComparableValues (comparisonResult ,
253
209
operator );
254
210
}
255
211
256
212
private boolean compareComparableValues (int comparisonResult , String operator ) {
213
+ if (comparisonResult == 2 ) {
214
+ return false ;
215
+ }
257
216
switch (operator ) {
258
217
case "=" :
259
218
return comparisonResult == 0 ;
0 commit comments