Made Configurator support generics
This commit is contained in:
parent
b92465b62b
commit
67e4d54fde
1 changed files with 11 additions and 8 deletions
19
src/zutil/db/bean/Configurator.java
Normal file → Executable file
19
src/zutil/db/bean/Configurator.java
Normal file → Executable file
|
|
@ -41,10 +41,9 @@ import java.util.logging.Logger;
|
||||||
/**
|
/**
|
||||||
* Created by Ziver
|
* Created by Ziver
|
||||||
*/
|
*/
|
||||||
public class Configurator {
|
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.
|
||||||
*/
|
*/
|
||||||
|
|
@ -65,14 +64,18 @@ public class Configurator {
|
||||||
|
|
||||||
private static HashMap<Class, ConfigurationParam[]> classConf = new HashMap<>();
|
private static HashMap<Class, ConfigurationParam[]> classConf = new HashMap<>();
|
||||||
|
|
||||||
private Object obj;
|
private T obj;
|
||||||
private ConfigurationParam[] params;
|
private ConfigurationParam[] params;
|
||||||
|
|
||||||
public Configurator(Object 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(){
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
public ConfigurationParam[] getConfiguration(){
|
public ConfigurationParam[] getConfiguration(){
|
||||||
return params;
|
return params;
|
||||||
}
|
}
|
||||||
|
|
@ -102,10 +105,10 @@ public class Configurator {
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setConfiguration(){
|
public void applyConfiguration(){
|
||||||
for(ConfigurationParam param : params){
|
for(ConfigurationParam param : params){
|
||||||
try {
|
try {
|
||||||
param.set();
|
param.apply();
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
log.log(Level.WARNING, null, e);
|
log.log(Level.WARNING, null, e);
|
||||||
}
|
}
|
||||||
|
|
@ -166,7 +169,7 @@ public class Configurator {
|
||||||
/**
|
/**
|
||||||
* 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 #setConfiguration()} 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)
|
||||||
|
|
@ -181,7 +184,7 @@ public class Configurator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void set() throws IllegalAccessException {
|
protected void apply() throws IllegalAccessException {
|
||||||
field.set(obj, value);
|
field.set(obj, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue