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
66
67
68 EXISTS("!=null"),
69
70
71
72
73 DOES_NOT_EXIST("=null");
74
75 private final String code;
76
77 ComparisonOperatorService comparisonOperatorService;
78
79
80
81
82
83 private ComparisonOperator(String code) {
84 this.code = code;
85 }
86
87
88
89
90
91 @Override
92 public String getCode() {
93 return code;
94 }
95
96
97
98
99
100
101
102 public static ComparisonOperator fromCode(String code) {
103 if (code == null) {
104 return null;
105 }
106 for (ComparisonOperator comparisonOperator : values()) {
107 if (comparisonOperator.code.equals(code)) {
108 return comparisonOperator;
109 }
110 }
111 throw new IllegalArgumentException("Failed to locate the ComparisionOperator with the given code: " + code);
112 }
113
114
115
116
117
118
119
120 public boolean compare(Object lhs, Object rhs) {
121 if (comparisonOperatorService == null) {
122 setComparisonOperatorService(KrmsApiServiceLocator.getComparisonOperatorService());
123 }
124 int result = comparisonOperatorService.compare(lhs, rhs);
125
126 if (this == EQUALS) {
127 return result == 0;
128 } else if (this == NOT_EQUALS) {
129 return result != 0;
130 } else if (this == GREATER_THAN) {
131 return result > 0;
132 } else if (this == GREATER_THAN_EQUAL) {
133 return result >= 0;
134 } else if (this == LESS_THAN) {
135 return result < 0;
136 } else if (this == LESS_THAN_EQUAL) {
137 return result <= 0;
138 } else if (this == EXISTS) {
139 return rhs != null;
140 } else if (this == DOES_NOT_EXIST) {
141 return rhs == null;
142 }
143 throw new IllegalStateException("Invalid comparison operator detected: " + this);
144 }
145
146
147
148
149
150 public static final Collection<String> OPERATOR_CODES =
151 Collections.unmodifiableCollection(Arrays.asList(EQUALS.getCode(), NOT_EQUALS.getCode(), GREATER_THAN.getCode(),
152 GREATER_THAN_EQUAL.getCode(), LESS_THAN.getCode(), LESS_THAN_EQUAL.getCode()));
153
154
155
156
157 public static final Collection<String> OPERATOR_NAMES =
158 Collections.unmodifiableCollection(Arrays.asList(EQUALS.name(), NOT_EQUALS.name(), GREATER_THAN.name(),
159 GREATER_THAN_EQUAL.name(), LESS_THAN.name(), LESS_THAN_EQUAL.name()));
160
161 public void setComparisonOperatorService(ComparisonOperatorService comparisonOperatorService) {
162 this.comparisonOperatorService = comparisonOperatorService;
163 }
164
165
166
167
168
169 @Override
170 public String toString(){
171 return code;
172 }
173
174 static final class Adapter extends EnumStringAdapter<ComparisonOperator> {
175
176 @Override
177 protected Class<ComparisonOperator> getEnumClass() {
178 return ComparisonOperator.class;
179 }
180 }
181 }