View Javadoc

1   /**
2    * Copyright 2005-2012 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  	private final String code;
66  
67      ComparisonOperatorService comparisonOperatorService;
68  
69      /**
70       * Create a ComparisonOperator from the given code
71       * @param code code the ComparisonOperator should be of.
72       */
73  	private ComparisonOperator(String code) {
74  		this.code = code;
75  	}
76  
77      /**
78       *
79       * @return code representing the type of operator
80       */
81      @Override
82  	public String getCode() {
83  		return code;
84  	}
85  
86      /**
87       * Create a ComparisonOperator from the given code
88       * @param code for type of ComparisonOperator to create
89       * @return a ComparisonOperator created with the given code.
90       * @throws IllegalArgumentException if the given code does not exist
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      * Compare the given objects
106      * @param lhs left hand side object
107      * @param rhs right hand side object
108      * @return boolean value of comparison results based on the type of operator.
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      * Operator codes, unmodifiable Collection
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      * Operator names, unmodifiable Collection
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      * @return type code
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 }