first commit
This commit is contained in:
@ -0,0 +1,51 @@
|
||||
package de.muehlencord.shared.configuration;
|
||||
|
||||
import de.muehlencord.shared.configuration.converter.BooleanStringConverter;
|
||||
|
||||
/**
|
||||
* A Boolean parameter
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public class BooleanParameter extends Parameter<Boolean> {
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public BooleanParameter(String name, StringConverter<Boolean> converter) {
|
||||
super(name, converter, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public BooleanParameter(String name, boolean mandatory) {
|
||||
super(name, new BooleanStringConverter(), mandatory);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 mandatory detremines if this is a mandatory parameter or not
|
||||
*/
|
||||
public BooleanParameter(String name, StringConverter<Boolean> converter, boolean mandatory) {
|
||||
super(name, converter, mandatory);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,79 @@
|
||||
package de.muehlencord.shared.configuration;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jomu
|
||||
*/
|
||||
public interface Configuration<T, V extends T> {
|
||||
|
||||
/**
|
||||
* adds a new parameter to the configuration
|
||||
*
|
||||
* @param p the parameter to add
|
||||
* @throws ConfigurationException if the parameter cannot be added
|
||||
*/
|
||||
public void addParameter(Parameter<T> p) throws ConfigurationException;
|
||||
|
||||
/**
|
||||
* sets the value of the given parameter
|
||||
*
|
||||
* @param p parameter to set
|
||||
* @param value value to set
|
||||
* @throws ConfigurationException if the parameter is not defined
|
||||
*/
|
||||
public void setParameterValue(Parameter<T> p, V value) throws ConfigurationException;
|
||||
|
||||
/**
|
||||
* sets the value of parameter with given name
|
||||
*
|
||||
* @param parameterName parameter to set
|
||||
* @param value value to set
|
||||
* @throws ConfigurationException if the parameter is not defined
|
||||
*/
|
||||
public void setParameterValue(String parameterName, V value) throws ConfigurationException;
|
||||
|
||||
/**
|
||||
* sets the value of the given parameter
|
||||
*
|
||||
* @param p parameter to set
|
||||
* @param value value to set
|
||||
* @throws ConfigurationException if the parameter is not defined
|
||||
*/
|
||||
public void setParameterValueByString(Parameter<T> p, String value) throws ConfigurationException;
|
||||
|
||||
/**
|
||||
* sets the value of the given parameter
|
||||
*
|
||||
* @param parameterName the name of the parameter to set the value for
|
||||
* @param stringValue the value of the parameter as string
|
||||
* @throws ConfigurationException if the parameter cannot be found or set
|
||||
*/
|
||||
public void setParameterValueByString(String parameterName, String stringValue) throws ConfigurationException;
|
||||
|
||||
/**
|
||||
* returns the value of the given parameter
|
||||
*
|
||||
* @param p the parameter to return the value for
|
||||
* @return the value of the given parameter; null if not set
|
||||
*
|
||||
* @throws ConfigurationException if the parameter is not defined or if the value is not set
|
||||
*/
|
||||
public V getParameterValue(Parameter<T> p) throws ConfigurationException;
|
||||
|
||||
/**
|
||||
* returns the value of the given parameter
|
||||
*
|
||||
* @param parameterName the name of the parameter to return the value for
|
||||
* @return the value of the given parameter; null if not set
|
||||
*
|
||||
* @throws ConfigurationException if the parameter is not defined or if the value is not set
|
||||
*/
|
||||
public V getParameterValue(String parameterName) throws ConfigurationException;
|
||||
|
||||
/**
|
||||
* validates the configuration
|
||||
*
|
||||
* @throws ConfigurationException if the configuration is invalid
|
||||
*/
|
||||
public void validateConfiguration() throws ConfigurationException;
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
package de.muehlencord.shared.configuration;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jomu
|
||||
*/
|
||||
public class ConfigurationException extends Exception {
|
||||
|
||||
/**
|
||||
* Creates a new instance of
|
||||
* <code>ParameterException</code> without detail message.
|
||||
*/
|
||||
public ConfigurationException() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an instance of
|
||||
* <code>ParameterException</code> with the specified detail message.
|
||||
*
|
||||
* @param msg the detail message.
|
||||
*/
|
||||
public ConfigurationException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an instance of
|
||||
* <code>ParameterException</code> with the specified detail message.
|
||||
*
|
||||
* @param msg the detail message.
|
||||
* @param th the root cause
|
||||
*/
|
||||
public ConfigurationException(String msg, Throwable th) {
|
||||
super(msg, th);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
package de.muehlencord.shared.configuration;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jomu
|
||||
*/
|
||||
public class ConverterException extends Exception {
|
||||
|
||||
/**
|
||||
* Creates a new instance of
|
||||
* <code>ConverterException</code> without detail message.
|
||||
*/
|
||||
public ConverterException() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an instance of
|
||||
* <code>ConverterException</code> with the specified detail message.
|
||||
*
|
||||
* @param msg the detail message.
|
||||
*/
|
||||
public ConverterException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an instance of
|
||||
* <code>ConverterException</code> with the specified detail message.
|
||||
*
|
||||
* @param msg the detail message.
|
||||
* @param th the causing exception
|
||||
*/
|
||||
public ConverterException(String msg, Throwable th) {
|
||||
super(msg, th);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
package de.muehlencord.shared.configuration;
|
||||
|
||||
import de.muehlencord.shared.configuration.converter.DateStringConverter;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* A Date parameter
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public class DateParameter extends Parameter<Date> {
|
||||
|
||||
/**
|
||||
* creates a new mandatory parameter object using default string converter
|
||||
*
|
||||
* @param name the name of the parameter
|
||||
*/
|
||||
public DateParameter(String name) {
|
||||
super(name, new DateStringConverter(), 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
|
||||
*/
|
||||
public DateParameter(String name, StringConverter<Date> converter) {
|
||||
super(name, converter, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public DateParameter(String name, boolean mandatory) {
|
||||
super(name, new DateStringConverter(), mandatory);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 mandatory detremines if this is a mandatory parameter or not
|
||||
*/
|
||||
public DateParameter(String name, StringConverter<Date> converter, boolean mandatory) {
|
||||
super(name, converter, mandatory);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,224 @@
|
||||
package de.muehlencord.shared.configuration;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param <T> the type of the parameter
|
||||
* @param <V> the value of the parameter - must be the same class as the type class of the parameter
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public class DefaultConfiguration<T, V extends T> implements Configuration<T, V> {
|
||||
|
||||
/**
|
||||
* the parameter map
|
||||
*/
|
||||
private Map<Parameter<T>, V> parameterMap;
|
||||
/** mapping from name to parameter */
|
||||
private Map<String, Parameter<T>> parameterNameMap;
|
||||
|
||||
/**
|
||||
* creates a new instance of a configuration
|
||||
*/
|
||||
public DefaultConfiguration() {
|
||||
parameterMap = new HashMap<Parameter<T>, V>();
|
||||
parameterNameMap = new HashMap<String, Parameter<T>>();
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the map of parameters and values
|
||||
*
|
||||
* @return the map of parameters and values
|
||||
*/
|
||||
protected Map<Parameter<T>, V> getParameterMap() {
|
||||
return parameterMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a new parameter to the configuration
|
||||
*
|
||||
* @param p the parameter to add
|
||||
* @throws ConfigurationException if the parameter cannot be added
|
||||
*/
|
||||
@Override
|
||||
public void addParameter(Parameter p) throws ConfigurationException {
|
||||
if (parameterMap.containsKey(p)) {
|
||||
throw new ConfigurationException("Parameter named " + p.getName() + " already defined");
|
||||
} else {
|
||||
parameterMap.put(p, null);
|
||||
parameterNameMap.put(p.getName(), p);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* validates the configuration
|
||||
*
|
||||
* @throws ConfigurationException if the configuration is invalid
|
||||
*/
|
||||
@Override
|
||||
public void validateConfiguration() throws ConfigurationException {
|
||||
List<Parameter> missingMandatoryParameters = new LinkedList<Parameter>();
|
||||
for (Parameter p : parameterMap.keySet()) {
|
||||
if ((!validateParameter(p)) && (!missingMandatoryParameters.contains(p))) {
|
||||
missingMandatoryParameters.add(p);
|
||||
}
|
||||
}
|
||||
|
||||
if (!missingMandatoryParameters.isEmpty()) {
|
||||
throw new ConfigurationException("Configuration invalid, mandatory parameters missing: " + missingMandatoryParameters.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the value of the given parameter
|
||||
*
|
||||
* @param p parameter to set
|
||||
* @param value value to set
|
||||
* @throws ConfigurationException if the parameter is not defined
|
||||
*/
|
||||
@Override
|
||||
public void setParameterValue(Parameter<T> p, V value) throws ConfigurationException {
|
||||
if (parameterMap.containsKey(p)) {
|
||||
parameterMap.put(p, value);
|
||||
} else {
|
||||
throw new ConfigurationException("Parameter " + p.getName() + " not defined");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the value of parameter with given name
|
||||
*
|
||||
* @param parameterName parameter to set
|
||||
* @param value value to set
|
||||
* @throws ConfigurationException if the parameter is not defined
|
||||
*/
|
||||
@Override
|
||||
public void setParameterValue(String parameterName, V value) throws ConfigurationException {
|
||||
if (parameterNameMap.containsKey(parameterName)) {
|
||||
Parameter<T> p = parameterNameMap.get(parameterName);
|
||||
parameterMap.put(p, value);
|
||||
} else {
|
||||
throw new ConfigurationException("Parameter " + parameterName + " not defined");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the value of the given parameter
|
||||
*
|
||||
* @param p parameter to set
|
||||
* @param stringValue value to set
|
||||
* @throws ConfigurationException if the parameter is not defined
|
||||
*/
|
||||
@Override
|
||||
public void setParameterValueByString(Parameter<T> p, String stringValue) throws ConfigurationException {
|
||||
if (parameterMap.containsKey(p)) {
|
||||
try {
|
||||
V value = p.getStringConverter().fromString(stringValue);
|
||||
parameterMap.put(p, value);
|
||||
} catch (ConverterException ex) {
|
||||
throw new ConfigurationException("Error while setting parameter value for parameter " + p.getName() + ". Reason:" + ex.getMessage(), ex);
|
||||
}
|
||||
} else {
|
||||
throw new ConfigurationException("Parameter " + p.getName() + " not defined");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the value of the given parameter
|
||||
*
|
||||
* @param parameterName name of parameter to set the value for
|
||||
* @param stringValue value to set
|
||||
* @throws ConfigurationException if the parameter is not defined
|
||||
*/
|
||||
@Override
|
||||
public void setParameterValueByString(String parameterName, String stringValue) throws ConfigurationException {
|
||||
if (parameterNameMap.containsKey(parameterName)) {
|
||||
Parameter<T> p = parameterNameMap.get(parameterName);
|
||||
|
||||
if (parameterMap.containsKey(p)) {
|
||||
try {
|
||||
V value = p.getStringConverter().fromString(stringValue);
|
||||
parameterMap.put(p, value);
|
||||
} catch (ConverterException ex) {
|
||||
throw new ConfigurationException("Error while setting parameter value for parameter " + p.getName() + ". Reason:" + ex.getMessage(), ex);
|
||||
}
|
||||
} else {
|
||||
throw new ConfigurationException("Parameter " + p.getName() + " not defined");
|
||||
}
|
||||
} else {
|
||||
throw new ConfigurationException("Parameter with name " + parameterName + " not found in configuration");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the value of the given parameter
|
||||
*
|
||||
* @param p the parameter to return the value for
|
||||
* @return the value of the given parameter
|
||||
*
|
||||
* @throws ConfigurationException if the value cannot be determined
|
||||
*/
|
||||
@Override
|
||||
public V getParameterValue(Parameter<T> p) throws ConfigurationException {
|
||||
if (parameterMap.containsKey(p)) {
|
||||
return parameterMap.get(p);
|
||||
} else {
|
||||
throw new ConfigurationException("Parameter " + p.getName() + " not defined");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the value of the given parameter
|
||||
*
|
||||
* @param parameterName the name of the parameter to return the value for
|
||||
* @return the value of the given parameter; null if not set
|
||||
*
|
||||
* @throws ConfigurationException if the parameter is not defined or if the value is not set
|
||||
*/
|
||||
@Override
|
||||
public V getParameterValue(String parameterName) throws ConfigurationException {
|
||||
if (parameterNameMap.containsKey(parameterName)) {
|
||||
Parameter<T> p = parameterNameMap.get(parameterName);
|
||||
return getParameterValue(p);
|
||||
} else {
|
||||
throw new ConfigurationException("Parameter " + parameterName + " not defined");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* validates a parameter. Returns true, if the parameter setup is valid; false otherwise
|
||||
*
|
||||
* @param p the parameter to validate
|
||||
* @return true, if the parameter is valid (mandatory parameter set, depending parameters set); false otherwise
|
||||
*
|
||||
* @throws ConfigurationException if a check fails
|
||||
*/
|
||||
private boolean validateParameter(Parameter<T> p) throws ConfigurationException {
|
||||
// check if parameter is mandatory and available
|
||||
if ((p.isMandatory()) && (parameterMap.get(p) == null)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// check if parameter has required parameters and if these are set
|
||||
V parameterValue = getParameterValue(p);
|
||||
if (parameterValue != null) {
|
||||
for (Parameter rp : p.getRequiredParameter()) {
|
||||
if (getParameterValue(rp) == null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check if parameter has uses value list -if yes ensure only valid value is selected
|
||||
if (!p.isValid(parameterValue)) {
|
||||
throw new ConfigurationException("Value for paramter " + p.getName() + " is invalid.");
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,51 @@
|
||||
package de.muehlencord.shared.configuration;
|
||||
|
||||
import de.muehlencord.shared.configuration.converter.IntegerStringConverter;
|
||||
|
||||
/**
|
||||
* A Integer parameter
|
||||
*
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public class IntegerParameter extends Parameter<Integer> {
|
||||
|
||||
/**
|
||||
* creates a new mandatory parameter object using default string converter
|
||||
*
|
||||
* @param name the name of the parameter
|
||||
*/
|
||||
public IntegerParameter(String name) {
|
||||
super(name, new IntegerStringConverter(), 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
|
||||
*/
|
||||
public IntegerParameter(String name, StringConverter<Integer> converter) {
|
||||
super(name, converter, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public IntegerParameter(String name, boolean mandatory) {
|
||||
super(name, new IntegerStringConverter(), mandatory);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 mandatory detremines if this is a mandatory parameter or not
|
||||
*/
|
||||
public IntegerParameter(String name, StringConverter<Integer> converter, boolean mandatory) {
|
||||
super(name, converter, mandatory);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,168 @@
|
||||
package de.muehlencord.shared.configuration;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param <T> type of parameter
|
||||
* @author jomu
|
||||
*/
|
||||
public abstract class Parameter<T> {
|
||||
|
||||
/** 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 type of the value object for this paramterer */
|
||||
// private T type;
|
||||
/** 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;
|
||||
/** optional validator */
|
||||
private Validator<T> validator;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public Parameter(String name, StringConverter<T> converter) {
|
||||
this.name = name;
|
||||
this.description = name;
|
||||
this.mandatory = true;
|
||||
this.stringConverter = converter;
|
||||
this.requiredParameters = new LinkedList<Parameter<?>>();
|
||||
this.validator = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 mandatory detremines if this is a mandatory parameter or not
|
||||
*/
|
||||
public Parameter(String name, StringConverter<T> converter, boolean mandatory) {
|
||||
this(name, converter);
|
||||
this.mandatory = mandatory;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 description the long text description of this parameter
|
||||
* @param mandatory detremines if this is a mandatory parameter or not
|
||||
*/
|
||||
public Parameter(String name, StringConverter<T> converter, String description, boolean mandatory) {
|
||||
this(name, converter);
|
||||
this.description = description;
|
||||
this.mandatory = mandatory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the description
|
||||
*/
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the mandatory
|
||||
*/
|
||||
public boolean isMandatory() {
|
||||
return mandatory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the stringConverter
|
||||
*/
|
||||
public StringConverter<T> getStringConverter() {
|
||||
return stringConverter;
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a parameter to the list of required parameters
|
||||
*
|
||||
* @param param the parameter to add
|
||||
*/
|
||||
public void addRequiredParameter(Parameter param) {
|
||||
if (!requiredParameters.contains(param)) {
|
||||
requiredParameters.add(param);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the list of parameters the given parameter requires
|
||||
*
|
||||
* @return the list of parameters the given parameter requires
|
||||
*/
|
||||
public List<Parameter<?>> getRequiredParameter() {
|
||||
return requiredParameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public boolean isValid(T value) {
|
||||
if (validator == null) {
|
||||
return true;
|
||||
} else {
|
||||
try {
|
||||
validator.validate(value);
|
||||
return true;
|
||||
} catch (ValidationException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == null) {
|
||||
return false;
|
||||
}
|
||||
if (o instanceof Parameter) {
|
||||
Parameter param = (Parameter) o;
|
||||
return param.getName().equals(getName());
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 7;
|
||||
hash = 97 * hash + (this.name != null ? this.name.hashCode() : 0);
|
||||
return hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param validator the validator to set
|
||||
*/
|
||||
public void setValidator(Validator<T> validator) {
|
||||
this.validator = validator;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,49 @@
|
||||
package de.muehlencord.shared.configuration;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jomu
|
||||
*/
|
||||
public abstract class ParameterFactory {
|
||||
|
||||
private ParameterFactory() {
|
||||
// hide public constructor to force factory
|
||||
}
|
||||
|
||||
public static Parameter getParameterInstance(String name, boolean mandatory, Class clazz) throws ConfigurationException {
|
||||
if (clazz.equals(String.class)) {
|
||||
return new StringParameter(name, mandatory);
|
||||
|
||||
} else if (clazz.equals(Boolean.class)) {
|
||||
return new BooleanParameter(name, mandatory);
|
||||
|
||||
} else if (clazz.equals(Date.class)) {
|
||||
return new DateParameter(name, mandatory);
|
||||
|
||||
} else if (clazz.equals(Integer.class)) {
|
||||
return new IntegerParameter(name, mandatory);
|
||||
|
||||
} else if (clazz.equals(URI.class)) {
|
||||
return new URIParameter(name, mandatory);
|
||||
|
||||
|
||||
} else {
|
||||
throw new ConfigurationException("Unsupported type " + clazz);
|
||||
}
|
||||
}
|
||||
|
||||
public static Parameter getParameterInstance(String name, boolean mandatory, Object value) throws ConfigurationException {
|
||||
return getParameterInstance(name, mandatory, value.getClass());
|
||||
}
|
||||
|
||||
public static Parameter getParameterInstance(String name) throws ConfigurationException {
|
||||
return getParameterInstance(name, true, String.class);
|
||||
}
|
||||
|
||||
public static Parameter getParameterInstance(String name, boolean mandatory) throws ConfigurationException {
|
||||
return getParameterInstance(name, mandatory, String.class);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package de.muehlencord.shared.configuration;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param <T> the class type of the parameter value to convert
|
||||
* @author jomu
|
||||
*/
|
||||
public interface StringConverter<T> {
|
||||
|
||||
/**
|
||||
* Converter a parameter object to a string representation
|
||||
* @param o the parameter value to convert
|
||||
* @return the parameter value as string
|
||||
* @throws ConverterException if the conversion fails
|
||||
*/
|
||||
public String toString (T o) throws ConverterException;
|
||||
|
||||
/**
|
||||
* Converter a string to a the given type
|
||||
* @param <V> the type of the converter
|
||||
* @param s string representation of a parameter value to convert to an object
|
||||
* @return the parameter value as object
|
||||
* @throws ConverterException if the conversion fails
|
||||
*/
|
||||
public <V> V fromString (String s) throws ConverterException;
|
||||
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
package de.muehlencord.shared.configuration;
|
||||
|
||||
import de.muehlencord.shared.configuration.converter.StringStringConverter;
|
||||
|
||||
/**
|
||||
* A String parameter
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public class StringParameter extends Parameter<String> {
|
||||
|
||||
/**
|
||||
* creates a new mandatory parameter object using default string converter
|
||||
*
|
||||
* @param name the name of the parameter
|
||||
*/
|
||||
public StringParameter(String name) {
|
||||
super(name, new StringStringConverter(), 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
|
||||
*/
|
||||
public StringParameter(String name, StringConverter<String> converter) {
|
||||
super(name, converter, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public StringParameter(String name, boolean mandatory) {
|
||||
super(name, new StringStringConverter(), mandatory);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 mandatory detremines if this is a mandatory parameter or not
|
||||
*/
|
||||
public StringParameter(String name, StringConverter<String> converter, boolean mandatory) {
|
||||
super(name, converter, mandatory);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
package de.muehlencord.shared.configuration;
|
||||
|
||||
import de.muehlencord.shared.configuration.converter.URIStringConverter;
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
* A URI parameter
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public class URIParameter extends Parameter<URI> {
|
||||
|
||||
/**
|
||||
* creates a new mandatory parameter object using default string converter
|
||||
*
|
||||
* @param name the name of the parameter
|
||||
*/
|
||||
public URIParameter(String name) {
|
||||
super(name, new URIStringConverter(), 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
|
||||
*/
|
||||
public URIParameter(String name, StringConverter<URI> converter) {
|
||||
super(name, converter, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public URIParameter(String name, boolean mandatory) {
|
||||
super(name, new URIStringConverter(), mandatory);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 mandatory detremines if this is a mandatory parameter or not
|
||||
*/
|
||||
public URIParameter(String name, StringConverter<URI> converter, boolean mandatory) {
|
||||
super(name, converter, mandatory);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
package de.muehlencord.shared.configuration;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public class ValidationException extends Exception {
|
||||
|
||||
/**
|
||||
* Creates a new instance of
|
||||
* <code>ValidationException</code> without detail message.
|
||||
*/
|
||||
public ValidationException() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an instance of
|
||||
* <code>ValidationException</code> with the specified detail message.
|
||||
*
|
||||
* @param msg the detail message.
|
||||
*/
|
||||
public ValidationException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an instance of
|
||||
* <code>ValidationException</code> with the specified detail message.
|
||||
*
|
||||
* @param msg the detail message.
|
||||
* @param th the root cause
|
||||
*/
|
||||
public ValidationException(String msg, Throwable th) {
|
||||
super(msg, th);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package de.muehlencord.shared.configuration;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param <T> the type of the validator
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public interface Validator<T> {
|
||||
|
||||
/**
|
||||
* validates the parameter value
|
||||
*
|
||||
* @param value the value of the parameter to validate
|
||||
* @throws ValidationException if the value is not valid according to the validator configuration
|
||||
*/
|
||||
public void validate(T value) throws ValidationException;
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
package de.muehlencord.shared.configuration.converter;
|
||||
|
||||
import de.muehlencord.shared.configuration.ConverterException;
|
||||
import de.muehlencord.shared.configuration.StringConverter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public class BooleanStringConverter implements StringConverter<Boolean> {
|
||||
|
||||
@Override
|
||||
public String toString(Boolean o) throws ConverterException {
|
||||
return o.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean fromString(String s) throws ConverterException {
|
||||
return Boolean.parseBoolean(s);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,49 @@
|
||||
package de.muehlencord.shared.configuration.converter;
|
||||
|
||||
import de.muehlencord.shared.configuration.ConverterException;
|
||||
import de.muehlencord.shared.configuration.StringConverter;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jomu
|
||||
*/
|
||||
public class DateStringConverter implements StringConverter<Date> {
|
||||
|
||||
/**
|
||||
* dateformat to convert from string to date and vice versa
|
||||
*/
|
||||
private static final String format = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
/**
|
||||
* Converter a parameter object to a string representation
|
||||
*
|
||||
* @param d the parameter value to convert
|
||||
* @return the parameter value as string
|
||||
*/
|
||||
@Override
|
||||
public String toString(Date d) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
|
||||
return sdf.format(d);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converter a string to date
|
||||
*
|
||||
* @param s string representation of a parameter value to convert to an object
|
||||
* @return the parameter value as date
|
||||
*
|
||||
* @throws ConverterException if the conversion fails
|
||||
*/
|
||||
@Override
|
||||
public Date fromString(String s) throws ConverterException {
|
||||
try {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
|
||||
return sdf.parse(s);
|
||||
} catch (Exception ex) {
|
||||
throw new ConverterException("Error while converting " + s + "*to date. Reason: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
package de.muehlencord.shared.configuration.converter;
|
||||
|
||||
import de.muehlencord.shared.configuration.ConverterException;
|
||||
import de.muehlencord.shared.configuration.StringConverter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jomu
|
||||
*/
|
||||
public class IntegerStringConverter implements StringConverter<Integer> {
|
||||
|
||||
public String toString(Integer o) throws ConverterException {
|
||||
return o.toString();
|
||||
}
|
||||
|
||||
public Integer fromString(String s) throws ConverterException {
|
||||
return Integer.parseInt(s);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package de.muehlencord.shared.configuration.converter;
|
||||
|
||||
import de.muehlencord.shared.configuration.ConverterException;
|
||||
import de.muehlencord.shared.configuration.StringConverter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jomu
|
||||
*/
|
||||
public class StringStringConverter implements StringConverter<String> {
|
||||
|
||||
@Override
|
||||
public String toString(String o) throws ConverterException {
|
||||
return o;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String fromString(String s) throws ConverterException {
|
||||
return s;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package de.muehlencord.shared.configuration.converter;
|
||||
|
||||
import de.muehlencord.shared.configuration.ConverterException;
|
||||
import de.muehlencord.shared.configuration.StringConverter;
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jomu
|
||||
*/
|
||||
public class URIStringConverter implements StringConverter<URI> {
|
||||
|
||||
@Override
|
||||
public String toString(URI o) throws ConverterException {
|
||||
return o.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI fromString(String s) throws ConverterException {
|
||||
return URI.create(s);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package de.muehlencord.shared.configuration.validator;
|
||||
|
||||
import de.muehlencord.shared.configuration.ValidationException;
|
||||
import de.muehlencord.shared.configuration.Validator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public class IntgerRangeValidator implements Validator<Integer> {
|
||||
|
||||
private Integer lowerBoundary;
|
||||
private Integer upperBoundary;
|
||||
|
||||
public IntgerRangeValidator(Integer lb, Integer ub) {
|
||||
this.lowerBoundary = lb;
|
||||
this.upperBoundary = ub;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(Integer value) throws ValidationException {
|
||||
if ((value > upperBoundary) || (value < lowerBoundary)) {
|
||||
throw new ValidationException("Value must be between " + lowerBoundary + " and " + upperBoundary + " but is " + value);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
package de.muehlencord.shared.configuration.validator;
|
||||
|
||||
import de.muehlencord.shared.configuration.ValidationException;
|
||||
import de.muehlencord.shared.configuration.Validator;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern@muehlencord.de
|
||||
*/
|
||||
public class StringValueListValidator implements Validator<String> {
|
||||
|
||||
private List<String> valueList;
|
||||
|
||||
public StringValueListValidator() {
|
||||
this.valueList = new LinkedList<String>();
|
||||
}
|
||||
|
||||
public StringValueListValidator(String... values) {
|
||||
this();
|
||||
valueList.addAll(Arrays.asList(values));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(String value) throws ValidationException {
|
||||
if (!valueList.contains(value)) {
|
||||
throw new ValidationException("Value " + value + " not valid. One of " + valueList.toString() + " expected");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
package de.muehlencord.shared.configuration;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user