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.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
27
28
29
30
31
32
33
34 public enum ComparisonOperator implements Coded {
35
36
37
38
39 EQUALS("="),
40
41
42
43
44 NOT_EQUALS("!="),
45
46
47
48
49 GREATER_THAN(">"),
50
51
52
53
54 GREATER_THAN_EQUAL(">="),
55
56
57
58
59 LESS_THAN("<"),
60
61
62
63
64 LESS_THAN_EQUAL("<="),
65
66
67
68
69 EXISTS("!=null"),
70
71
72
73
74 DOES_NOT_EXIST("=null");
75
76 private final String code;
77
78 ComparisonOperatorService comparisonOperatorService;
79
80
81
82
83
84 private ComparisonOperator(String code) {
85 this.code = code;
86 }
87
88
89
90
91
92 @Override
93 public String getCode() {
94 return code;
95 }
96
97
98
99
100
101
102
103 public static ComparisonOperator fromCode(String code) {
104 if (code == null) {
105 return null;
106 }
107 for (ComparisonOperator comparisonOperator : values()) {
108 if (comparisonOperator.code.equals(code)) {
109 return comparisonOperator;
110 }
111 }
112 throw new IllegalArgumentException("Failed to locate the ComparisionOperator with the given code: " + code);
113 }
114
115
116
117
118
119
120
121 public boolean compare(Object lhs, Object rhs) {
122 if (comparisonOperatorService == null) {
123 setComparisonOperatorService(KrmsApiServiceLocator.getComparisonOperatorService());
124 }
125 int result = comparisonOperatorService.compare(lhs, rhs);
126
127 if (this == EQUALS) {
128 return result == 0;
129 } else if (this == NOT_EQUALS) {
130 return result != 0;
131 } else if (this == GREATER_THAN) {
132 return result > 0;
133 } else if (this == GREATER_THAN_EQUAL) {
134 return result >= 0;
135 } else if (this == LESS_THAN) {
136 return result < 0;
137 } else if (this == LESS_THAN_EQUAL) {
138 return result <= 0;
139 } else if (this == EXISTS) {
140 return rhs != null;
141 } else if (this == DOES_NOT_EXIST) {
142 return rhs == null;
143 }
144 throw new IllegalStateException("Invalid comparison operator detected: " + this);
145 }
146
147
148
149
150
151 public static final Collection<String> OPERATOR_CODES;
152
153
154
155
156 public static final Collection<String> OPERATOR_NAMES;
157
158 static {
159 List<String> operatorCodes = new ArrayList<String>();
160 List<String> operatorNames = new ArrayList<String>();
161
162 for (ComparisonOperator operator : values()) {
163 operatorCodes.add(operator.getCode());
164 operatorNames.add(operator.name());
165 }
166
167 OPERATOR_CODES = Collections.unmodifiableCollection(operatorCodes);
168 OPERATOR_NAMES = Collections.unmodifiableCollection(operatorNames);
169 }
170
171 public void setComparisonOperatorService(ComparisonOperatorService comparisonOperatorService) {
172 this.comparisonOperatorService = comparisonOperatorService;
173 }
174
175
176
177
178
179 @Override
180 public String toString(){
181 return code;
182 }
183
184 static final class Adapter extends EnumStringAdapter<ComparisonOperator> {
185
186 @Override
187 protected Class<ComparisonOperator> getEnumClass() {
188 return ComparisonOperator.class;
189 }
190 }
191 }