Coverage Report - org.kuali.rice.krms.framework.engine.ComparisonOperator
 
Classes in this File Line Coverage Branch Coverage Complexity
ComparisonOperator
61%
21/34
30%
9/30
4.167
ComparisonOperator$Adapter
0%
0/2
N/A
4.167
 
 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;
 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  1
 public enum ComparisonOperator implements Coded {
 26  
 
 27  1
         EQUALS ("="),
 28  1
         NOT_EQUALS ("!="),
 29  1
         GREATER_THAN (">"),
 30  1
         GREATER_THAN_EQUAL (">="),
 31  1
         LESS_THAN ("<"),
 32  1
         LESS_THAN_EQUAL ("<=");
 33  
         
 34  
     private final String code;
 35  
     
 36  6
     private ComparisonOperator(String code){
 37  6
         this.code = code;
 38  6
     }
 39  
     
 40  
     @Override
 41  
     public String getCode(){
 42  6
         return code;
 43  
     }
 44  
     
 45  
         public <T> boolean compare(Comparable<T> lhs, T rhs) {
 46  40
                 int result = lhs.compareTo(rhs);
 47  40
                 if (this == EQUALS) {
 48  0
                         return result == 0;
 49  
                 }
 50  40
                 if (this == NOT_EQUALS) {
 51  0
                         return result != 0;
 52  
                 }
 53  40
                 if (this == GREATER_THAN) {
 54  39
                         return result > 0;
 55  
                 }
 56  1
                 if (this == GREATER_THAN_EQUAL) {
 57  0
                         return result >= 0;
 58  
                 }
 59  1
                 if (this == LESS_THAN) {
 60  1
                         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  1
     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  1
     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  
 }