View Javadoc

1   /**
2    * Copyright 2005-2013 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.Arrays;
24  import java.util.Collection;
25  import java.util.Collections;
26  
27  /**
28   * Operators enumeration for comparing objects.  EQUALS NOT_EQUALS GREATER_THAN GREATER_THAN_EQUAL LESS_THAN LESS_THAN_EQUAL.
29   * Uses registered {@link EngineComparatorExtension} for the given objects or the {@link DefaultComparisonOperator}.
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public enum ComparisonOperator implements Coded {
34  
35      /**
36       * use this flag with the static factory to get a {@link ComparisonOperator} EQUALS
37       */
38  	EQUALS("="),
39  
40      /**
41       * use this flag with the static factory to get a {@link ComparisonOperator} NOT_EQUALS
42       */
43  	NOT_EQUALS("!="),
44  
45      /**
46       * use this flag with the static factory to get a {@link ComparisonOperator} GREATER_THAN
47       */
48  	GREATER_THAN(">"),
49  
50      /**
51       * use this flag with the static factory to get a {@link ComparisonOperator} GREATER_THAN_EQUAL
52       */
53  	GREATER_THAN_EQUAL(">="),
54  
55      /**
56       * use this flag with the static factory to get a {@link ComparisonOperator} LESS_THAN
57       */
58  	LESS_THAN("<"),
59  
60      /**
61       * use this flag with the static factory to get a {@link ComparisonOperator} LESS_THAN_EQUAL
62       */
63  	LESS_THAN_EQUAL("<="),
64  
65      /**
66       * use this flag with the static factory to get a {@link ComparisonOperator} EXISTS
67       */
68      EXISTS("!=null"),
69  
70      /**
71       * use this flag with the static factory to get a {@link ComparisonOperator} DOES_NOT_EXISTS
72       */
73      DOES_NOT_EXIST("=null");
74  
75  	private final String code;
76  
77      ComparisonOperatorService comparisonOperatorService;
78  
79      /**
80       * Create a ComparisonOperator from the given code
81       * @param code code the ComparisonOperator should be of.
82       */
83  	private ComparisonOperator(String code) {
84  		this.code = code;
85  	}
86  
87      /**
88       *
89       * @return code representing the type of operator
90       */
91      @Override
92  	public String getCode() {
93  		return code;
94  	}
95  
96      /**
97       * Create a ComparisonOperator from the given code
98       * @param code for type of ComparisonOperator to create
99       * @return a ComparisonOperator created with the given code.
100      * @throws IllegalArgumentException if the given code does not exist
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      * Compare the given objects
116      * @param lhs left hand side object
117      * @param rhs right hand side object
118      * @return boolean value of comparison results based on the type of operator.
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      * Operator codes, unmodifiable Collection
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      * Operator names, unmodifiable Collection
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      * @return type code
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 }