Skip to content

Commit 4116361

Browse files
committed
New Dynamic Model pattern to support type-checked additional properties
Why: The SDK code generator is being updated to improve the pattern used for dynamic models (i.e. models that support "additionalProperties"). What: The changes in this PR can be summarized as follows: 1) An enhanced DynamicModel class which is now parameterized with the additionalProperties value type and includes a map for them 2) A new DynamicModelTypeAdapterFactory class which is registered with Gson to perform serialization and deserialization of ALL generated DynamicModel subclasses. 3) GsonSerializationHelper is now deprecated as it's no longer needed. 4) Tests related to the above functional changes.
1 parent 40fa057 commit 4116361

16 files changed

+1583
-4
lines changed

src/main/java/com/ibm/cloud/sdk/core/service/model/DynamicModel.java

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,103 @@
11
package com.ibm.cloud.sdk.core.service.model;
22

33
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Set;
46

7+
import com.google.gson.reflect.TypeToken;
58
import com.ibm.cloud.sdk.core.util.GsonSingleton;
69

710
/**
8-
* Abstract model class for objects which may have dynamic properties attached to them,
9-
* which is represented with an internal map.
11+
* Abstract model class for a model which supports dynamic (arbitrary) properties of type T.
1012
*/
11-
public abstract class DynamicModel extends HashMap<String, Object> implements ObjectModel {
13+
public abstract class DynamicModel<T> implements ObjectModel {
14+
private TypeToken<T> additionalPropertyTypeToken;
15+
16+
// The set of dynamic properties associated with this object.
17+
private Map<String, T> dynamicProperties = new HashMap<>();
18+
19+
/**
20+
* Force use of 1-arg ctor.
21+
*/
22+
@SuppressWarnings("unused")
23+
private DynamicModel() {
24+
}
25+
26+
/**
27+
* This ctor accepts a TypeToken instance that represents the type of values stored in the map.
28+
*
29+
* @param t
30+
* the TypeToken which represents the type of map values
31+
*/
32+
public DynamicModel(TypeToken<T> t) {
33+
this.additionalPropertyTypeToken = t;
34+
}
35+
36+
/**
37+
* Returns the TypeToken which describes the type of additional properties stored in the map.
38+
* @return The TypeToken which descriptes the map value type
39+
*/
40+
public TypeToken<T> getAdditionalPropertyTypeToken() {
41+
return this.additionalPropertyTypeToken;
42+
}
43+
44+
/**
45+
* Sets an arbitrary property.
46+
*
47+
* @param key
48+
* the name of the property to set
49+
* @param value
50+
* the value of the property to be set
51+
* @return the previous value of the property, or null if the property was not previously set
52+
*/
53+
public T setProperty(String key, T value) {
54+
return this.dynamicProperties.put(key, value);
55+
}
56+
57+
/**
58+
* Returns the value of the specified property.
59+
*
60+
* @param key
61+
* the name of the property to get
62+
* @return the value of the property, or null if the property is not set
63+
*/
64+
public T getProperty(String key) {
65+
return this.dynamicProperties.get(key);
66+
}
67+
68+
/**
69+
* Returns a map containing the arbitrary properties set on this object.
70+
*
71+
* @return a copy of the map containing arbitrary properties set on this object
72+
*/
73+
public Map<String, T> getProperties() {
74+
return new HashMap<String, T>(this.dynamicProperties);
75+
}
76+
77+
/**
78+
* Returns the names of arbitrary properties set on this object.
79+
* @return a set containing the names of arbitrary properties set on this object
80+
*/
81+
public Set<String> getPropertyNames() {
82+
return this.dynamicProperties.keySet();
83+
}
84+
/**
85+
* Removes the specified property from this object's map of arbitrary properties.
86+
*
87+
* @param key
88+
* the name of the property to be removed
89+
* @return the previous value of the property, or null if the property was not previously set
90+
*/
91+
public T removeProperty(String key) {
92+
return this.dynamicProperties.remove(key);
93+
}
94+
95+
/**
96+
* Removes all of the arbitrary properties set on this object.
97+
*/
98+
public void removeProperties() {
99+
this.dynamicProperties.clear();
100+
}
12101

13102
/*
14103
* (non-Javadoc)
@@ -23,7 +112,7 @@ public boolean equals(Object o) {
23112
return false;
24113
}
25114

26-
final DynamicModel other = (DynamicModel) o;
115+
final DynamicModel<?> other = (DynamicModel<?>) o;
27116

28117
return toString().equals(other.toString());
29118
}

0 commit comments

Comments
 (0)