Coverage Report - org.openpermis.policy.predicate.ValueSet
 
Classes in this File Line Coverage Branch Coverage Complexity
ValueSet
76%
32/42
71%
20/28
3.875
 
 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 static org.openpermis.policy.bean.basic.BasicUtilities.equalLists;
 13  
 
 14  
 import java.util.Arrays;
 15  
 import java.util.List;
 16  
 import java.util.Map;
 17  
 
 18  
 import org.openpermis.policy.TimeStamp;
 19  
 
 20  
 /**
 21  
  * Set of values.
 22  
  * @since 0.3.0
 23  
  */
 24  
 public class ValueSet  {
 25  
 
 26  
         //---- Static
 27  
 
 28  
         /**
 29  
          * @since 0.3.0
 30  
          */
 31  
         private static final long serialVersionUID = -120774600735285360L;
 32  
 
 33  
 
 34  
         //---- State
 35  
 
 36  
         /**
 37  
          * @since 0.3.0
 38  
          */
 39  
         private final List<Value<?>> values;
 40  
 
 41  
 
 42  
         //---- Constructors
 43  
 
 44  
         /**
 45  
          * Creates a set, at least one argument is needed.
 46  
          * @param values {@link Value}s.
 47  
          * @since 0.3.0
 48  
          */
 49  2
         public ValueSet (Value<?>... values) {
 50  2
                 if (values.length < 1) {
 51  0
                         throw new IllegalArgumentException("Set operator needs at least one argument.");
 52  
                 }
 53  2
                 this.values = Arrays.asList(values);
 54  2
         }
 55  
 
 56  
         /**
 57  
          * Creates a set, at least one argument is needed.
 58  
          * @param values {@link Value}s.
 59  
          * @since 0.3.0
 60  
          */
 61  80
         public ValueSet (List<Value<?>> values) {
 62  80
                 if (values.size() < 1) {
 63  0
                         throw new IllegalArgumentException("Set operator needs at least one argument.");
 64  
                 }
 65  80
                 this.values = values;
 66  80
         }
 67  
 
 68  
 
 69  
         //---- Methods
 70  
 
 71  
         /**
 72  
          * Get current values.
 73  
          * new
 74  
          */
 75  
         public List<Value<?>> getValues () {
 76  78
                 return this.values;
 77  
         }
 78  
 
 79  
         /**
 80  
          * @since 0.3.0
 81  
          */
 82  
         @SuppressWarnings("unchecked")
 83  
         private boolean containsElement (
 84  
                 Value<?> value, TimeStamp timeStamp, Map<String, ?> arguments
 85  
         ) {
 86  
                 Object op1;
 87  
                 Object op2;
 88  
                 Comparable op1Value;
 89  
                 Comparable op2Value;
 90  
 
 91  18
                 op1 = value.valueOf(timeStamp, arguments);
 92  18
                 op1Value = (Comparable) op1;
 93  41
                 for (int i = 0; i < this.values.size(); i++) {
 94  33
                         op2 = this.values.get(i).valueOf(timeStamp, arguments);
 95  
                         // only compare compatible types
 96  33
                         if (op1.getClass() == op2.getClass()) {
 97  28
                                 op2Value = (Comparable) op2;
 98  28
                                 if (op1Value.compareTo(op2Value) == 0) {
 99  10
                                         return true;
 100  
                                 }
 101  
                         }
 102  
                 }
 103  8
                 return false;
 104  
         }
 105  
 
 106  
         /**
 107  
          * Checks if any element of another set is contained.
 108  
          * @since 0.3.0
 109  
          */
 110  
         public boolean containsAnyElementOfSet (
 111  
                 ValueSet otherSet, TimeStamp timeStamp, Map<String, ?> arguments) {
 112  6
                 for (int i = 0; i < otherSet.getValues().size(); i++) {
 113  5
                         if (this.containsElement(otherSet.getValues().get(i), timeStamp, arguments)) {
 114  2
                                 return true;
 115  
                         }
 116  
                 }
 117  1
                 return false;
 118  
         }
 119  
 
 120  
         /**
 121  
          * Checks if all elements of another set are contained.
 122  
          * @since 0.3.0
 123  
          */
 124  
         public boolean containsAllElementOfSet (
 125  
                 ValueSet otherSet, TimeStamp timeStamp, Map<String, ?> arguments) {
 126  18
                 for (int i = 0; i < otherSet.getValues().size(); i++) {
 127  13
                         if (!this.containsElement(otherSet.getValues().get(i), timeStamp, arguments)) {
 128  5
                                 return false;
 129  
                         }
 130  
                 }
 131  5
                 return true;
 132  
         }
 133  
 
 134  
         //---- Object
 135  
 
 136  
         /**
 137  
          * @since 0.3.0
 138  
          */
 139  
         public final boolean equals (Object obj) {
 140  12
                 if (obj == null) {
 141  0
                         return false;
 142  
                 }
 143  12
                 if (obj == this) {
 144  0
                         return true;
 145  
                 }
 146  12
                 if (obj instanceof ValueSet) {
 147  12
                         final ValueSet other = (ValueSet) obj;
 148  12
                         if (!equalLists(getValues(), other.getValues())) {
 149  0
                                 return false;
 150  
                         }
 151  12
                         return true;
 152  
                 }
 153  0
                 return false;
 154  
         }
 155  
 
 156  
         /**
 157  
          * @since 0.3.0
 158  
          */
 159  
         public final int hashCode () {
 160  0
                 int hashCode = ValueSet.class.hashCode();
 161  0
                 for (Value<?> value : getValues()) {
 162  0
                         hashCode *= value.hashCode();
 163  
                 }
 164  0
                 return hashCode;
 165  
         }
 166  
 
 167  
 }