org.javabuilders.ValueListDefinition Maven / Gradle / Ivy
The newest version!
/**
*
*/
package org.javabuilders;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* Defines a list of items that can be passed to a property
* @author Jacek Furmankiewicz
*
*/
public class ValueListDefinition {
/**
* @param relatedTypes Related classes (used mostly for event handlers)
* @return Common event definitions
*/
public static List getCommonEventDefinitions(Class>...relatedTypes) {
List eventDefs = new ArrayList();
//defines the standard event listener definitions
ValueListDefinition vlDef = new ValueListDefinition();
ValueDefinition def = new ValueDefinition("methodName",EventMethod.class);
def.setVariableLength(true);
def.setRelatedTypes(relatedTypes);
vlDef.addValueDefinition(def);
eventDefs.add(vlDef);
return eventDefs;
}
private List valueDefs = new LinkedList();
/**
* Constructor for a list that has unlimited elements of one type
* @param type Value definitions
*/
public ValueListDefinition(ValueDefinition...valueDefs) {
this.valueDefs = new LinkedList(Arrays.asList(valueDefs));
validateValueDefinitions();
}
/**
* @param valueDef Value definition
* @return Same instance, for use in Builder pattern
*/
public ValueListDefinition addValueDefinition(ValueDefinition valueDef) {
valueDefs.add(0,valueDef);
validateValueDefinitions();
return this;
}
//validates if the value defs are valid
private void validateValueDefinitions() {
boolean noMoreAllowed = false;
//only the last value may be of variable length
for(ValueDefinition def : valueDefs) {
if (noMoreAllowed) {
throw new BuildException("Only the last value definition may have variable length");
} else if (def.isVariableLength()) {
noMoreAllowed = true;
}
}
}
/**
* Checks if the values are a potential match
* @param values Values
* @return true/false
*/
public boolean isPotentialMatch(List