Created
November 28, 2014 09:36
-
-
Save Jezza/6a39425fe4b90413133b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class ConfigHandler { | |
private Collection<Class<?>> registeredClasses; | |
private ConfigCollection<? extends Annotation> configCollection; | |
public ConfigHandler() { | |
registeredClasses = new LinkedHashSet<>(); | |
configCollection = new ConfigCollection<>(); | |
configCollection.registerAnnotations(Config.VALID_CONFIG_ANNOTATIONS); | |
} | |
public boolean register(Class<?> clazz) { | |
if (!registeredClasses.contains(clazz)) | |
registeredClasses.add(clazz); | |
return registeredClasses.contains(clazz); | |
} | |
public boolean unregister(Class<?> clazz) { | |
if (registeredClasses.contains(clazz)) | |
registeredClasses.remove(clazz); | |
return !registeredClasses.contains(clazz); | |
} | |
public void initialiseFields() { | |
for (Class<?> clazz : registeredClasses) | |
for (Field field : clazz.getDeclaredFields()) | |
processAnnotations(field); | |
} | |
public void readFrom(File file) { | |
Configuration config = new Configuration(file); | |
config.load(); | |
Collection<ConfigInteger> annotations = configCollection.getAnnotationCollection(ConfigInteger.class); | |
CoreProperties.logger.info("ConfigInteger size: " + annotations.size()); | |
config.save(); | |
} | |
private void processAnnotations(Field field) { | |
Class[] validConfigAnnotations = Config.VALID_CONFIG_ANNOTATIONS; | |
for (int i = 0; i < validConfigAnnotations.length; i++) { | |
Class<? extends Annotation> clazz = validConfigAnnotations[i]; | |
if (field.isAnnotationPresent(clazz)){ | |
configCollection.add(clazz.cast(field.getAnnotation(clazz))); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment