##### Coverage Report - org.jbehave.core.steps.ConvertedParameters

Classes in this File | Line Coverage | Branch Coverage | Complexity
ConvertedParameters |
100% | 15/15
100% | 4/4
| 1.5;1.5
ConvertedParameters$ValueNotFound |
100% | 2/2
N/A | N/A
| 1.5;1.5

1 |  |
     package org.jbehave.core.steps;
2 |  |
3 |  |
     import java.util.Map;
4 |  |
5 |  |
     /**
6 |  |
      * Implementation of Parameters that uses {@link ParameterConverters} to convert
7 |  |
      * values.
8 |  |
      */
9 |  |
     public class ConvertedParameters implements Parameters {
10 |  |
11 |  |
         private final Map<String, String> values;
12 |  |
         private final ParameterConverters parameterConverters;
13 |  |
14 |  |
         /**
15 |  |
          * Creates an instance of ConvertedParameters from a Row which provides the
16 |  |
          * values
17 |  |
          *
18 |  |
          * @param row the Row to get the values from
19 |  |
          * @param parameterConverters the ParameterConverters used for conversion
20 |  |
          */
21 |  |
         public ConvertedParameters(Row row, ParameterConverters parameterConverters) {
22 | 12 |
             this(row.values(), parameterConverters);
23 | 12 |
         }
24 |  |
25 |  |
         /**
26 |  |
          * Creates an instance of ConvertedParameters with given values
27 |  |
          *
28 |  |
          * @param values the Map<String,String> of values
29 |  |
          * @param parameterConverters the ParameterConverters used for conversion
30 |  |
          */
31 | 211 |
         public ConvertedParameters(Map<String, String> values, ParameterConverters parameterConverters) {
32 | 211 |
             this.values = values;
33 | 211 |
             this.parameterConverters = parameterConverters;
34 | 211 |
         }
35 |  |
36 |  |
         public <T> T valueAs(String name, Class<T> type) {
37 | 25 |
             return convert(valueFor(name), type);
38 |  |
         }
39 |  |
40 |  |
         public <T> T valueAs(String name, Class<T> type, T defaultValue) {
41 | 6 |
             if (values.containsKey(name)) {
42 | 2 |
                 return valueAs(name, type);
43 |  |
             }
44 | 4 |
             return defaultValue;
45 |  |
         }
46 |  |
47 |  |
         private <T> T convert(String value, Class<T> type) {
48 | 24 |
             return type.cast(parameterConverters.convert(value, type));
49 |  |
         }
50 |  |
51 |  |
         private String valueFor(String name) {
52 | 25 |
             if ( !values.containsKey(name) ){
53 | 1 |
                 throw new ValueNotFound(name);
54 |  |
             }
55 | 24 |
             return values.get(name);
56 |  |
         }
57 |  |
58 |  |
         public Map<String, String> values() {
59 | 72 |
             return values;
60 |  |
         }
61 |  |
62 |  |
         @SuppressWarnings("serial")
63 |  |
         public static class ValueNotFound extends RuntimeException {
64 |  |
65 |  |
             public ValueNotFound(String name) {
66 | 1 |
                 super(name);
67 | 1 |
             }
68 |  |
69 |  |
         }
70 |  |
71 |  |
     }