changed generic handling

This commit is contained in:
2018-08-24 13:17:32 +02:00
parent a0cda5f498
commit 81cd34817b
3 changed files with 31 additions and 34 deletions

View File

@ -4,11 +4,9 @@ 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 * @author joern@muehlencord.de
*/ */
public interface Configuration<T, V extends T> { public interface Configuration {
/** /**
* adds a new parameter to the configuration * adds a new parameter to the configuration
@ -16,16 +14,17 @@ public interface Configuration<T, V extends T> {
* @param p the parameter to add * @param p the parameter to add
* @throws ConfigurationException if the parameter cannot be added * @throws ConfigurationException if the parameter cannot be added
*/ */
public void addParameter(Parameter<T> p) throws ConfigurationException; public <T> void addParameter(Parameter<T> p) throws ConfigurationException;
/** /**
* sets the value of the given parameter * sets the value of the given parameter
* *
* @param p parameter to set * @param <T>
* @param value value to set * @param p
* @param value
* @throws ConfigurationException if the parameter is not defined * @throws ConfigurationException if the parameter is not defined
*/ */
public void setParameterValue(Parameter<T> p, V value) throws ConfigurationException; public <T> void setParameterValue(Parameter<T> p, T value) throws ConfigurationException;
/** /**
* sets the value of parameter with given name * sets the value of parameter with given name
@ -34,7 +33,7 @@ public interface Configuration<T, V extends T> {
* @param value value to set * @param value value to set
* @throws ConfigurationException if the parameter is not defined * @throws ConfigurationException if the parameter is not defined
*/ */
public void setParameterValue(String parameterName, V value) throws ConfigurationException; public <T> void setParameterValue(String parameterName, T value) throws ConfigurationException;
/** /**
* sets the value of the given parameter * sets the value of the given parameter
@ -43,7 +42,7 @@ public interface Configuration<T, V extends T> {
* @param value value to set * @param value value to set
* @throws ConfigurationException if the parameter is not defined * @throws ConfigurationException if the parameter is not defined
*/ */
public void setParameterValueByString(Parameter<T> p, String value) throws ConfigurationException; public <T> void setParameterValueByString(Parameter<T> p, String value) throws ConfigurationException;
/** /**
* sets the value of the given parameter * sets the value of the given parameter
@ -62,7 +61,7 @@ public interface Configuration<T, V extends T> {
* *
* @throws ConfigurationException if the parameter is not defined or if the value is 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; public <T> T getParameterValue(Parameter<T> p) throws ConfigurationException;
/** /**
* returns the value of the given parameter * returns the value of the given parameter
@ -72,7 +71,7 @@ public interface Configuration<T, V extends T> {
* *
* @throws ConfigurationException if the parameter is not defined or if the value is not set * @throws ConfigurationException if the parameter is not defined or if the value is not set
*/ */
public V getParameterValue(String parameterName) throws ConfigurationException; public <T> T getParameterValue(String parameterName) throws ConfigurationException;
/** /**
* validates the configuration * validates the configuration
@ -86,5 +85,5 @@ public interface Configuration<T, V extends T> {
* *
* @return the map of parameters and values * @return the map of parameters and values
*/ */
public Map<Parameter<T>, V> getParameterMap(); public <T> Map<Parameter<T>, T> getParameterMap();
} }

View File

