Skip to content

Instantly share code, notes, and snippets.

@gilleain
Created June 9, 2010 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilleain/431636 to your computer and use it in GitHub Desktop.
Save gilleain/431636 to your computer and use it in GitHub Desktop.
package tmp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Model {
public Map<String, Class> parameterTypes;
public Map<String, Object> defaultValues;
public Map<String, Object> currentValues;
public Model(List<IGenerator> generators) {
parameterTypes = new HashMap<String, Class>();
defaultValues = new HashMap<String, Object>();
currentValues = new HashMap<String, Object>();
for (IGenerator generator : generators) {
for (String parameterName : generator.getParameterNames()) {
register(parameterName,
generator.getParameterType(parameterName),
generator.getParameterDefault(parameterName));
}
}
}
public void register(String parameter, Class type, Object defaultValue) {
parameterTypes.put(parameter, type);
defaultValues.put(parameter, defaultValue);
}
public Object get(String parameterName) {
if (currentValues.containsKey(parameterName)) {
return currentValues.get(parameterName);
} else {
return defaultValues.get(parameterName);
}
}
public void set(String parameterName, Object value) throws Exception {
Class requiredClass = parameterTypes.get(parameterName);
if (value.getClass() == requiredClass) {
currentValues.put(parameterName, value);
} else {
throw new Exception();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment