source: references/src/main/java/geniusweb/references/Parameters.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 4.0 KB
Line 
1package geniusweb.references;
2
3import java.util.HashMap;
4
5import com.fasterxml.jackson.annotation.JsonCreator;
6import com.fasterxml.jackson.annotation.JsonValue;
7
8/**
9 * init parameters for a party. Object must be either a HashMap, List, String or
10 * Number. Other objects may not deserialize properly. We never use blanket
11 * Object deserializers (enableDefaultTyping) because of security reasons.
12 *
13 */
14public class Parameters {
15
16 @JsonValue
17 private final HashMap<String, Object> params = new HashMap<>();
18
19 public Parameters() {
20 }
21
22 @JsonCreator
23 public Parameters(HashMap<String, Object> vals) {
24 params.putAll(vals);
25 }
26
27 /**
28 *
29 * @param key the key to get the value for.
30 * @return the raw value
31 */
32 public Object get(String key) {
33 return params.get(key);
34 }
35
36 /**
37 *
38 * @return true iff params is empty
39 */
40 public boolean isEmpty() {
41 return params.isEmpty();
42 }
43
44 /**
45 *
46 * @param key the key to be checked
47 * @return true iff params contains the given key
48 */
49 public boolean containsKey(String key) {
50 return params.containsKey(key);
51 }
52
53 /**
54 *
55 * @param key the key. Key may already in params.
56 * @param val the new value for the key
57 * @return new Parameters , which is copy of this but with key-value pair
58 * added/overridden
59 */
60 public Parameters with(String key, Object val) {
61 HashMap<String, Object> newparams = new HashMap<String, Object>(params);
62 newparams.put(key, val);
63 return new Parameters(newparams);
64 }
65
66 /**
67 * Getter with type-check
68 *
69 * @param <T> the expected type
70 * @param paramname the parameter name that must be in the map
71 * @param classType the expected value type for this parameter
72 * @return object of requested type.
73 * @throws IllegalArgumentException if the object is not available.
74 */
75 @SuppressWarnings("unchecked")
76 public <T> T get(String paramname, Class<T> classType) {
77 if (!params.containsKey(paramname)
78 || !(classType.isInstance(params.get(paramname))))
79 throw new IllegalArgumentException(" Missing a parameter "
80 + paramname + " with a " + classType.getName() + " value");
81 return (T) params.get(paramname);
82 }
83
84 /**
85 *
86 * @param parameters the parameters to be added/overridden
87 * @return new Parameters, with new parameters ADDED to existing. The new
88 * parameters override existing ones.
89 */
90 public Parameters with(Parameters parameters) {
91 HashMap<String, Object> newparams = new HashMap<String, Object>(params);
92 newparams.putAll(parameters.params);
93 return new Parameters(newparams);
94 }
95
96 /**
97 *
98 * @param name the parameter name
99 * @param defaultval the default value to return if parameter is not
100 * available or not inside the given range. Can be null
101 * @param min the minimum value, or null if not applicable
102 * @param max the maximum value, or null if not applicable
103 * @return the double value contained inside the parameter with the given
104 * name, or defaultval if the parameter does not exist, does not
105 * contain a double, or is outside [min, max].
106 */
107 public Double getDouble(String name, Double defaultval, Double min,
108 Double max) {
109 if (!params.containsKey(name) || !(params.get(name) instanceof Number))
110 return defaultval;
111 Double val = ((Number) params.get(name)).doubleValue();
112 if (min != null && val < min)
113 return defaultval;
114 if (max != null && val > max)
115 return defaultval;
116 return val;
117 }
118
119 @Override
120 public int hashCode() {
121 final int prime = 31;
122 int result = 1;
123 result = prime * result + ((params == null) ? 0 : params.hashCode());
124 return result;
125 }
126
127 @Override
128 public boolean equals(Object obj) {
129 if (this == obj)
130 return true;
131 if (obj == null)
132 return false;
133 if (getClass() != obj.getClass())
134 return false;
135 Parameters other = (Parameters) obj;
136 if (params == null) {
137 if (other.params != null)
138 return false;
139 } else if (!params.equals(other.params))
140 return false;
141 return true;
142 }
143
144 @Override
145 public String toString() {
146 return params.toString();
147 }
148
149}
Note: See TracBrowser for help on using the repository browser.