1 | package geniusweb.references;
|
---|
2 |
|
---|
3 | import java.util.HashMap;
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * init parameters for a party. Object must be either a HashMap, List, String or
|
---|
7 | * Number. Other objects may not deserialize properly. We never use blanket
|
---|
8 | * Object deserializers (enableDefaultTyping) because of security reasons.
|
---|
9 | *
|
---|
10 | */
|
---|
11 | public class Parameters extends HashMap<String, Object> {
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Getter with type-check
|
---|
15 | *
|
---|
16 | * @param <T> the expected type
|
---|
17 | * @param paramname the parameter name that must be in the map
|
---|
18 | * @param classType the expected value type for this parameter
|
---|
19 | * @return object of requested type.
|
---|
20 | * @throws IllegalArgumentException if the object is not available.
|
---|
21 | */
|
---|
22 | @SuppressWarnings("unchecked")
|
---|
23 | public <T> T get(String paramname, Class<T> classType) {
|
---|
24 | if (!containsKey(paramname) || !(classType.isInstance(get(paramname))))
|
---|
25 | throw new IllegalArgumentException(" Missing a parameter "
|
---|
26 | + paramname + " with a " + classType.getName() + " value");
|
---|
27 | return (T) get(paramname);
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | *
|
---|
32 | * @param name the parameter name
|
---|
33 | * @param defaultval the default value to return if parameter is not
|
---|
34 | * available or not inside the given range. Can be null
|
---|
35 | * @param min the minimum value, or null if not applicable
|
---|
36 | * @param max the maximum value, or null if not applicable
|
---|
37 | * @return the double value contained inside the parameter with the given
|
---|
38 | * name, or defaultval if the parameter does not exist, does not
|
---|
39 | * contain a double, or is outside [min, max].
|
---|
40 | */
|
---|
41 | public Double getDouble(String name, Double defaultval, Double min,
|
---|
42 | Double max) {
|
---|
43 | if (!containsKey(name) || !(get(name) instanceof Double))
|
---|
44 | return defaultval;
|
---|
45 | Double val = (Double) get(name);
|
---|
46 | if (min != null && val < min)
|
---|
47 | return defaultval;
|
---|
48 | if (max != null && val > max)
|
---|
49 | return defaultval;
|
---|
50 | return val;
|
---|
51 | }
|
---|
52 | }
|
---|