@ -7,18 +7,16 @@ 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 * @author joern@muehlencord.de
*/ */
public class DefaultConfiguration<T, V extends T> implements Configuration<T, V> { public class DefaultConfiguration implements Configuration {
/** /**
* the parameter map * the parameter map
*/ */
private final Map<Parameter<T>, V> parameterMap; private final Map<Parameter, Object> parameterMap;
/** mapping from name to parameter */ /** mapping from name to parameter */
private final Map<String, Parameter<T>> parameterNameMap; private final Map<String, Parameter> parameterNameMap;
/** /**
* creates a new instance of a configuration * creates a new instance of a configuration
@ -33,7 +31,7 @@ public class DefaultConfiguration<T, V extends T> implements Configuration<T, V>
* *
* @return the map of parameters and values * @return the map of parameters and values
*/ */
public Map<Parameter<T>, V> getParameterMap() { public Map<Parameter, Object> getParameterMap() {
return parameterMap; return parameterMap;
} }
@ -80,7 +78,7 @@ public class DefaultConfiguration<T, V extends T> implements Configuration<T, V>
* @throws ConfigurationException if the parameter is not defined * @throws ConfigurationException if the parameter is not defined
*/ */
@Override @Override
public void setParameterValue(Parameter<T> p, V value) throws ConfigurationException { public <T> void setParameterValue(Parameter<T> p, T value) throws ConfigurationException {
if (parameterMap.containsKey(p)) { if (parameterMap.containsKey(p)) {
parameterMap.put(p, value); parameterMap.put(p, value);
} else { } else {
@ -94,10 +92,10 @@ public class DefaultConfiguration<T, V extends T> implements Configuration<T, V>
* @param map map to use * @param map map to use
* @throws ConfigurationException if the parameter is not defined * @throws ConfigurationException if the parameter is not defined
*/ */
public void setParameterValue (Map<Parameter<T>, V> map) throws ConfigurationException { public <T> void setParameterValue (Map<Parameter<T>, T> map) throws ConfigurationException {
for ( Map.Entry<Parameter<T>,V> entry : map.entrySet()) { for ( Map.Entry<Parameter<T>,T> entry : map.entrySet()) {
Parameter<T> key = entry.getKey(); Parameter<T> key = entry.getKey();
V value = entry.getValue(); T value = entry.getValue();
if (parameterMap.containsKey(key)) { if (parameterMap.containsKey(key)) {
parameterMap.put (key, value); parameterMap.put (key, value);
} else { } else {
@ -114,7 +112,7 @@ public class DefaultConfiguration<T, V extends T> implements Configuration<T, V>
* @throws ConfigurationException if the parameter is not defined * @throws ConfigurationException if the parameter is not defined
*/ */
@Override @Override
public void setParameterValue(String parameterName, V value) throws ConfigurationException { public <T> void setParameterValue(String parameterName, T value) throws ConfigurationException {
if (parameterNameMap.containsKey(parameterName)) { if (parameterNameMap.containsKey(parameterName)) {
Parameter<T> p = parameterNameMap.get(parameterName); Parameter<T> p = parameterNameMap.get(parameterName);
parameterMap.put(p, value); parameterMap.put(p, value);
@ -132,10 +130,10 @@ public class DefaultConfiguration<T, V extends T> implements Configuration<T, V>
* @throws ConfigurationException if the parameter is not defined * @throws ConfigurationException if the parameter is not defined
*/ */
@Override @Override
public void setParameterValueByString(Parameter<T> p, String stringValue) throws ConfigurationException { public <T> void setParameterValueByString(Parameter<T> p, String stringValue) throws ConfigurationException {
if (parameterMap.containsKey(p)) { if (parameterMap.containsKey(p)) {
try { try {
V value = p.getStringConverter().fromString(stringValue); T value = p.getStringConverter().fromString(stringValue);
parameterMap.put(p, value); parameterMap.put(p, value);
} catch (ConverterException ex) { } catch (ConverterException ex) {
throw new ConfigurationException("Error while setting parameter value for parameter " + p.getName() + ". Reason:" + ex.getMessage(), ex); throw new ConfigurationException("Error while setting parameter value for parameter " + p.getName() + ". Reason:" + ex.getMessage(), ex);
@ -155,11 +153,11 @@ public class DefaultConfiguration<T, V extends T> implements Configuration<T, V>
@Override @Override
public void setParameterValueByString(String parameterName, String stringValue) throws ConfigurationException { public void setParameterValueByString(String parameterName, String stringValue) throws ConfigurationException {
if (parameterNameMap.containsKey(parameterName)) { if (parameterNameMap.containsKey(parameterName)) {
Parameter<T> p = parameterNameMap.get(parameterName); Parameter p = parameterNameMap.get(parameterName);
if (parameterMap.containsKey(p)) { if (parameterMap.containsKey(p)) {
try { try {
V value = p.getStringConverter().fromString(stringValue); Object value = p.getStringConverter().fromString(stringValue);
parameterMap.put(p, value); parameterMap.put(p, value);
} catch (ConverterException ex) { } catch (ConverterException ex) {
throw new ConfigurationException("Error while setting parameter value for parameter " + p.getName() + ". Reason:" + ex.getMessage(), ex); throw new ConfigurationException("Error while setting parameter value for parameter " + p.getName() + ". Reason:" + ex.getMessage(), ex);
@ -181,9 +179,9 @@ public class DefaultConfiguration<T, V extends T> implements Configuration<T, V>
* @throws ConfigurationException if the value cannot be determined * @throws ConfigurationException if the value cannot be determined
*/ */
@Override @Override
public V getParameterValue(Parameter<T> p) throws ConfigurationException { public <T> T getParameterValue(Parameter<T> p) throws ConfigurationException {
if (parameterMap.containsKey(p)) { if (parameterMap.containsKey(p)) {
return parameterMap.get(p); return (T) parameterMap.get(p);
} else { } else {
throw new ConfigurationException("Parameter " + p.getName() + " not defined"); throw new ConfigurationException("Parameter " + p.getName() + " not defined");
} }
@ -198,7 +196,7 @@ public class DefaultConfiguration<T, V extends T> implements Configuration<T, V>
* @throws ConfigurationException if the parameter is not defined or if the value is not set * @throws ConfigurationException if the parameter is not defined or if the value is not set
*/ */
@Override @Override
public V getParameterValue(String parameterName) throws ConfigurationException { public <T> T getParameterValue(String parameterName) throws ConfigurationException {
if (parameterNameMap.containsKey(parameterName)) { if (parameterNameMap.containsKey(parameterName)) {
Parameter<T> p = parameterNameMap.get(parameterName); Parameter<T> p = parameterNameMap.get(parameterName);
return getParameterValue(p); return getParameterValue(p);
@ -215,14 +213,14 @@ public class DefaultConfiguration<T, V extends T> implements Configuration<T, V>
* *
* @throws ConfigurationException if a check fails * @throws ConfigurationException if a check fails
*/ */
private boolean validateParameter(Parameter<T> p) throws ConfigurationException { private <T> boolean validateParameter(Parameter<T> p) throws ConfigurationException {
// check if parameter is mandatory and available // check if parameter is mandatory and available
if ((p.isMandatory()) && (parameterMap.get(p) == null)) { if ((p.isMandatory()) && (parameterMap.get(p) == null)) {
return false; return false;
} }
// check if parameter has required parameters and if these are set // check if parameter has required parameters and if these are set
V parameterValue = getParameterValue(p); T parameterValue = getParameterValue(p);
if (parameterValue != null) { if (parameterValue != null) {
for (Parameter rp : p.getRequiredParameter()) { for (Parameter rp : p.getRequiredParameter()) {
if (getParameterValue(rp) == null) { if (getParameterValue(rp) == null) {

View File

@ -25,7 +25,7 @@ public abstract class Parameter<T> {
/** the string converter to convert the object to string and back again */ /** the string converter to convert the object to string and back again */
private StringConverter<T> stringConverter; private StringConverter<T> stringConverter;
/** list of mandatory parameters, if this parameter is active */ /** list of mandatory parameters, if this parameter is active */
private List<Parameter<?>> requiredParameters; private List<Parameter<?>> requiredParameters; // TODO rename to depending parmeter
/** optional validator */ /** optional validator */
private Validator<T> validator; private Validator<T> validator;