I have a heterogeneous map which exists in JSON format and I would like to parse it and turn it into a heterogeneous map object (class HeterogeneousMap
).
To parse the map I use an object which defines all known keys the map can have (class HeterogeneousMapStructure
).
The MapKey<T>
interface has the method T parseValue(JsonReader jsonReader)
to parse the value of the key.
The problem I am having is how to put the parsed value in the type safe heterogeneous map object:
public class HeterogeneousMap {
public <T> void put(MapKey<T> mapKey, T value) {
// Put key and value in map
}
}
public interface MapKey<T> {
T parseValue(JsonReader jsonReader) throws IOException;
}
public class HeterogeneousMapStructure {
private final List<MapKey<?>> keyList;
public HeterogeneousMap parseMap(JsonReader jsonReader) {
HeterogeneousMap heterogeneousMap = new HeterogeneousMap();
// ... find matching key
MapKey<?> matchingMapKey = ...;
/*
* Compiling error:
* The method put(TestClass.MapKey<T>, T) in the type TestClass.HeterogeneousMap
* is not applicable for the arguments (TestClass.MapKey<capture#1-of ?>, capture#2-of ?)
*/
heterogeneousMap.put(matchingMapKey, matchingMapKey.parseValue(jsonReader));
return heterogeneousMap;
}
}
Is there a way to solve this?