Coverage Report - org.openpermis.policy.predicate.ValueRelationalPredicate
 
Classes in this File Line Coverage Branch Coverage Complexity
ValueRelationalPredicate
86%
19/22
86%
26/30
3.571
ValueRelationalPredicate$1
100%
1/1
N/A
3.571
ValueRelationalPredicate$Relation
100%
6/6
N/A
3.571
 
 1  
 /*
 2  
  * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport
 3  
  *                     (http://www.vbs.admin.ch)
 4  
  * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch)
 5  
  * All rights reserved.
 6  
  *
 7  
  * Licensed under the Open Permis License which accompanies this distribution,
 8  
  * and is available at http://www.openpermis.org/BSDlicenceKent.txt
 9  
  */
 10  
 package org.openpermis.policy.predicate;
 11  
 
 12  
 import java.util.Map;
 13  
 
 14  
 import org.openpermis.policy.Predicate;
 15  
 import org.openpermis.policy.TimeStamp;
 16  
 
 17  
 /**
 18  
  * A relational predicate.
 19  
  * @since 0.1.0
 20  
  */
 21  
 public class ValueRelationalPredicate extends AbstractPredicate<Value<?>> {
 22  
         
 23  
         //---- Static
 24  
         
 25  
         /**
 26  
          * @since 0.1.0
 27  
          */
 28  8
         public static enum Relation {
 29  
                 /**
 30  
                  * @since 0.1.0
 31  
                  */
 32  1
                 GreaterThan,
 33  
                 
 34  
                 /**
 35  
                  * @since 0.1.0
 36  
                  */
 37  1
                 GreaterEqual,
 38  
                 
 39  
                 /**
 40  
                  * @since 0.1.0
 41  
                  */
 42  1
                 LessThan,
 43  
                 
 44  
                 /**
 45  
                  * @since 0.1.0
 46  
                  */
 47  1
                 LessEqual,
 48  
                 
 49  
                 /**
 50  
                  * @since 0.1.0
 51  
                  */
 52  1
                 Equal
 53  
         }
 54  
         
 55  
         /**
 56  
          * @since 0.1.0
 57  
          */
 58  
         private static final long serialVersionUID = 5176218191733835322L;
 59  
         
 60  
         //---- State
 61  
         
 62  
         /**
 63  
          * @since 0.1.0
 64  
          */
 65  
         private Relation relation;
 66  
 
 67  
         //---- Constructors
 68  
 
 69  
         /**
 70  
          * Creates a relational predicate.
 71  
          * @param relation a {@link Relation}.
 72  
          * @param first the first comparable {@link Value}.
 73  
          * @param second the second comparable {@link Value}.
 74  
          * @since 0.1.0
 75  
          */
 76  
         public ValueRelationalPredicate (Relation relation, Value<?> first, Value<?> second) {
 77  146
                 super(first, second);
 78  146
                 this.relation = relation;
 79  146
         }
 80  
 
 81  
         //---- Methods
 82  
         
 83  
         /**
 84  
          * Returns the relation of this predicate.
 85  
          * @return the relation of this predicate.
 86  
          * @since 0.1.0
 87  
          */
 88  
         public Relation getRelation () {
 89  114
                 return this.relation;
 90  
         }
 91  
         
 92  
         //---- AbstractPredicate
 93  
         
 94  
         /**
 95  
          * @since 0.1.0
 96  
          */
 97  
         public boolean isValid () {
 98  141
                 return
 99  
                         getOperand(0).getType().isAssignableFrom(getOperand(1).getType()) &&
 100  
                         getOperand(1).getType().isAssignableFrom(getOperand(0).getType()) &&
 101  
                         Comparable.class.isAssignableFrom(getOperand(0).getType());
 102  
         }
 103  
 
 104  
         /**
 105  
          * @since 0.1.0
 106  
          */
 107  
         @SuppressWarnings("unchecked")
 108  
         public boolean matches (TimeStamp timeStamp, Map<String, ?> arguments) {
 109  26
                 if (!isValid()) {
 110  1
                         throw new IllegalStateException("Illegal predicate.");
 111  
                 }
 112  
                 
 113  25
                 final Comparable first = (Comparable) getOperand(0).valueOf(timeStamp, arguments);
 114  25
                 final Comparable second = (Comparable) getOperand(1).valueOf(timeStamp, arguments);
 115  
                 
 116  25
                 switch (getRelation()) {
 117  
                         case GreaterThan:
 118  13
                                 return first.compareTo(second) > 0;
 119  
                         case GreaterEqual:
 120  3
                                 return first.compareTo(second) >= 0;
 121  
                         case LessThan:
 122  3
                                 return first.compareTo(second) < 0;
 123  
                         case LessEqual:
 124  3
                                 return first.compareTo(second) <= 0;
 125  
                         case Equal:
 126  3
                                 return first.compareTo(second) == 0;
 127  
                         default:
 128  0
                                 throw new IllegalStateException("Should never happen.");
 129  
                 }        
 130  
         }
 131  
 
 132  
         /**
 133  
          * @since 0.3.0
 134  
          */
 135  
         public boolean isMatchable (Map<String, Class<?>> arguments) {
 136  9
                 return getOperand(0).isMatchable(arguments) && getOperand(1).isMatchable(arguments);
 137  
         }
 138  
         
 139  
         /**
 140  
          * @since 0.1.0
 141  
          */
 142  
         public boolean comparablePredicate (Predicate predicate) {
 143  40
                 if (!(predicate instanceof ValueRelationalPredicate)) {
 144  0
                         return false;
 145  
                 }
 146  40
                 final ValueRelationalPredicate other = (ValueRelationalPredicate) predicate;
 147  40
                 return getRelation().equals(other.getRelation());
 148  
         }
 149  
         
 150  
         /**
 151  
          * @since 0.1.0
 152  
          */
 153  
         protected int partHashCode () {
 154  0
                 return ValueRelationalPredicate.class.hashCode();
 155  
         }
 156  
         
 157  
 }