This question has been edited from its original version
Hi everyone, following your comments, I have modified my original class. The objective of this class is to store two sets of values:
- The first one is the "applied" one. If the user request the data from the class, he will receive this one.
- The second one is the "pending" changes. If the user modify the data, it will be stored into this set.
- When the user apply the changes, they are copied in the first set.
Now, here is the class as I changed it:
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class ChangesQueue< K, V, T extends Map< K, V > > implements Serializable {
private static final long serialVersionUID = 1L;
HashMap< K, V > mCurrent;
HashMap< K, V > mStack;
public ChangesQueue() {
mCurrent = new HashMap< K, V >();
mStack = new HashMap< K, V >();
}
public ChangesQueue(T iValue) {
this();
set(iValue);
apply();
}
public void apply() {
mCurrent = new HashMap< K, V >(mStack);
}
public Map< K, V > get() {
return mCurrent;
}
public void set(T iValue) {
mStack.putAll(iValue);
}
}
The biggest issue that I have here is that I cannot actually store the real type of T, which is why I am instantiating 'HashMap'. But this class is not intended to work only with 'HashMap', but with any kind of maps.
Actually, I am using this memory with the following class:
public class IndexedHashMap<K, T> implements Map<K, Pair< Integer, T >>, Serializable
And accessing the data with the following method:
private IndexedHashMap<String, T> getElements()
{
return (null != mElements.get()) ? (IndexedHashMap<String, T>) mElements.get() : new IndexedHashMap<String, T>();
}
Of course this is not possible as the cast is not valid.
I am currently considering a simple extension of 'IndexedHashMap' but I would really need a sort of generic Memory (this is not a YAGNI case) and I do not want to get multiples, specific classes, for the same thing.
Would there be any way to do this while keeping the generic side ? Maybe it is achievable if I use a 'Class clazz' argument which would allow me to instantiate the real type of T instead of an HashMap ?
Thanks for the help