I am looking at creating a Hashmap class which allows me to store key and value. However, the value can only be stored if it matches a specific type. For example, EMAIL(String.class)
- that value for key Test can only be stored if it is of type String
.
I have created following class :
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
public class test {
private static final Map<ValidKeys, Object> sessionData = new HashMap<>();
public enum ValidKeys {
EMAIL(String.class),
PASSWORD(String.class),
FIRST_NAME(String.class),
LAST_NAME(String.class),;
private Class type;
private boolean isList;
private Pattern pattern;
ValidKeys(Class type, boolean isList) {
this.type = type;
this.isList = isList;
}
ValidKeys(Class<String> type) {
}
}
public <T> void setData(ValidKeys key, T value) {
sessionData.put(key,value);
}
public <T> T getData(ValidKeys key) {
return (T) sessionData.get(key);
}
public static void main(String[] args) {
test t = new test();
t.setData(ValidKeys.EMAIL,"abc");
System.out.println(t.getData(ValidKeys.EMAIL).toString());
}
}
I would like to use methods such as setData
and getData
and store values into sessionData
. Also, I want to ensure if the value is a list of objects then thats stored properly as well.
Aucun commentaire:
Enregistrer un commentaire