1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.framework.engine.expression;
17
18 import org.kuali.rice.core.api.mo.common.Coded;
19 import org.kuali.rice.core.api.util.jaxb.EnumStringAdapter;
20 import org.kuali.rice.krms.api.KrmsApiServiceLocator;
21 import org.kuali.rice.krms.api.engine.expression.ComparisonOperatorService;
22
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Collections;
26
27
28
29
30
31
32
33 public enum ComparisonOperator implements Coded {
34
35
36
37
38 EQUALS("="),
39
40
41
42
43 NOT_EQUALS("!="),
44
45
46
47
48 GREATER_THAN(">"),
49
50
51
52
53 GREATER_THAN_EQUAL(">="),
54
55
56
57
58 LESS_THAN("<"),
59
60
61
62
63 LESS_THAN_EQUAL("<=");
64
65 private final String code;
66
67
68
69
70
71 private ComparisonOperator(String code) {
72 this.code = code;
73 }
74
75
76
77
78
79 @Override
80 public String getCode() {
81 return code;
82 }
83
84
85
86
87
88
89
90 public static ComparisonOperator fromCode(String code) {
91 if (code == null) {
92 return null;
93 }
94 for (ComparisonOperator comparisonOperator : values()) {
95 if (comparisonOperator.code.equals(code)) {
96 return comparisonOperator;
97 }
98 }
99 throw new IllegalArgumentException("Failed to locate the ComparisionOperator with the given code: " + code);
100 }
101
102
103
104
105
106
107
108 public boolean compare(Object lhs, Object rhs) {
109 EngineComparatorExtension extension = determineComparatorOperator(lhs, rhs);
110
111 int result = extension.compare(lhs, rhs);
112
113 if (this == EQUALS) {
114 return result == 0;
115 } else if (this == NOT_EQUALS) {
116 return result != 0;
117 } else if (this == GREATER_THAN) {
118 return result > 0;
119 } else if (this == GREATER_THAN_EQUAL) {
120 return result >= 0;
121 } else if (this == LESS_THAN) {
122 return result < 0;
123 } else if (this == LESS_THAN_EQUAL) {
124 return result <= 0;
125 }
126 throw new IllegalStateException("Invalid comparison operator detected: " + this);
127 }
128
129
130
131
132
133
134
135
136 private EngineComparatorExtension determineComparatorOperator(Object lhs, Object rhs) {
137 EngineComparatorExtension extension = null;
138 try {
139
140
141 ComparisonOperatorService service = KrmsApiServiceLocator.getComparisonOperatorService();
142 if (service.canCompare(lhs, rhs)) {
143 extension = service.findComparatorExtension(lhs, rhs);
144 }
145 } catch (Exception e) {
146 e.printStackTrace();
147 }
148 if (extension == null) {
149
150
151 extension = new DefaultComparisonOperator();
152 }
153 return extension;
154 }
155
156
157
158
159 public static final Collection<String> OPERATOR_CODES =
160 Collections.unmodifiableCollection(Arrays.asList(EQUALS.getCode(), NOT_EQUALS.getCode(), GREATER_THAN.getCode(),
161 GREATER_THAN_EQUAL.getCode(), LESS_THAN.getCode(), LESS_THAN_EQUAL.getCode()));
162
163
164
165
166 public static final Collection<String> OPERATOR_NAMES =
167 Collections.unmodifiableCollection(Arrays.asList(EQUALS.name(), NOT_EQUALS.name(), GREATER_THAN.name(),
168 GREATER_THAN_EQUAL.name(), LESS_THAN.name(), LESS_THAN_EQUAL.name()));
169
170
171
172
173
174 @Override
175 public String toString(){
176 return code;
177 }
178
179 static final class Adapter extends EnumStringAdapter<ComparisonOperator> {
180
181 @Override
182 protected Class<ComparisonOperator> getEnumClass() {
183 return ComparisonOperator.class;
184 }
185 }
186 }