From 530b6f500a30d078cac589494556854a735279af Mon Sep 17 00:00:00 2001 From: jomu Date: Wed, 19 Sep 2018 19:01:06 +0200 Subject: [PATCH] add valuelist support --- .../configuration/BooleanParameter.java | 48 +++++-- .../shared/configuration/Parameter.java | 133 +++++++++++++----- 2 files changed, 132 insertions(+), 49 deletions(-) diff --git a/configuration/src/main/java/de/muehlencord/shared/configuration/BooleanParameter.java b/configuration/src/main/java/de/muehlencord/shared/configuration/BooleanParameter.java index 2e5f38f..186b470 100644 --- a/configuration/src/main/java/de/muehlencord/shared/configuration/BooleanParameter.java +++ b/configuration/src/main/java/de/muehlencord/shared/configuration/BooleanParameter.java @@ -4,29 +4,31 @@ import de.muehlencord.shared.configuration.converter.BooleanStringConverter; /** * A Boolean parameter + * * @author joern@muehlencord.de */ public class BooleanParameter extends Parameter { - + /** * creates a new mandatory parameter object using default string converter * * @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 converter) { - super(name, converter, true); - } - + this(name, converter, true); + } + /** * creates a new parameter object using default string converter * @@ -35,17 +37,43 @@ public class BooleanParameter extends Parameter { */ 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 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; + } + } + } diff --git a/configuration/src/main/java/de/muehlencord/shared/configuration/Parameter.java b/configuration/src/main/java/de/muehlencord/shared/configuration/Parameter.java index 98752d2..dc3e294 100644 --- a/configuration/src/main/java/de/muehlencord/shared/configuration/Parameter.java +++ b/configuration/src/main/java/de/muehlencord/shared/configuration/Parameter.java @@ -1,39 +1,60 @@ 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 type of parameter * @author jomu */ public abstract class Parameter { - - 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 stringConverter; - /** list of mandatory parameters, if this parameter is active */ - private List> requiredParameters; // TODO rename to depending parmeter - /** optional validator */ - private Validator validator; + private final static Logger LOGGER = LoggerFactory.getLogger(Parameter.class); + + /** + * 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 stringConverter; + /** + * list of mandatory parameters, if this parameter is active + */ + protected List> requiredParameters; // TODO rename to depending parmeter + /** + * optional validator + */ + protected Validator validator; + /** + * optional value list + */ + protected List 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 converter) { this.name = name; @@ -42,13 +63,16 @@ public abstract class Parameter { 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 converter, boolean mandatory) { @@ -60,7 +84,8 @@ public abstract class Parameter { * 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 */ @@ -69,7 +94,7 @@ public abstract class Parameter { this.description = description; this.mandatory = mandatory; } - + @Override public String toString() { return name; @@ -95,7 +120,7 @@ public abstract class Parameter { public boolean isMandatory() { return mandatory; } - + /** * @return the stringConverter */ @@ -120,26 +145,62 @@ public abstract class Parameter { * @return the list of parameters the given parameter requires */ public List> 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 validator) { + this.validator = validator; + } + + public void addValueList(T... values) { + if (valueList == null) { + valueList = new ArrayList<>(); + } + valueList.addAll(Arrays.asList(values)); + } + + public List getValueList() { + List 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); } } @@ -148,7 +209,7 @@ public abstract class Parameter { if (o == null) { return false; } - if (o.getClass() == this.getClass()) { + if (o.getClass() == this.getClass()) { Parameter param = (Parameter) o; return param.getName().equals(getName()); } else { @@ -163,10 +224,4 @@ public abstract class Parameter { return hash; } - /** - * @param validator the validator to set - */ - public void setValidator(Validator validator) { - this.validator = validator; - } }