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