add valuelist support

This commit is contained in:
2018-09-19 19:01:06 +02:00
parent 8b2da09418
commit 530b6f500a
2 changed files with 132 additions and 49 deletions

View File

@ -4,6 +4,7 @@ import de.muehlencord.shared.configuration.converter.BooleanStringConverter;
/**
* A Boolean parameter
*
* @author joern@muehlencord.de
*/
public class BooleanParameter extends Parameter<Boolean> {
@ -14,17 +15,18 @@ public class BooleanParameter extends Parameter<Boolean> {
* @param name the name of the parameter
*/
public BooleanParameter(String name) {
super(name, new BooleanStringConverter(), true);
this(name, new BooleanStringConverter(), true);
}
/**
* creates a new mandatory parameter object
*
* @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) {
super(name, converter, true);
this(name, converter, true);
}
/**
@ -35,17 +37,43 @@ public class BooleanParameter extends Parameter<Boolean> {
*/
public BooleanParameter(String name, boolean 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
*
* @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
*/
public BooleanParameter(String name, StringConverter<Boolean> converter, boolean mandatory) {
super(name, converter, mandatory);
this.addValueList(Boolean.TRUE, Boolean.FALSE);
}
@Override
public Boolean getDefaultValue() {
if (defaultValue == null) {
return false;
} else {
return defaultValue;
}
}
}

View File

@ -1,12 +1,12 @@
package de.muehlencord.shared.configuration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @param <T> type of parameter
@ -16,24 +16,45 @@ public abstract class Parameter<T> {
private final static Logger LOGGER = LoggerFactory.getLogger(Parameter.class);
/** the name of the parameter */
private String name;
/** the long description of this parameter */
private String description;
/** boolean flag if this is a mandatory parameter or not */
private boolean mandatory;
/** the string converter to convert the object to string and back again */
private StringConverter<T> stringConverter;
/** list of mandatory parameters, if this parameter is active */
private List<Parameter<?>> requiredParameters; // TODO rename to depending parmeter
/** optional validator */
private Validator<T> validator;
/**
* the name of the parameter
*/
protected String name;
/**
* the long description of this parameter
*/
protected String description;
/**
* 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
*
* @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) {
this.name = name;
@ -42,13 +63,16 @@ public abstract class Parameter<T> {
this.stringConverter = converter;
this.requiredParameters = new LinkedList<>();
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 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
*/
public Parameter(String name, StringConverter<T> converter, boolean mandatory) {
@ -60,7 +84,8 @@ public abstract class Parameter<T> {
* creates a new string parameter object.
*
* @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 mandatory determines if this is a mandatory parameter or not
*/
@ -124,22 +149,58 @@ public abstract class Parameter<T> {
}
/**
* 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
* @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) {
if (validator == null) {
return true;
} else {
try {
validator.validate(value);
if (valueList == null) {
if (validator == null) {
return true;
} catch (ValidationException ex) {
LOGGER.debug (ex.toString(), ex);
return false;
} else {
try {
validator.validate(value);
return true;
} catch (ValidationException ex) {
LOGGER.debug(ex.toString(), ex);
return false;
}
}
} else {
return valueList.contains(value);
}
}
@ -163,10 +224,4 @@ public abstract class Parameter<T> {
return hash;
}
/**
* @param validator the validator to set
*/
public void setValidator(Validator<T> validator) {
this.validator = validator;
}
}