Coverage Report - org.openpermis.policy.ParameterList
 
Classes in this File Line Coverage Branch Coverage Complexity
ParameterList
92%
52/56
85%
36/42
3.263
ParameterList$Parameter
87%
14/16
70%
7/10
3.263
 
 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;
 11  
 
 12  
 import java.io.Serializable;
 13  
 import java.util.ArrayList;
 14  
 import java.util.HashSet;
 15  
 import java.util.Iterator;
 16  
 import java.util.List;
 17  
 import java.util.Set;
 18  
 
 19  
 
 20  
 /**
 21  
  * A list of parameters.
 22  
  * @since 0.1.0
 23  
  */
 24  
 public final class ParameterList 
 25  
         implements Iterable<ParameterList.Parameter>, 
 26  
         Serializable
 27  
 {
 28  
         
 29  
         //---- Static
 30  
         
 31  
         private static final long serialVersionUID = 7726102676246748249L;
 32  
 
 33  
         /**
 34  
          * An empty parameter list.
 35  
          * @return a {@link ParameterList} without parameters.
 36  
          * @since 0.1.0
 37  
          */
 38  
         public static final ParameterList empty () {
 39  181
                 return new ParameterList();
 40  
         }
 41  
 
 42  
         //---- State
 43  
 
 44  
         /**
 45  
          * @since 0.1.0
 46  
          */
 47  
         private List<Parameter> parameters;
 48  
 
 49  
         //---- Constructors
 50  
 
 51  
         /**
 52  
          * Creates an empty parameter list.
 53  
          * @since 0.1.0
 54  
          */
 55  3685
         public ParameterList () {
 56  3685
                 this.parameters = new ArrayList<Parameter>();
 57  3685
         }
 58  
 
 59  
         //---- Methods
 60  
 
 61  
         /**
 62  
          * Adds a parameter with name and type to the end of the list.
 63  
          * @param name a name.
 64  
          * @param type a type.
 65  
          * @since 0.1.0
 66  
          */
 67  
         public void add (String name, Class<?> type) {
 68  624
                 if (name == null || type == null) {
 69  3
                         throw new IllegalArgumentException("Name or type is null.");
 70  
                 }
 71  621
                 this.parameters.add(new Parameter(name, type));
 72  621
         }
 73  
 
 74  
         /**
 75  
          * Returns true if this parameter list is valid.
 76  
          * @return true if this parameter list is valid.
 77  
          * @since 0.1.0
 78  
          */
 79  
         public boolean isValid () {
 80  
                 // Check if parameter names are unique.
 81  3
                 final Set<String> names = new HashSet<String>();
 82  3
                 for (Parameter parameter : this) {
 83  5
                         if (names.contains(parameter.getName())) {
 84  1
                                 return false;
 85  
                         }
 86  4
                         names.add(parameter.getName());
 87  
                 }
 88  2
                 return true;
 89  
         }
 90  
 
 91  
         /**
 92  
          * Returns the name of parameter at position.
 93  
          * @param position of the parameter.
 94  
          * @return name the name.
 95  
          * @since 0.1.0
 96  
          */
 97  
         public String getName (int position) {
 98  3
                 if (position >= this.parameters.size()) {
 99  1
                         throw new IllegalArgumentException("Position out of bounds.");
 100  
                 }
 101  2
                 return this.parameters.get(position).getName();
 102  
         }
 103  
 
 104  
         /**
 105  
          * Returns true if a parameter contained in this.
 106  
          * @param name a name.
 107  
          * @return true if a parameter contained in this.
 108  
          * @since 0.1.0
 109  
          */
 110  
         public boolean contains (String name) {
 111  100
                 for (Parameter parameter : this.parameters) {
 112  75
                         if (parameter.getName().equals(name)) {
 113  1
                                 return true;
 114  
                         }
 115  
                 }
 116  99
                 return false;
 117  
         }
 118  
 
 119  
         /**
 120  
          * Returns the type of parameter at position.
 121  
          * @param position of the parameter.
 122  
          * @return type the type.
 123  
          * @since 0.1.0
 124  
          */
 125  
         public Class<?> getType (int position) {
 126  10
                 if (position >= this.parameters.size()) {
 127  1
                         throw new IllegalArgumentException("Position out of bounds.");
 128  
                 }
 129  9
                 return this.parameters.get(position).getType();
 130  
         }
 131  
 
 132  
         /**
 133  
          * Returns the name of parameter at position.
 134  
          * @param name of the parameter.
 135  
          * @return type the type.
 136  
          * @since 0.1.0
 137  
          */
 138  
         public Class<?> getType (String name) {
 139  3
                 for (Parameter parameter : this.parameters) {
 140  2
                         if (parameter.getName().equals(name)) {
 141  2
                                 return parameter.getType();
 142  
                         }
 143  
                 }
 144  1
                 throw new IllegalArgumentException("No such parameter name.");
 145  
         }
 146  
 
 147  
         /**
 148  
          * Returns the number of parameters.
 149  
          * @return the number of parameters.
 150  
          * @since 0.1.0
 151  
          */
 152  
         public int getParameterCount () {
 153  1831
                 return this.parameters.size();
 154  
         }
 155  
 
 156  
         /**
 157  
          * Returns true if arguments matches parameters.
 158  
          * @param arguments a list of arguments.
 159  
          * @return true if arguments matches parameters.
 160  
          * @since 0.1.0
 161  
          */
 162  
         public boolean matchesArguments (List<?> arguments) {
 163  35
                 if (getParameterCount() != arguments.size()) {
 164  0
                         return false;
 165  
                 }
 166  40
                 for (int i = 0; i < getParameterCount(); i++) {
 167  9
                         if (!arguments.get(i).getClass().equals(getType(i))) {
 168  4
                                 return false;
 169  
                         }
 170  
                 }
 171  31
                 return true;
 172  
         }
 173  
 
 174  
         /**
 175  
          * @since 0.1.0
 176  
          */
 177  
         public Iterator<Parameter> iterator () {
 178  2130
                 return this.parameters.iterator();
 179  
         }
 180  
 
 181  
         //---- Object
 182  
 
 183  
         /**
 184  
          * @since 0.1.0
 185  
          */
 186  
         public boolean equals (Object obj) {
 187  869
                 if (obj == null) {
 188  0
                         return false;
 189  869
                 } else if (obj == this) {
 190  0
                         return true;
 191  869
                 } else if (obj instanceof ParameterList) {
 192  869
                         final ParameterList other = (ParameterList) obj;
 193  869
                         if (getParameterCount() != other.getParameterCount()) {
 194  131
                                 return false;
 195  
                         }
 196  738
                         Iterator<Parameter> it1 = iterator();
 197  738
                         Iterator<Parameter> it2 = other.iterator();
 198  813
                         while (it1.hasNext() && it2.hasNext()) {
 199  81
                                 if (!it1.next().equals(it2.next())) {
 200  6
                                         return false;
 201  
                                 }
 202  
                         }
 203  732
                         return true;
 204  
                 }
 205  0
                 return false;
 206  
         }
 207  
 
 208  
         /**
 209  
          * @since 0.1.0
 210  
          */
 211  
         public int hashCode () {
 212  110
                 return this.parameters.hashCode();
 213  
         }
 214  
 
 215  
         /**
 216  
          * @since 0.1.0
 217  
          */
 218  
         public ParameterList getCopy () {
 219  1754
                 ParameterList result = new ParameterList();
 220  1754
                 for (Parameter parameter : this.parameters) {
 221  341
                         result.add(parameter.getName(), parameter.getType());
 222  
                 }
 223  1754
                 return result;
 224  
         }
 225  
 
 226  
         //---- Parameter
 227  
 
 228  
         /**
 229  
          * A parameter with a name and a type.
 230  
          * @since 0.1.0
 231  
          */
 232  
         public static class Parameter {
 233  
 
 234  
                 //---- State
 235  
 
 236  
                 /**
 237  
                  * @since 0.1.0
 238  
                  */
 239  
                 private String name;
 240  
 
 241  
                 /**
 242  
                  * @since 0.1.0
 243  
                  */
 244  
                 private Class<?> type;
 245  
 
 246  
                 //---- Constructors
 247  
 
 248  
                 /**
 249  
                  * Creates a parameter.
 250  
                  * @param name the name.
 251  
                  * @param type the type.
 252  
                  */
 253  621
                 public Parameter (String name, Class<?> type) {
 254  621
                         this.name = name;
 255  621
                         this.type = type;
 256  621
                 }
 257  
 
 258  
                 //---- Methods
 259  
 
 260  
                 /**
 261  
                  * Returns the name.
 262  
                  * @return the name.
 263  
                  * @since 0.1.0
 264  
                  */
 265  
                 public String getName () {
 266  885
                         return this.name;
 267  
                 }
 268  
 
 269  
                 /**
 270  
                  * Returns the type.
 271  
                  * @return the type.
 272  
                  * @since 0.1.0
 273  
                  */
 274  
                 public Class<?> getType () {
 275  810
                         return this.type;
 276  
                 }
 277  
 
 278  
                 //---- Object
 279  
 
 280  
                 /**
 281  
                  * @since 0.1.0
 282  
                  */
 283  
                 public boolean equals (Object obj) {
 284  81
                         if (obj == null) {
 285  0
                                 return false;
 286  
                         }
 287  81
                         if (this == obj) {
 288  0
                                 return true;
 289  
                         }
 290  81
                         if (obj instanceof Parameter) {
 291  81
                                 final Parameter other = (Parameter) obj;
 292  81
                                 if (getName().equals(other.getName()) &&
 293  
                                         getType().equals(other.getType())
 294  
                                 ) {
 295  75
                                         return true;
 296  
                                 }
 297  
                         }
 298  6
                         return false;
 299  
                 }
 300  
 
 301  
                 /**
 302  
                  * @since 0.1.0
 303  
                  */
 304  
                 public int hashCode () {
 305  34
                         return getName().hashCode() * getType().hashCode();
 306  
                 }
 307  
         }
 308  
 
 309  
 }