Moved Configurator to the correct package

This commit is contained in:
Ziver Koc 2015-12-08 16:19:43 +01:00
parent d3f3c02f78
commit 432d7cb438

View file

@ -1,197 +1,197 @@
/* /*
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2015 Ziver Koc * Copyright (c) 2015 Ziver Koc
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package zutil.db.bean; package zutil.ui;
import zutil.log.LogUtil; import zutil.log.LogUtil;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
* Created by Ziver * Created by Ziver
*/ */
public class Configurator<T> { public class Configurator<T> {
private static final Logger log = LogUtil.getLogger(); private static final Logger log = LogUtil.getLogger();
/** /**
* Sets a field in a class as externally configurable. * Sets a field in a class as externally configurable.
*/ */
@Retention(RetentionPolicy.RUNTIME) // Make this annotation accessible at runtime via reflection. @Retention(RetentionPolicy.RUNTIME) // Make this annotation accessible at runtime via reflection.
@Target({ElementType.FIELD}) // This annotation can only be applied to class fields. @Target({ElementType.FIELD}) // This annotation can only be applied to class fields.
public static @interface Configurable{ public static @interface Configurable{
/* Nice name of this parameter */ /* Nice name of this parameter */
String value(); String value();
/* Defines the order the parameters, in ascending order */ /* Defines the order the parameters, in ascending order */
int order() default Integer.MAX_VALUE; int order() default Integer.MAX_VALUE;
} }
public static enum ConfigType{ public static enum ConfigType{
STRING, INT, BOOLEAN STRING, INT, BOOLEAN
} }
private static HashMap<Class, ConfigurationParam[]> classConf = new HashMap<>(); private static HashMap<Class, ConfigurationParam[]> classConf = new HashMap<>();
private T obj; private T obj;
private ConfigurationParam[] params; private ConfigurationParam[] params;
public Configurator(T obj){ public Configurator(T obj){
this.obj = obj; this.obj = obj;
this.params = getConfiguration(obj.getClass(), obj); this.params = getConfiguration(obj.getClass(), obj);
} }
public T getObject(){ public T getObject(){
return obj; return obj;
} }
public ConfigurationParam[] getConfiguration(){ public ConfigurationParam[] getConfiguration(){
return params; return params;
} }
public static ConfigurationParam[] getConfiguration(Class c){ public static ConfigurationParam[] getConfiguration(Class c){
if(!classConf.containsKey(c)) if(!classConf.containsKey(c))
classConf.put(c, getConfiguration(c, null)); classConf.put(c, getConfiguration(c, null));
return classConf.get(c); return classConf.get(c);
} }
protected static ConfigurationParam[] getConfiguration(Class c, Object obj){ protected static ConfigurationParam[] getConfiguration(Class c, Object obj){
ArrayList<ConfigurationParam> conf = new ArrayList<ConfigurationParam>(); ArrayList<ConfigurationParam> conf = new ArrayList<ConfigurationParam>();
Field[] all = c.getDeclaredFields(); Field[] all = c.getDeclaredFields();
for(Field f : all){ for(Field f : all){
if(f.isAnnotationPresent(Configurable.class) && if(f.isAnnotationPresent(Configurable.class) &&
!Modifier.isStatic(f.getModifiers()) && !Modifier.isTransient(f.getModifiers())) { !Modifier.isStatic(f.getModifiers()) && !Modifier.isTransient(f.getModifiers())) {
try { try {
conf.add(new ConfigurationParam(f, obj)); conf.add(new ConfigurationParam(f, obj));
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
log.log(Level.WARNING, null, e); log.log(Level.WARNING, null, e);
} }
} }
} }
ConfigurationParam[] list = conf.toArray(new ConfigurationParam[conf.size()]); ConfigurationParam[] list = conf.toArray(new ConfigurationParam[conf.size()]);
Arrays.sort(list); Arrays.sort(list);
return list; return list;
} }
public void applyConfiguration(){ public void applyConfiguration(){
for(ConfigurationParam param : params){ for(ConfigurationParam param : params){
try { try {
param.apply(); param.apply();
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
log.log(Level.WARNING, null, e); log.log(Level.WARNING, null, e);
} }
} }
} }
public static class ConfigurationParam implements Comparable<ConfigurationParam>{ public static class ConfigurationParam implements Comparable<ConfigurationParam>{
protected Field field; protected Field field;
protected String name; protected String name;
protected String niceName; protected String niceName;
protected ConfigType type; protected ConfigType type;
protected Object obj; protected Object obj;
protected Object value; protected Object value;
protected int order; protected int order;
protected ConfigurationParam(Field f, Object o) throws IllegalAccessException { protected ConfigurationParam(Field f, Object o) throws IllegalAccessException {
field = f; field = f;
obj = o; obj = o;
field.setAccessible(true); field.setAccessible(true);
name = field.getName(); name = field.getName();
if(obj != null) if(obj != null)
value = field.get(obj); value = field.get(obj);
if(field.isAnnotationPresent(Configurable.class)) { if(field.isAnnotationPresent(Configurable.class)) {
niceName = field.getAnnotation(Configurable.class).value(); niceName = field.getAnnotation(Configurable.class).value();
order = field.getAnnotation(Configurable.class).order(); order = field.getAnnotation(Configurable.class).order();
} }
else{ else{
niceName = name; niceName = name;
order = Integer.MAX_VALUE; order = Integer.MAX_VALUE;
} }
if (f.getType() == String.class) type = ConfigType.STRING; if (f.getType() == String.class) type = ConfigType.STRING;
else if(f.getType() == int.class) type = ConfigType.INT; else if(f.getType() == int.class) type = ConfigType.INT;
else if(f.getType() == boolean.class)type = ConfigType.BOOLEAN; else if(f.getType() == boolean.class)type = ConfigType.BOOLEAN;
} }
public String getName(){ return name;} public String getName(){ return name;}
public String getNiceName(){ return niceName;} public String getNiceName(){ return niceName;}
public ConfigType getType(){ return type;} public ConfigType getType(){ return type;}
public boolean isTypeString(){ return type == ConfigType.STRING;} public boolean isTypeString(){ return type == ConfigType.STRING;}
public boolean isTypeInt(){ return type == ConfigType.INT;} public boolean isTypeInt(){ return type == ConfigType.INT;}
public boolean isTypeBoolean(){return type == ConfigType.BOOLEAN;} public boolean isTypeBoolean(){return type == ConfigType.BOOLEAN;}
public String getString(){ public String getString(){
if(value == null) if(value == null)
return null; return null;
return value.toString(); return value.toString();
} }
public boolean getBoolean(){ public boolean getBoolean(){
if(value == null || type != ConfigType.BOOLEAN) if(value == null || type != ConfigType.BOOLEAN)
return false; return false;
return (boolean)value; return (boolean)value;
} }
/** /**
* This method will set a value for the represented field, * This method will set a value for the represented field,
* to apply the change to the source object the method * to apply the change to the source object the method
* {@link #applyConfiguration()} needs to be called * {@link #applyConfiguration()} needs to be called
*/ */
public void setValue(String v){ public void setValue(String v){
if(obj == null) if(obj == null)
return; return;
switch(type){ switch(type){
case STRING: case STRING:
value = v; break; value = v; break;
case INT: case INT:
value = Integer.parseInt(v); break; value = Integer.parseInt(v); break;
case BOOLEAN: case BOOLEAN:
value = Boolean.parseBoolean(v); break; value = Boolean.parseBoolean(v); break;
} }
} }
protected void apply() throws IllegalAccessException { protected void apply() throws IllegalAccessException {
field.set(obj, value); field.set(obj, value);
} }
@Override @Override
public int compareTo(ConfigurationParam configurationParam) { public int compareTo(ConfigurationParam configurationParam) {
return this.order - configurationParam.order; return this.order - configurationParam.order;
} }
} }
} }