Coverage Report - org.openpermis.policy.predicate.Argument
 
Classes in this File Line Coverage Branch Coverage Complexity
Argument
82%
23/28
72%
13/18
3.429
 
 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.TimeStamp;
 15  
 
 16  
 
 17  
 /**
 18  
  * An argument value that is evaluated at runtime.
 19  
  * @param <T> argument value type.
 20  
  * @since 0.1.0
 21  
  */
 22  
 public class Argument<T> implements Value<T> {
 23  
 
 24  
         //---- State
 25  
 
 26  
         /**
 27  
          * @since 0.1.0
 28  
          */
 29  
         private String name;
 30  
 
 31  
         /**
 32  
          * @since 0.1.0
 33  
          */
 34  
         private Class<?> type;
 35  
 
 36  
         //---- Constructors
 37  
 
 38  
         /**
 39  
          *
 40  
          */
 41  125
         public Argument (String name, Class<T> type) {
 42  125
                 if (name == null || type == null) {
 43  2
                         throw new IllegalArgumentException("Name or type is null.");
 44  
                 }
 45  123
                 if (name.equals("")) {
 46  1
                         throw new IllegalArgumentException("Name is empty.");
 47  
                 }
 48  122
                 this.name = name;
 49  122
                 this.type = type;
 50  122
         }
 51  
 
 52  
         //---- Methods
 53  
 
 54  
         /**
 55  
          * @since 0.1.0
 56  
          */
 57  
         public Class<?> getType () {
 58  51
                 return this.type;
 59  
         }
 60  
 
 61  
         /**
 62  
          * Returns the argument name.
 63  
          * @return the argument name.
 64  
          * @since 0.1.0
 65  
          */
 66  
         public String getName () {
 67  93
                 return this.name;
 68  
         }
 69  
 
 70  
         //---- Value
 71  
 
 72  
         /**
 73  
          * @since 0.1.0
 74  
          */
 75  
         @SuppressWarnings("unchecked")
 76  
         public T valueOf (TimeStamp timeStamp, Map<String, ?> arguments) {
 77  38
                 final Object result = arguments.get(getName());
 78  38
                 if (result != null) {
 79  38
                         return (T) result;
 80  
                 }
 81  0
                 throw new IllegalStateException("Runtime evaluation error.");
 82  
         }
 83  
         
 84  
         /**
 85  
          * @since 0.3.0
 86  
          */
 87  
         public boolean isMatchable (Map<String, Class<?>> arguments) {
 88  5
                 final Object result = arguments.get(getName());
 89  5
                 if (result != null) {
 90  3
                         return true;
 91  
                 }
 92  2
                 return false;
 93  
         }
 94  
 
 95  
         //---- Object
 96  
 
 97  
         /**
 98  
          * @since 0.1.0
 99  
          */
 100  
         public final boolean equals (Object obj) {
 101  16
                 if (obj == null) {
 102  0
                         return false;
 103  
                 }
 104  16
                 if (obj == this) {
 105  0
                         return true;
 106  
                 }
 107  16
                 if (obj instanceof Argument) {
 108  16
                         final Argument<?> other = (Argument<?>) obj;
 109  16
                         if (getName().equals(other.getName())) {
 110  16
                                 return true;
 111  
                         }
 112  
                 }
 113  0
                 return false;
 114  
         }
 115  
 
 116  
         /**
 117  
          * @since 0.1.0
 118  
          */
 119  
         public int hashCode () {
 120  0
                 return Argument.class.hashCode() * getName().hashCode();
 121  
         }
 122  
 
 123  
 }