@@ -123,7 +123,7 @@ public void putDouble(String key, double value) {
123
123
public void put (String key , @ Nullable Object value ) {
124
124
if (value != null ) {
125
125
Object result = this .map .put (key , value );
126
- this .dirty = result == null || result != null && !result .equals (value );
126
+ this .dirty = result == null || !result .equals (value );
127
127
}
128
128
else {
129
129
Object result = this .map .remove (key );
@@ -148,7 +148,7 @@ public boolean isDirty() {
148
148
*/
149
149
public String getString (String key ) {
150
150
151
- return ( String ) readAndValidate (key , String .class );
151
+ return readAndValidate (key , String .class );
152
152
}
153
153
154
154
/**
@@ -174,7 +174,7 @@ public String getString(String key, String defaultString) {
174
174
*/
175
175
public long getLong (String key ) {
176
176
177
- return ( Long ) readAndValidate (key , Long .class );
177
+ return readAndValidate (key , Long .class );
178
178
}
179
179
180
180
/**
@@ -200,7 +200,7 @@ public long getLong(String key, long defaultLong) {
200
200
*/
201
201
public int getInt (String key ) {
202
202
203
- return ( Integer ) readAndValidate (key , Integer .class );
203
+ return readAndValidate (key , Integer .class );
204
204
}
205
205
206
206
/**
@@ -225,7 +225,7 @@ public int getInt(String key, int defaultInt) {
225
225
* @return The <code>Double</code> value
226
226
*/
227
227
public double getDouble (String key ) {
228
- return ( Double ) readAndValidate (key , Double .class );
228
+ return readAndValidate (key , Double .class );
229
229
}
230
230
231
231
/**
@@ -255,14 +255,51 @@ public Object get(String key) {
255
255
return this .map .get (key );
256
256
}
257
257
258
+ /**
259
+ * Typesafe getter for the value represented by the provided key, with cast to given class.
260
+ *
261
+ * @param key The key to get a value for
262
+ * @param clazz The class of return type
263
+ * @param <V> Type of returned value
264
+ * @return The value of given type represented by the given key or {@code null} if the key
265
+ * is not present
266
+ */
267
+ @ Nullable
268
+ public <V > V get (String key , Class <V > clazz ) {
269
+ Object value = this .map .get (key );
270
+ if (value == null ) {
271
+ return null ;
272
+ }
273
+ return get (key , clazz , null );
274
+ }
275
+
276
+ /**
277
+ * Typesafe getter for the value represented by the provided key, with cast to given class.
278
+ *
279
+ * @param key The key to get a value for
280
+ * @param type The class of return type
281
+ * @param defaultValue Default value in case element is not present
282
+ * @param <V> Type of returned value
283
+ * @return The value of given type represented by the given key or {@code null} if the key
284
+ * is not present
285
+ */
286
+ @ Nullable
287
+ public <V > V get (String key , Class <V > clazz , @ Nullable V defaultValue ) {
288
+ Object value = this .map .get (key );
289
+ if (value == null ) {
290
+ return defaultValue ;
291
+ }
292
+ return clazz .cast (value );
293
+ }
294
+
258
295
/**
259
296
* Utility method that attempts to take a value represented by a given key and
260
297
* validate it as a member of the specified type.
261
298
* @param key The key to validate a value for
262
299
* @param type Class against which value should be validated
263
300
* @return Value typed to the specified <code>Class</code>
264
301
*/
265
- private Object readAndValidate (String key , Class <? > type ) {
302
+ private < V > V readAndValidate (String key , Class <V > type ) {
266
303
267
304
Object value = get (key );
268
305
@@ -271,7 +308,7 @@ private Object readAndValidate(String key, Class<?> type) {
271
308
+ (value == null ? null : "(" + value .getClass () + ")" + value ) + "]" );
272
309
}
273
310
274
- return value ;
311
+ return type . cast ( value ) ;
275
312
}
276
313
277
314
/**
0 commit comments