Coverage Report - org.openpermis.basic.TimePeriodConstraint
 
Classes in this File Line Coverage Branch Coverage Complexity
TimePeriodConstraint
76%
40/52
66%
44/66
6.222
 
 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.basic;
 11  
 
 12  
 import static org.openpermis.policy.bean.basic.BasicUtilities.multiHashCode;
 13  
 
 14  
 import org.joda.time.Period;
 15  
 
 16  
 
 17  
 /**
 18  
  * A time period constraint containing one absolute and three relative time constraints.
 19  
  * 
 20  
  * <p>
 21  
  * Absolute Period:<br/>
 22  
  * The absolute valid period.
 23  
  * The actual validity period is the intersection of the policy absolute validity period and the 
 24  
  * attribute certificate validity period.
 25  
  * </p>
 26  
  * <p>
 27  
  * Minimum Valid From:<br/>
 28  
  * The minimum duration that an attribute certificate must have been valid relative to the 
 29  
  * evaluation time.
 30  
  * </p>
 31  
  * <p>
 32  
  * Minimum Valid Up To:<br/>
 33  
  * The minimum duration that an attribute certificate must be valid from relative to the 
 34  
  * evaluation time.
 35  
  * </p>
 36  
  * <p>
 37  
  * Maximum Valid Up To:<br/>
 38  
  * The maximum duration that an attribute certificate should be valid from the evaluation time.
 39  
  * </p>
 40  
  * @since 0.3.0
 41  
  */
 42  
 public final class TimePeriodConstraint {
 43  
 
 44  
         //---- Static
 45  
         
 46  
         /**
 47  
          * A constraint that do not constrain (empty constraint).
 48  
          */
 49  1
         public static final TimePeriodConstraint UNCONSTRAINED = 
 50  
                 new TimePeriodConstraint(null, null, null, null);
 51  
         
 52  
         /**
 53  
          * @since 0.3.0
 54  
          */
 55  
         private static boolean containsNegativePositiveParts (Period duration) {
 56  223
                 if (duration == null) {
 57  207
                         return false;
 58  
                 }
 59  16
                 final int[] values = duration.getValues();
 60  144
                 for (int i = 0; i < values.length; i++) {
 61  128
                         if (values[i] < 0) {
 62  0
                                 return true;
 63  
                         }
 64  
                 }
 65  16
                 return false;
 66  
         }
 67  
         
 68  
         /**
 69  
          * @since 0.3.0
 70  
          */
 71  
         private static boolean isGreater (Period left, Period right) {
 72  13
                 final int size = left.getValues().length;
 73  13
                 for (int i = 0; i < size; i++) {
 74  13
                         if (left.getValue(i) != right.getValue(i)) {
 75  13
                                 return left.getValue(i) > right.getValue(i);
 76  
                         }
 77  
                 }
 78  0
                 return false;
 79  
         }
 80  
         
 81  
         //---- State
 82  
         
 83  
         private TimePeriod absolutePeriod;
 84  
         
 85  
         private Period minimumValidFrom;
 86  
         
 87  
         private Period minimumValidUpTo;
 88  
         
 89  
         private Period maximumValidUpTo;
 90  
         
 91  
         //---- Constructors
 92  
         
 93  
         /**
 94  
          * Creates a time period constraint. Every argument may be null.
 95  
          * @param absolutePeriod a {@link TimePeriod}.
 96  
          * @param minimumValidFrom a {@link Period}.
 97  
          * @param minimumValidUpTo a {@link Period}.
 98  
          * @param maximumValidUpTo a {@link Period}.
 99  
          */
 100  
         public TimePeriodConstraint (
 101  
                 TimePeriod absolutePeriod, 
 102  
                 Period minimumValidFrom, 
 103  
                 Period minimumValidUpTo, 
 104  
                 Period maximumValidUpTo
 105  223
         ) {
 106  
                 // All relative durations must be positive.
 107  223
                 if (containsNegativePositiveParts(maximumValidUpTo) &&
 108  
                         containsNegativePositiveParts(minimumValidFrom) &&
 109  
                         containsNegativePositiveParts(minimumValidUpTo)
 110  
                 
 111  
                 ) {
 112  0
                         throw new IllegalArgumentException("Only positive durations are allowed.");
 113  
                 }
 114  
                 
 115  
                 // MinimumValidUpTo must be smaller than maximumValidUpTo.
 116  223
                 if (minimumValidUpTo != null &&
 117  
                         maximumValidUpTo != null &&
 118  
                         isGreater(minimumValidUpTo, maximumValidUpTo)
 119  
                 ) {
 120  0
                         throw new IllegalArgumentException("Minimum valid up to is greater than maximum.");
 121  
                 }
 122  
                 
 123  223
                 this.absolutePeriod = absolutePeriod;
 124  223
                 this.minimumValidFrom = minimumValidFrom;
 125  223
                 this.minimumValidUpTo = minimumValidUpTo;
 126  223
                 this.maximumValidUpTo = maximumValidUpTo;
 127  223
                 if (absolutePeriod == null) {
 128  210
                         this.absolutePeriod = TimePeriod.INFINITE;
 129  
                 }
 130  223
         }
 131  
         
 132  
         //---- Methods
 133  
         
 134  
         /**
 135  
          * Returns the absolute valid time period.
 136  
          * @return the absolute valid time period.
 137  
          */
 138  
         public TimePeriod getAbsolutePeriod () {
 139  24
                 return this.absolutePeriod;
 140  
         }
 141  
         
 142  
         /**
 143  
          * Returns the minimum valid from period.
 144  
          * @return the minimum valid from period.
 145  
          */
 146  
         public Period getMinimumValidFrom () {
 147  16
                 return this.minimumValidFrom;
 148  
         }
 149  
         
 150  
         /**
 151  
          * Returns the minimum valid up to period.
 152  
          * @return the minimum valid up to period.
 153  
          */
 154  
         public Period getMinimumValidUpTo () {
 155  29
                 return this.minimumValidUpTo;
 156  
         }
 157  
         
 158  
         /**
 159  
          * Returns the maximum valid up to period.
 160  
          * @return the maximum valid up to period.
 161  
          */
 162  
         public Period getMaximumValidUpTo () {
 163  14
                 return this.maximumValidUpTo;
 164  
         }
 165  
         
 166  
         //---- Object
 167  
         
 168  
         /**
 169  
          * @since 0.3.0
 170  
          */
 171  
         public boolean equals (Object object) {
 172  
                 
 173  28
                 if (object == null) {
 174  0
                         return false;
 175  
                 }
 176  28
                 if (this == object) {
 177  7
                         return true;
 178  
                 }
 179  
 
 180  21
                 if (object instanceof TimePeriodConstraint) {
 181  21
                         final TimePeriodConstraint other = (TimePeriodConstraint) object;
 182  
 
 183  
                         // Absolute Period.
 184  21
                         if (!this.absolutePeriod.equals(other.absolutePeriod)) {
 185  1
                                 return false;
 186  
                         }
 187  
                         
 188  
                         // Maximum valid up to.
 189  20
                         if (this.maximumValidUpTo != null &&
 190  
                                 !this.maximumValidUpTo.equals(other.maximumValidUpTo)
 191  
                         ) {
 192  0
                                 return false;
 193  20
                         } else if (this.maximumValidUpTo == null && other.maximumValidUpTo != null) {
 194  0
                                 return false;
 195  
                         }
 196  
                         
 197  
                         // Minimum valid from.
 198  20
                         if (this.minimumValidFrom != null &&
 199  
                                 !this.minimumValidFrom.equals(other.minimumValidFrom)
 200  
                         ) {
 201  0
                                 return false;
 202  20
                         } else if (this.minimumValidFrom == null && other.minimumValidFrom != null) {
 203  0
                                 return false;
 204  
                         }
 205  
                         
 206  
                         // Minimum valid up to.
 207  20
                         if (this.minimumValidUpTo != null &&
 208  
                                 !this.minimumValidUpTo.equals(other.minimumValidUpTo)
 209  
                         ) {
 210  0
                                 return false;
 211  20
                         } else if (this.minimumValidUpTo == null && other.minimumValidUpTo != null) {
 212  0
                                 return false;
 213  
                         }
 214  20
                         return true;
 215  
                 }
 216  0
                 return false;
 217  
         }
 218  
 
 219  
         /**
 220  
          * @since 0.3.0
 221  
          */
 222  
         public int hashCode () {
 223  12
                 return multiHashCode(
 224  
                         this.absolutePeriod != null ? this.absolutePeriod.hashCode() : 0,
 225  
                         this.maximumValidUpTo != null ? this.maximumValidUpTo.hashCode() : 0,
 226  
                         this.minimumValidFrom != null ? this.minimumValidFrom.hashCode() : 0,
 227  
                         this.minimumValidUpTo != null ? this.minimumValidUpTo.hashCode() : 0
 228  
                 );
 229  
         }
 230  
         
 231  
 }