| 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 | 0 | public enum ComparisonOperator implements Coded { |
| 26 | |
|
| 27 | 0 | EQUALS ("="), |
| 28 | 0 | NOT_EQUALS ("!="), |
| 29 | 0 | GREATER_THAN (">"), |
| 30 | 0 | GREATER_THAN_EQUAL (">="), |
| 31 | 0 | LESS_THAN ("<"), |
| 32 | 0 | LESS_THAN_EQUAL ("<="); |
| 33 | |
|
| 34 | |
private final String code; |
| 35 | |
|
| 36 | 0 | private ComparisonOperator(String code){ |
| 37 | 0 | this.code = code; |
| 38 | 0 | } |
| 39 | |
|
| 40 | |
@Override |
| 41 | |
public String getCode(){ |
| 42 | 0 | return code; |
| 43 | |
} |
| 44 | |
|
| 45 | |
public <T> boolean compare(Comparable<T> lhs, T rhs) { |
| 46 | 0 | int result = lhs.compareTo(rhs); |
| 47 | 0 | if (this == EQUALS) { |
| 48 | 0 | return result == 0; |
| 49 | |
} |
| 50 | 0 | if (this == NOT_EQUALS) { |
| 51 | 0 | return result != 0; |
| 52 | |
} |
| 53 | 0 | if (this == GREATER_THAN) { |
| 54 | 0 | return result > 0; |
| 55 | |
} |
| 56 | 0 | if (this == GREATER_THAN_EQUAL) { |
| 57 | 0 | return result >= 0; |
| 58 | |
} |
| 59 | 0 | if (this == LESS_THAN) { |
| 60 | 0 | 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 | 0 | 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 | 0 | 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 | |
} |