1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krms.framework.engine; |
17 | |
|
18 | |
import org.kuali.rice.core.api.mo.common.Coded; |
19 | |
import org.kuali.rice.core.api.util.jaxb.EnumStringAdapter; |
20 | |
|
21 | |
import java.util.Arrays; |
22 | |
import java.util.Collection; |
23 | |
import java.util.Collections; |
24 | |
|
25 | 1 | public enum ComparisonOperator implements Coded { |
26 | |
|
27 | 1 | EQUALS ("="), |
28 | 1 | NOT_EQUALS ("!="), |
29 | 1 | GREATER_THAN (">"), |
30 | 1 | GREATER_THAN_EQUAL (">="), |
31 | 1 | LESS_THAN ("<"), |
32 | 1 | LESS_THAN_EQUAL ("<="); |
33 | |
|
34 | |
private final String code; |
35 | |
|
36 | 6 | private ComparisonOperator(String code){ |
37 | 6 | this.code = code; |
38 | 6 | } |
39 | |
|
40 | |
@Override |
41 | |
public String getCode(){ |
42 | 6 | return code; |
43 | |
} |
44 | |
|
45 | |
public <T> boolean compare(Comparable<T> lhs, T rhs) { |
46 | 40 | int result = lhs.compareTo(rhs); |
47 | 40 | if (this == EQUALS) { |
48 | 0 | return result == 0; |
49 | |
} |
50 | 40 | if (this == NOT_EQUALS) { |
51 | 0 | return result != 0; |
52 | |
} |
53 | 40 | if (this == GREATER_THAN) { |
54 | 39 | return result > 0; |
55 | |
} |
56 | 1 | if (this == GREATER_THAN_EQUAL) { |
57 | 0 | return result >= 0; |
58 | |
} |
59 | 1 | if (this == LESS_THAN) { |
60 | 1 | return result < 0; |
61 | |
} |
62 | 0 | if (this == LESS_THAN_EQUAL) { |
63 | 0 | return result <= 0; |
64 | |
} |
65 | 0 | throw new IllegalStateException("Invalid operator detected: " + this); |
66 | |
} |
67 | |
|
68 | 1 | public static final Collection<String> OPERATOR_CODES = |
69 | |
Collections.unmodifiableCollection(Arrays.asList(EQUALS.getCode(), NOT_EQUALS.getCode(), GREATER_THAN.getCode(), |
70 | |
GREATER_THAN_EQUAL.getCode(), LESS_THAN.getCode(), LESS_THAN_EQUAL.getCode())); |
71 | |
|
72 | 1 | public static final Collection<String> OPERATOR_NAMES = |
73 | |
Collections.unmodifiableCollection(Arrays.asList(EQUALS.name(), NOT_EQUALS.name(), GREATER_THAN.name(), |
74 | |
GREATER_THAN_EQUAL.name(), LESS_THAN.name(), LESS_THAN_EQUAL.name())); |
75 | |
|
76 | |
public static ComparisonOperator fromCode(String code) { |
77 | 0 | if (code == null) { |
78 | 0 | return null; |
79 | |
} |
80 | 0 | for (ComparisonOperator operator : values()) { |
81 | 0 | if (operator.code.equals(code)) { |
82 | 0 | return operator; |
83 | |
} |
84 | |
} |
85 | 0 | throw new IllegalArgumentException("Failed to locate the ComparisonOperator with the given code: " + code); |
86 | |
} |
87 | |
|
88 | |
@Override |
89 | |
public String toString(){ |
90 | 0 | return code; |
91 | |
} |
92 | |
|
93 | 0 | static final class Adapter extends EnumStringAdapter<ComparisonOperator> { |
94 | |
|
95 | |
protected Class<ComparisonOperator> getEnumClass() { |
96 | 0 | return ComparisonOperator.class; |
97 | |
} |
98 | |
} |
99 | |
} |