add valuelist support
This commit is contained in:
@ -4,29 +4,31 @@ import de.muehlencord.shared.configuration.converter.BooleanStringConverter;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* A Boolean parameter
|
* A Boolean parameter
|
||||||
|
*
|
||||||
* @author joern@muehlencord.de
|
* @author joern@muehlencord.de
|
||||||
*/
|
*/
|
||||||
public class BooleanParameter extends Parameter<Boolean> {
|
public class BooleanParameter extends Parameter<Boolean> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates a new mandatory parameter object using default string converter
|
* creates a new mandatory parameter object using default string converter
|
||||||
*
|
*
|
||||||
* @param name the name of the parameter
|
* @param name the name of the parameter
|
||||||
*/
|
*/
|
||||||
public BooleanParameter(String name) {
|
public BooleanParameter(String name) {
|
||||||
super(name, new BooleanStringConverter(), true);
|
this(name, new BooleanStringConverter(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates a new mandatory parameter object
|
* creates a new mandatory parameter object
|
||||||
*
|
*
|
||||||
* @param name the name of the parameter
|
* @param name the name of the parameter
|
||||||
* @param converter the converter object to convert the value of the parameter to string and vice versa
|
* @param converter the converter object to convert the value of the
|
||||||
|
* parameter to string and vice versa
|
||||||
*/
|
*/
|
||||||
public BooleanParameter(String name, StringConverter<Boolean> converter) {
|
public BooleanParameter(String name, StringConverter<Boolean> converter) {
|
||||||
super(name, converter, true);
|
this(name, converter, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates a new parameter object using default string converter
|
* creates a new parameter object using default string converter
|
||||||
*
|
*
|
||||||
@ -35,17 +37,43 @@ public class BooleanParameter extends Parameter<Boolean> {
|
|||||||
*/
|
*/
|
||||||
public BooleanParameter(String name, boolean mandatory) {
|
public BooleanParameter(String name, boolean mandatory) {
|
||||||
super(name, new BooleanStringConverter(), mandatory);
|
super(name, new BooleanStringConverter(), mandatory);
|
||||||
}
|
this.addValueList(Boolean.TRUE, Boolean.FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* creates a new parameter object using default string converter
|
||||||
|
*
|
||||||
|
* @param name the name of the parameter
|
||||||
|
* @param mandatory detremines if this is a mandatory parameter or not
|
||||||
|
* @param defaultValue the default value for this parameter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public BooleanParameter(String name, boolean mandatory, Boolean defaultValue) {
|
||||||
|
super(name, new BooleanStringConverter(), mandatory);
|
||||||
|
this.addValueList(Boolean.TRUE, Boolean.FALSE);
|
||||||
|
this.setDefaultValue(defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates a new parameter object
|
* creates a new parameter object
|
||||||
*
|
*
|
||||||
* @param name the name of the parameter
|
* @param name the name of the parameter
|
||||||
* @param converter the converter object to convert the value of the parameter to string and vice versa
|
* @param converter the converter object to convert the value of the
|
||||||
|
* parameter to string and vice versa
|
||||||
* @param mandatory detremines if this is a mandatory parameter or not
|
* @param mandatory detremines if this is a mandatory parameter or not
|
||||||
*/
|
*/
|
||||||
public BooleanParameter(String name, StringConverter<Boolean> converter, boolean mandatory) {
|
public BooleanParameter(String name, StringConverter<Boolean> converter, boolean mandatory) {
|
||||||
super(name, converter, mandatory);
|
super(name, converter, mandatory);
|
||||||
|
this.addValueList(Boolean.TRUE, Boolean.FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean getDefaultValue() {
|
||||||
|
if (defaultValue == null) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,39 +1,60 @@
|
|||||||
package de.muehlencord.shared.configuration;
|
package de.muehlencord.shared.configuration;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param <T> type of parameter
|
* @param <T> type of parameter
|
||||||
* @author jomu
|
* @author jomu
|
||||||
*/
|
*/
|
||||||
public abstract class Parameter<T> {
|
public abstract class Parameter<T> {
|
||||||
|
|
||||||
private final static Logger LOGGER = LoggerFactory.getLogger(Parameter.class);
|
|
||||||
|
|
||||||
/** the name of the parameter */
|
private final static Logger LOGGER = LoggerFactory.getLogger(Parameter.class);
|
||||||
private String name;
|
|
||||||
/** the long description of this parameter */
|
/**
|
||||||
private String description;
|
* the name of the parameter
|
||||||
/** boolean flag if this is a mandatory parameter or not */
|
*/
|
||||||
private boolean mandatory;
|
protected String name;
|
||||||
/** the string converter to convert the object to string and back again */
|
/**
|
||||||
private StringConverter<T> stringConverter;
|
* the long description of this parameter
|
||||||
/** list of mandatory parameters, if this parameter is active */
|
*/
|
||||||
private List<Parameter<?>> requiredParameters; // TODO rename to depending parmeter
|
protected String description;
|
||||||
/** optional validator */
|
/**
|
||||||
private Validator<T> validator;
|
* boolean flag if this is a mandatory parameter or not
|
||||||
|
*/
|
||||||
|
protected boolean mandatory;
|
||||||
|
/**
|
||||||
|
* the string converter to convert the object to string and back again
|
||||||
|
*/
|
||||||
|
protected StringConverter<T> stringConverter;
|
||||||
|
/**
|
||||||
|
* list of mandatory parameters, if this parameter is active
|
||||||
|
*/
|
||||||
|
protected List<Parameter<?>> requiredParameters; // TODO rename to depending parmeter
|
||||||
|
/**
|
||||||
|
* optional validator
|
||||||
|
*/
|
||||||
|
protected Validator<T> validator;
|
||||||
|
/**
|
||||||
|
* optional value list
|
||||||
|
*/
|
||||||
|
protected List<T> valueList;
|
||||||
|
/**
|
||||||
|
* the default value
|
||||||
|
*/
|
||||||
|
protected T defaultValue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates a new mandatory string parameter object
|
* creates a new mandatory string parameter object
|
||||||
*
|
*
|
||||||
* @param name the name of the parameter
|
* @param name the name of the parameter
|
||||||
* @param converter the converter object to convert the value of the parameter to string and vice versa
|
* @param converter the converter object to convert the value of the
|
||||||
|
* parameter to string and vice versa
|
||||||
*/
|
*/
|
||||||
public Parameter(String name, StringConverter<T> converter) {
|
public Parameter(String name, StringConverter<T> converter) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@ -42,13 +63,16 @@ public abstract class Parameter<T> {
|
|||||||
this.stringConverter = converter;
|
this.stringConverter = converter;
|
||||||
this.requiredParameters = new LinkedList<>();
|
this.requiredParameters = new LinkedList<>();
|
||||||
this.validator = null;
|
this.validator = null;
|
||||||
|
this.valueList = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates a new string parameter object. If the parameter is mandatory or not can be specified.
|
* creates a new string parameter object. If the parameter is mandatory or
|
||||||
|
* not can be specified.
|
||||||
*
|
*
|
||||||
* @param name the name of the parameter
|
* @param name the name of the parameter
|
||||||
* @param converter the converter object to convert the value of the parameter to string and vice versa
|
* @param converter the converter object to convert the value of the
|
||||||
|
* parameter to string and vice versa
|
||||||
* @param mandatory determines if this is a mandatory parameter or not
|
* @param mandatory determines if this is a mandatory parameter or not
|
||||||
*/
|
*/
|
||||||
public Parameter(String name, StringConverter<T> converter, boolean mandatory) {
|
public Parameter(String name, StringConverter<T> converter, boolean mandatory) {
|
||||||
@ -60,7 +84,8 @@ public abstract class Parameter<T> {
|
|||||||
* creates a new string parameter object.
|
* creates a new string parameter object.
|
||||||
*
|
*
|
||||||
* @param name the name of the parameter
|
* @param name the name of the parameter
|
||||||
* @param converter the converter object to convert the value of the parameter to string and vice versa
|
* @param converter the converter object to convert the value of the
|
||||||
|
* parameter to string and vice versa
|
||||||
* @param description the long text description of this parameter
|
* @param description the long text description of this parameter
|
||||||
* @param mandatory determines if this is a mandatory parameter or not
|
* @param mandatory determines if this is a mandatory parameter or not
|
||||||
*/
|
*/
|
||||||
@ -69,7 +94,7 @@ public abstract class Parameter<T> {
|
|||||||
this.description = description;
|
this.description = description;
|
||||||
this.mandatory = mandatory;
|
this.mandatory = mandatory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return name;
|
return name;
|
||||||
@ -95,7 +120,7 @@ public abstract class Parameter<T> {
|
|||||||
public boolean isMandatory() {
|
public boolean isMandatory() {
|
||||||
return mandatory;
|
return mandatory;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the stringConverter
|
* @return the stringConverter
|
||||||
*/
|
*/
|
||||||
@ -120,26 +145,62 @@ public abstract class Parameter<T> {
|
|||||||
* @return the list of parameters the given parameter requires
|
* @return the list of parameters the given parameter requires
|
||||||
*/
|
*/
|
||||||
public List<Parameter<?>> getRequiredParameter() {
|
public List<Parameter<?>> getRequiredParameter() {
|
||||||
return new ArrayList<>(requiredParameters);
|
return new ArrayList<>(requiredParameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns true, if the given value is part of the possible value list or if the given value list is true
|
* @param validator the validator to set
|
||||||
|
*/
|
||||||
|
public void setValidator(Validator<T> validator) {
|
||||||
|
this.validator = validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addValueList(T... values) {
|
||||||
|
if (valueList == null) {
|
||||||
|
valueList = new ArrayList<>();
|
||||||
|
}
|
||||||
|
valueList.addAll(Arrays.asList(values));
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<T> getValueList() {
|
||||||
|
List<T> returnList = new ArrayList<>();
|
||||||
|
if (valueList != null) {
|
||||||
|
returnList.addAll(valueList);
|
||||||
|
}
|
||||||
|
return returnList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getDefaultValue() {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDefaultValue(T defaultValue) {
|
||||||
|
this.defaultValue = defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns true, if the given value is part of the possible value list or if
|
||||||
|
* the given value list is true
|
||||||
*
|
*
|
||||||
* @param value the value to validate
|
* @param value the value to validate
|
||||||
* @return true if no value list is defined or value is part of the value list; false otherwise
|
* @return true if no value list is defined or value is part of the value
|
||||||
|
* list; false otherwise
|
||||||
*/
|
*/
|
||||||
public boolean isValid(T value) {
|
public boolean isValid(T value) {
|
||||||
if (validator == null) {
|
if (valueList == null) {
|
||||||
return true;
|
if (validator == null) {
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
validator.validate(value);
|
|
||||||
return true;
|
return true;
|
||||||
} catch (ValidationException ex) {
|
} else {
|
||||||
LOGGER.debug (ex.toString(), ex);
|
try {
|
||||||
return false;
|
validator.validate(value);
|
||||||
|
return true;
|
||||||
|
} catch (ValidationException ex) {
|
||||||
|
LOGGER.debug(ex.toString(), ex);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return valueList.contains(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,7 +209,7 @@ public abstract class Parameter<T> {
|
|||||||
if (o == null) {
|
if (o == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (o.getClass() == this.getClass()) {
|
if (o.getClass() == this.getClass()) {
|
||||||
Parameter param = (Parameter) o;
|
Parameter param = (Parameter) o;
|
||||||
return param.getName().equals(getName());
|
return param.getName().equals(getName());
|
||||||
} else {
|
} else {
|
||||||
@ -163,10 +224,4 @@ public abstract class Parameter<T> {
|
|||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param validator the validator to set
|
|
||||||
*/
|
|
||||||
public void setValidator(Validator<T> validator) {
|
|
||||||
this.validator = validator;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user