Added pre configuration listener to Configurator

This commit is contained in:
Ziver Koc 2016-01-20 13:19:07 +01:00
parent de32d050af
commit df7d99ef8b
2 changed files with 35 additions and 6 deletions

BIN
Zutil.jar

Binary file not shown.

View file

@ -68,6 +68,8 @@ public class Configurator<T> {
private T obj; private T obj;
private ConfigurationParam[] params; private ConfigurationParam[] params;
private PreConfigurationActionListener<T> preListener;
private PostConfigurationActionListener<T> postListener;
public Configurator(T obj){ public Configurator(T obj){
this.obj = obj; this.obj = obj;
@ -141,13 +143,35 @@ public class Configurator<T> {
} }
/**
* Set a listener that will be called just before the configuration has been applied
*/
public void setPreConfigurationListener(PreConfigurationActionListener<T> listener){
preListener = listener;
}
/**
* Set a listener that will be called after the configuration has been applied
*/
public void setPostConfigurationListener(PostConfigurationActionListener<T> listener){
postListener = listener;
}
/** /**
* All configuration parameters that was set * All configuration parameters that was set
* for each parameter will be applied to the object. * for each parameter will be applied to the object.
* *
* The postConfigurationAction() method will be called on the target object if it implements the ConfigurationActionListener interface. * The preConfigurationAction() method will be called before the target object has
* been configured if it implements the PreConfigurationActionListener interface.
* The postConfigurationAction() method will be called after the target object is
* configured if it implements the PostConfigurationActionListener interface.
*/ */
public void applyConfiguration(){ public void applyConfiguration(){
if(preListener != null)
preListener.preConfigurationAction(this, obj);
if(obj instanceof PreConfigurationActionListener)
((PreConfigurationActionListener<T>) obj).preConfigurationAction(this, obj);
StringBuilder strParams = new StringBuilder(); StringBuilder strParams = new StringBuilder();
for(ConfigurationParam param : params){ for(ConfigurationParam param : params){
try { try {
@ -163,17 +187,22 @@ public class Configurator<T> {
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
logger.log(Level.WARNING, null, e); logger.log(Level.WARNING, null, e);
} }
if(obj instanceof ConfigurationActionListener)
((ConfigurationActionListener) obj).postConfigurationAction();
} }
if(logger.isLoggable(Level.FINE)) if(logger.isLoggable(Level.FINE))
logger.fine("Configured object: " + obj.getClass().getName() + " ("+ strParams +")"); logger.fine("Configured object: " + obj.getClass().getName() + " ("+ strParams +")");
if(obj instanceof PostConfigurationActionListener)
((PostConfigurationActionListener<T>) obj).postConfigurationAction(this, obj);
if(postListener != null)
postListener.postConfigurationAction(this, obj);
} }
public interface PreConfigurationActionListener<T> {
public interface ConfigurationActionListener{ void preConfigurationAction(Configurator<T> configurator, T obj);
void postConfigurationAction(); }
public interface PostConfigurationActionListener<T> {
void postConfigurationAction(Configurator<T> configurator, T obj);
} }