| 1 | |
package org.jbehave.core.steps; |
| 2 | |
|
| 3 | |
import java.util.Map; |
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
public class ConvertedParameters implements Parameters { |
| 10 | |
|
| 11 | |
private final Map<String, String> values; |
| 12 | |
private final ParameterConverters parameterConverters; |
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
public ConvertedParameters(Row row, ParameterConverters parameterConverters) { |
| 22 | 22 | this(row.values(), parameterConverters); |
| 23 | 22 | } |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | 244 | public ConvertedParameters(Map<String, String> values, ParameterConverters parameterConverters) { |
| 32 | 244 | this.values = values; |
| 33 | 244 | this.parameterConverters = parameterConverters; |
| 34 | 244 | } |
| 35 | |
|
| 36 | |
public <T> T valueAs(String name, Class<T> type) { |
| 37 | 37 | 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 | 36 | return type.cast(parameterConverters.convert(value, type)); |
| 49 | |
} |
| 50 | |
|
| 51 | |
private String valueFor(String name) { |
| 52 | 37 | if ( !values.containsKey(name) ){ |
| 53 | 1 | throw new ValueNotFound(name); |
| 54 | |
} |
| 55 | 36 | return values.get(name); |
| 56 | |
} |
| 57 | |
|
| 58 | |
public Map<String, String> values() { |
| 59 | 122 | 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 | |
} |