package urban.transformers;
import org.apache.commons.collections15.Predicate;
import org.apache.commons.collections15.Transformer;
import urban.model.Site;
/**
* Multi-purpose site transformer.
*
* The class can be used to rename, change states or bindings or all three.
* The transformer uses a Predicate to determine whether or not to apply the transformation.
*/
public class SiteTransformer implements Transformer<Site,Site> {
private Predicate<Site> test;
private String newName;
private String newState;
private String newBindingMark;
private boolean isBindingSet;
/**
* @param test Only Sites that pass the test will be transformed
*/
public SiteTransformer(Predicate<Site> test) {
this.test = test;
this.newName = null;
}
/**
*
* @param test Only Sites that pass the test will be transformed
* @param newName The new name for transformed sites.
*/
public SiteTransformer(Predicate<Site> test, String newName) {
this.test = test;
this.newName = newName;
}
/**
* A test will need to be set before this class can be used to transform any site
*/
public SiteTransformer(){}
/**
* @return the new name to be given to transformed sites or null if not changing the name
*/
public String getNewName() {
return newName;
}
/**
* @param newName the new name, if null then the name will not be changed when transforming
* @return this
*/
public SiteTransformer setNewName(String newName) {
this.newName = newName;
return this;
}
/**
* @param newState the new state, if null then the state will not be changed when transforming
* @return this
*/
public SiteTransformer setNewState(String newState) {
this.newState = newState;
return this;
}
/**
* @param s the new binding mark, unlike the other settings null <b>will</b> be used then transforming
* @return this
*/
public SiteTransformer setNewBindingMark(String s) {
this.newBindingMark = s;
isBindingSet = true;
return this;
}
@Override
public Site transform(Site arg0) {
if (test.evaluate(arg0)){
return new Site(arg0.getAgent(),
newName == null ? arg0.getName() : newName,
newState == null ? arg0.getState() : newState,
!isBindingSet ? arg0.getBindingMark() : newBindingMark);
}
return arg0;
}
/**
* @param test The test which sites are checked against before transforming
*/
public void setTest(Predicate<Site> test) {
this.test = test;
}
}