View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Operators enumeration for comparing objects.  EQUALS NOT_EQUALS GREATER_THAN GREATER_THAN_EQUAL LESS_THAN LESS_THAN_EQUAL.
30   * Uses registered {@link EngineComparatorExtension} for the given objects or the {@link DefaultComparisonOperator}.
31   *
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  public enum ComparisonOperator implements Coded {
35  
36      /**
37       * use this flag with the static factory to get a {@link ComparisonOperator} EQUALS
38       */
39  	EQUALS("="),
40  
41      /**
42       * use this flag with the static factory to get a {@link ComparisonOperator} NOT_EQUALS
43       */
44  	NOT_EQUALS("!="),
45  
46      /**
47       * use this flag with the static factory to get a {@link ComparisonOperator} GREATER_THAN
48       */
49  	GREATER_THAN(">"),
50  
51      /**
52       * use this flag with the static factory to get a {@link ComparisonOperator} GREATER_THAN_EQUAL
53       */
54  	GREATER_THAN_EQUAL(">="),
55  
56      /**
57       * use this flag with the static factory to get a {@link ComparisonOperator} LESS_THAN
58       */
59  	LESS_THAN("<"),
60  
61      /**
62       * use this flag with the static factory to get a {@link ComparisonOperator} LESS_THAN_EQUAL
63       */
64  	LESS_THAN_EQUAL("<="),
65  
66      /**
67       * use this flag with the static factory to get a {@link ComparisonOperator} EXISTS
68       */
69      EXISTS("!=null"),
70  
71      /**
72       * use this flag with the static factory to get a {@link ComparisonOperator} DOES_NOT_EXISTS
73       */
74      DOES_NOT_EXIST("=null");
75  
76  	private final String code;
77  
78      ComparisonOperatorService comparisonOperatorService;
79  
80      /**
81       * Create a ComparisonOperator from the given code
82       * @param code code the ComparisonOperator should be of.
83       */
84  	private ComparisonOperator(String code) {
85  		this.code = code;
86  	}
87  
88      /**
89       *
90       * @return code representing the type of operator
91       */
92      @Override
93  	public String getCode() {
94  		return code;
95  	}
96  
97      /**
98       * Create a ComparisonOperator from the given code
99       * @param code for type of ComparisonOperator to create
100      * @return a ComparisonOperator created with the given code.
101      * @throws IllegalArgumentException if the given code does not exist
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      * Compare the given objects
117      * @param lhs left hand side object
118      * @param rhs right hand side object
119      * @return boolean value of comparison results based on the type of operator.
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      * Operator codes, unmodifiable Collection
150      */
151     public static final Collection<String> OPERATOR_CODES;
152 
153     /**
154      * Operator names, unmodifiable Collection
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      * @return type code
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 }