Created
June 9, 2010 15:24
-
-
Save gilleain/431636 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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