Coverage Report - org.kuali.rice.krms.framework.engine.expression.ComparisonOperator
 
Classes in this File Line Coverage Branch Coverage Complexity
ComparisonOperator
0%
0/38
0%
0/40
8.25
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.apache.commons.lang.ObjectUtils;
 19  
 import org.kuali.rice.core.api.mo.common.Coded;
 20  
 import org.kuali.rice.krms.api.engine.IncompatibleTypeException;
 21  
 
 22  0
 public enum ComparisonOperator implements Coded {
 23  
 
 24  0
         EQUALS("="),
 25  0
         NOT_EQUALS("!="),
 26  0
         GREATER_THAN(">"),
 27  0
         GREATER_THAN_EQUAL(">="),
 28  0
         LESS_THAN("<"),
 29  0
         LESS_THAN_EQUAL("<=");
 30  
         
 31  
         private final String code;
 32  
         
 33  0
         private ComparisonOperator(String code) {
 34  0
                 this.code = code;
 35  0
         }
 36  
         
 37  
         public String getCode() {
 38  0
                 return code;
 39  
         }
 40  
         
 41  
         public static ComparisonOperator fromCode(String code) {
 42  0
                 if (code == null) {
 43  0
                         return null;
 44  
                 }
 45  0
                 for (ComparisonOperator comparisonOperator : values()) {
 46  0
                         if (comparisonOperator.code.equals(code)) {
 47  0
                                 return comparisonOperator;
 48  
                         }
 49  
                 }
 50  0
                 throw new IllegalArgumentException("Failed to locate the ComparisionOperator with the given code: " + code);
 51  
         }
 52  
         
 53  
         public boolean compare(Object lhs, Object rhs) {
 54  
                 
 55  
                 // TODO this implementation seems largely incomplete, it seems we are need to have some kind of engine
 56  
                 // or utility which can coerce types to possible forms for comparision purposes?  For now, let's verify
 57  
                 // they are of the same type
 58  
                 
 59  0
                 if (lhs != null && rhs != null && !lhs.getClass().equals(rhs.getClass())) {
 60  0
                         throw new IncompatibleTypeException("Could not compare values for operator " + this, lhs, rhs.getClass());
 61  
                 }
 62  
                 
 63  0
                 if (this == EQUALS) {
 64  0
                         return ObjectUtils.equals(lhs, rhs);
 65  0
                 } else if (this == NOT_EQUALS) {
 66  0
                         return ObjectUtils.notEqual(lhs, rhs);
 67  0
                 } else if (lhs == null || rhs == null) {
 68  
                         // any other operation besides equals and not equals will evaluate to false in the case of null
 69  0
                         return false;
 70  
                 }
 71  0
                 if (lhs instanceof Comparable && rhs instanceof Comparable) {
 72  
                         
 73  
                         // TODO not sure what to do about this cast and whether or not it will safe,
 74  
                         // be sure to hit this in unit testing!
 75  
                         
 76  0
                         int result = ((Comparable)lhs).compareTo(rhs);
 77  0
                         if (this == GREATER_THAN) {
 78  0
                                 return result > 0;
 79  
                         }
 80  0
                         if (this == GREATER_THAN_EQUAL) {
 81  0
                                 return result >= 0;
 82  
                         }
 83  0
                         if (this == LESS_THAN) {
 84  0
                                 return result < 0;
 85  
                         }
 86  0
                         if (this == LESS_THAN_EQUAL) {
 87  0
                                 return result <= 0;
 88  
                         }                        
 89  0
                 } else {
 90  0
                         throw new IncompatibleTypeException("Could not compare values, they are not comparable for operator " + this, lhs, rhs.getClass());
 91  
                 }
 92  0
                 throw new IllegalStateException("Invalid operator detected: " + this);
 93  
         }
 94  
         
 95  
 }