Coverage Report - org.openpermis.policy.predicate.SubstringOf
 
Classes in this File Line Coverage Branch Coverage Complexity
SubstringOf
84%
11/13
66%
8/12
2.167
 
 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  
  * Substring of operation.
 19  
  * @since 0.1.0
 20  
  */
 21  
 public class SubstringOf extends AbstractPredicate<Value<?>> {
 22  
         
 23  
         //---- State
 24  
         
 25  
         /**
 26  
          * @since 0.1.0
 27  
          */
 28  
         private static final long serialVersionUID = 5176218191733835322L;
 29  
 
 30  
         //---- Constructors
 31  
 
 32  
         /**
 33  
          * Creates a substring of operation.
 34  
          * @param first the first value.
 35  
          * @param second the second value.
 36  
          * @since 0.1.0
 37  
          */
 38  
         public SubstringOf (Value<?> first, Value<?> second) {
 39  11
                 super(first, second);
 40  11
         }
 41  
 
 42  
         //---- AbstractPredicate
 43  
 
 44  
         /**
 45  
          * @since 0.1.0
 46  
          */
 47  
         public boolean isValid () {
 48  4
                 if (getOperand(0).getType().isAssignableFrom(getOperand(1).getType()) &&
 49  
                         getOperand(1).getType().isAssignableFrom(getOperand(0).getType()) &&
 50  
                         String.class.isAssignableFrom(getOperand(0).getType())
 51  
                 ) {
 52  4
                         return true;
 53  
                 }
 54  0
                 return false;
 55  
         }
 56  
         
 57  
         /**
 58  
          * @since 0.1.0
 59  
          */
 60  
         public boolean matches (TimeStamp timeStamp, Map<String, ?> arguments) {
 61  3
                 if (!isValid()) {
 62  0
                         throw new IllegalStateException("Illegal predicate.");
 63  
                 }
 64  
                 
 65  3
                 final String first = (String) getOperand(0).valueOf(timeStamp, arguments);
 66  3
                 final String second = (String) getOperand(1).valueOf(timeStamp, arguments);
 67  
                 
 68  3
                 return second.contains(first);
 69  
         }
 70  
         
 71  
         /**
 72  
          * @since 0.3.0
 73  
          */
 74  
         public boolean isMatchable (Map<String, Class<?>> arguments) {
 75  4
                 return getOperand(0).isMatchable(arguments) && getOperand(1).isMatchable(arguments);
 76  
         }
 77  
         
 78  
         /**
 79  
          * @since 0.1.0
 80  
          */
 81  
         public boolean comparablePredicate (Predicate predicate) {
 82  1
                 return predicate instanceof SubstringOf;
 83  
         }
 84  
         
 85  
         /**
 86  
          * @since 0.1.0
 87  
          */
 88  
         protected int partHashCode () {
 89  2
                 return SubstringOf.class.hashCode();
 90  
         }
 91  
 
 92  
 }