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