Coverage Report - org.openpermis.policy.predicate.AbstractPredicate
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractPredicate
68%
17/25
55%
10/18
2.556
 
 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.Collections;
 16  
 import java.util.List;
 17  
 
 18  
 import org.openpermis.policy.PartProblemReporter;
 19  
 import org.openpermis.policy.Predicate;
 20  
 
 21  
 /**
 22  
  * A node in the predicate tree consisting in a predicate having two generic operands.
 23  
  * 
 24  
  * @param <T> type of the operands.
 25  
  * @since 0.1.0
 26  
  */
 27  
 public abstract class AbstractPredicate<T> implements Predicate {
 28  
         
 29  
         //---- Static
 30  
         
 31  
         /**
 32  
          * @since 0.1.0
 33  
          */
 34  
         private static final long serialVersionUID = 2447669570741402163L;
 35  
         
 36  
         //---- State
 37  
         
 38  
         /**
 39  
          * @since 0.1.0
 40  
          */
 41  
         private final List<T> operandList;
 42  
 
 43  
         //---- Constructors
 44  
 
 45  
         /**
 46  
          * Creates an abstract predicate with operands.
 47  
          * @param operands an array of operands.
 48  
          * @since 0.1.0
 49  
          */
 50  319
         public AbstractPredicate (T... operands) {
 51  1016
                 for (T operand : operands) {
 52  697
                         if (operand == null) {
 53  0
                                 throw new IllegalArgumentException("Operand is null.");
 54  
                         }
 55  
                 }
 56  319
                 this.operandList = Arrays.asList(operands);
 57  319
         }
 58  
         
 59  
         //---- Methods
 60  
         
 61  
         /**
 62  
          * Returns the number of operands.
 63  
          * @return the number of operands.
 64  
          * @since 0.1.0
 65  
          */
 66  
         public int getOperandCount () {
 67  64
                 return this.operandList.size();
 68  
         }
 69  
         
 70  
         /**
 71  
          * Returns the n-th operand of this predicate.
 72  
          * @param index the position of the desired operand.
 73  
          * @return the n-th operand.
 74  
          * @since 0.1.0
 75  
          */
 76  
         public T getOperand (int index) {
 77  957
                 return this.operandList.get(index);
 78  
         }
 79  
         
 80  
         /**
 81  
          * Returns a list of operands.
 82  
          * @return a list of operands
 83  
          * @since 0.1.0
 84  
          */
 85  
         public List<T> getOperands () {
 86  127
                 return Collections.unmodifiableList(this.operandList);
 87  
         }
 88  
         
 89  
         /**
 90  
          * Compares the type of predicate with this.
 91  
          * @param predicate a {@link Predicate}.
 92  
          * @return true if this is comparable to predicate.
 93  
          * @since 0.1.0
 94  
          */
 95  
         protected abstract boolean comparablePredicate (Predicate predicate);
 96  
         
 97  
         /**
 98  
          * Returns the hash code of this part.
 99  
          * @return hash code of this part. 
 100  
          * @since 0.1.0
 101  
          */
 102  
         protected abstract int partHashCode ();
 103  
         
 104  
         //---- Part
 105  
         
 106  
         /**
 107  
          * @since 0.1.0
 108  
          */
 109  
         public final boolean isValid (PartProblemReporter reporter) {
 110  3
                 return isValid();
 111  
         }
 112  
         
 113  
         //---- Object
 114  
         
 115  
         /**
 116  
          * @since 0.1.0
 117  
          */
 118  
         public final boolean equals (Object obj) {
 119  46
                 if (obj == null) {
 120  0
                         return false;
 121  
                 }
 122  46
                 if (obj == this) {
 123  11
                         return true;
 124  
                 }
 125  35
                 if (obj instanceof AbstractPredicate) {
 126  35
                         final AbstractPredicate<?> other = (AbstractPredicate<?>) obj;
 127  35
                         if (!equalLists(getOperands(), other.getOperands())) {
 128  0
                                 return false;
 129  
                         }
 130  35
                         if (comparablePredicate(other) &&
 131  
                                 other.comparablePredicate(this)
 132  
                         ) {
 133  35
                                 return true;
 134  
                         }
 135  
                 }
 136  0
                 return false;
 137  
         }
 138  
 
 139  
         /**
 140  
          * @since 0.1.0
 141  
          */
 142  
         public final int hashCode () {
 143  0
                 int hashCode = partHashCode();
 144  0
                 for (T operand : getOperands()) {
 145  0
                         hashCode *= operand.hashCode();
 146  
                 }
 147  0
                 return hashCode;
 148  
         }
 149  
         
 150  
 }