From 749e1f3b0abe10a6ac098a31454fb98f5fd9bf36 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 10 Apr 2019 09:18:23 +0200 Subject: [PATCH] [Major] Refactored ListParameter hierarchy, adding abstract class, reducing methods --- .../src/main/java/li/strolch/model/Tags.java | 1 + .../parameter/AbstractListParameter.java | 210 ++++++++++++++++++ .../model/parameter/FloatListParameter.java | 135 +---------- .../model/parameter/IntegerListParameter.java | 136 +----------- .../model/parameter/ListParameter.java | 9 + .../model/parameter/LongListParameter.java | 137 +----------- .../model/parameter/StringListParameter.java | 135 +---------- 7 files changed, 256 insertions(+), 507 deletions(-) create mode 100644 li.strolch.model/src/main/java/li/strolch/model/parameter/AbstractListParameter.java diff --git a/li.strolch.model/src/main/java/li/strolch/model/Tags.java b/li.strolch.model/src/main/java/li/strolch/model/Tags.java index 15a9f19fa..585a1cf52 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/Tags.java +++ b/li.strolch.model/src/main/java/li/strolch/model/Tags.java @@ -131,6 +131,7 @@ public class Tags { public static final String MESSAGE = "message"; public static final String MESSAGES = "messages"; public static final String REALM = "realm"; + public static final String ROLES = "roles"; // miscellaneous diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/AbstractListParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/AbstractListParameter.java new file mode 100644 index 000000000..23aae99d1 --- /dev/null +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/AbstractListParameter.java @@ -0,0 +1,210 @@ +package li.strolch.model.parameter; + +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import li.strolch.exception.StrolchException; +import li.strolch.utils.helper.StringHelper; + +public abstract class AbstractListParameter extends AbstractParameter> implements ListParameter { + + protected List value; + + /** + * Empty constructor + */ + AbstractListParameter() { + // + } + + /** + * Default constructor + * + * @param id + * the id + * @param name + * the name + */ + AbstractListParameter(String id, String name) { + super(id, name); + } + + protected abstract void validateElement(E value); + + protected abstract String elementToString(E element); + + protected abstract List parseString(String valueAsString); + + /** + * Validates that the value is legal. This is the case when it is not null in this implementation + * + * @param values + * the values to check for this parameter instance + * + * @throws StrolchException + * if the value is null + */ + protected void validateValue(List values) throws StrolchException { + if (values == null) { + String msg = "Can not set null value on Parameter {0}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, getLocator()); + throw new StrolchException(msg); + } + + values.forEach(this::validateElement); + } + + /** + * Validates that the value is legal. This is the case when it is not null in this implementation + * + * @param values + * the values to check for this parameter instance + * + * @throws StrolchException + * if the value is null + */ + protected void validateValue(Collection values) throws StrolchException { + if (values == null) { + String msg = "Can not set null value on Parameter {0}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, getLocator()); + throw new StrolchException(msg); + } + + values.forEach(this::validateElement); + } + + @Override + public String getValueAsString() { + if (this.value.isEmpty()) { + return StringHelper.EMPTY; + } + + StringBuilder sb = new StringBuilder(); + Iterator iter = this.value.iterator(); + while (iter.hasNext()) { + + sb.append(elementToString(iter.next())); + + if (iter.hasNext()) { + sb.append(VALUE_SEPARATOR2); + sb.append(" "); + } + } + + return sb.toString(); + } + + @SuppressWarnings("unchecked") + @Override + public List getValue() { + return new ArrayList<>(this.value); + } + + @Override + public void setValue(List values) { + assertNotReadonly(); + validateValue(values); + this.value = new ArrayList<>(values); + } + + @Override + public void setValue(Collection values) { + assertNotReadonly(); + validateValue(values); + this.value = new ArrayList<>(values); + } + + @Override + public void setValueFrom(Parameter> parameter) { + assertNotReadonly(); + this.value = new ArrayList<>(parameter.getValue()); + } + + @Override + public void setValueFromString(String valueAsString) { + setValue(parseString(valueAsString)); + } + + @Override + public void addValue(E value) { + assertNotReadonly(); + validateElement(value); + this.value.add(value); + } + + @Override + public void addAllValues(List values) { + assertNotReadonly(); + for (E value : values) { + validateElement(value); + this.value.add(value); + } + } + + @Override + public void addAllValuesIfNotContains(List values) { + assertNotReadonly(); + for (E value : values) { + validateElement(value); + if (!this.value.contains(value)) + this.value.add(value); + } + } + + @Override + public boolean addValueIfNotContains(E value) { + assertNotReadonly(); + validateElement(value); + + if (this.value.contains(value)) + return false; + + this.value.add(value); + return true; + } + + @Override + public boolean removeValue(E value) { + assertNotReadonly(); + return this.value.remove(value); + } + + @Override + public void clear() { + assertNotReadonly(); + this.value.clear(); + } + + @Override + public boolean isEmpty() { + return this.value.isEmpty(); + } + + @Override + public boolean isEqualTo(Parameter> otherValue) { + return this.value.equals(otherValue.getValue()); + } + + @Override + public boolean isEqualTo(List otherValue) { + return this.value.equals(otherValue); + } + + @Override + public int size() { + return this.value.size(); + } + + @Override + public boolean contains(E value) { + return this.value.contains(value); + } + + @Override + public boolean containsAll(List values) { + return this.value.containsAll(values); + } +} diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/FloatListParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/FloatListParameter.java index 5c6976969..20d4c661a 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/FloatListParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/FloatListParameter.java @@ -17,20 +17,16 @@ package li.strolch.model.parameter; import java.util.ArrayList; import java.util.Collections; -import java.util.Iterator; import java.util.List; import li.strolch.model.StrolchValueType; import li.strolch.model.visitor.StrolchElementVisitor; import li.strolch.utils.dbc.DBC; -import li.strolch.utils.helper.StringHelper; /** * @author Robert von Burg */ -public class FloatListParameter extends AbstractParameter> implements ListParameter { - - protected List value; +public class FloatListParameter extends AbstractListParameter implements ListParameter { /** * Empty constructor @@ -56,131 +52,13 @@ public class FloatListParameter extends AbstractParameter> implemen } @Override - public String getValueAsString() { - if (this.value.isEmpty()) { - return StringHelper.EMPTY; - } - - StringBuilder sb = new StringBuilder(); - Iterator iter = this.value.iterator(); - while (iter.hasNext()) { - - sb.append(iter.next()); - - if (iter.hasNext()) { - sb.append(VALUE_SEPARATOR2); - sb.append(" "); - } - } - - return sb.toString(); - } - - @SuppressWarnings("unchecked") - @Override - public List getValue() { - return new ArrayList<>(this.value); + protected String elementToString(Double element) { + return element.toString(); } @Override - public void setValue(List values) { - assertNotReadonly(); - validateValue(values); - for (Double value : values) { - DBC.PRE.assertNotNull("null values not allowed!", value); - } - this.value = new ArrayList<>(values); - } - - @Override - public void setValueFrom(Parameter> parameter) { - assertNotReadonly(); - this.value = new ArrayList<>(parameter.getValue()); - } - - @Override - public void setValueFromString(String valueAsString) { - setValue(parseFromString(valueAsString)); - } - - @Override - public void addValue(Double value) { - assertNotReadonly(); + protected void validateElement(Double value) { DBC.PRE.assertNotNull("null values not allowed!", value); - this.value.add(value); - } - - @Override - public void addAllValues(List values) { - assertNotReadonly(); - for (Double value : values) { - DBC.PRE.assertNotNull("null values not allowed!", value); - this.value.add(value); - } - } - - @Override - public void addAllValuesIfNotContains(List values) { - assertNotReadonly(); - for (Double value : values) { - DBC.PRE.assertNotNull("null values not allowed!", value); - if (!this.value.contains(value)) - this.value.add(value); - } - } - - @Override - public boolean addValueIfNotContains(Double value) { - assertNotReadonly(); - DBC.PRE.assertNotNull("null values not allowed!", value); - - if (this.value.contains(value)) - return false; - - this.value.add(value); - return true; - } - - @Override - public boolean removeValue(Double value) { - assertNotReadonly(); - return this.value.remove(value); - } - - @Override - public void clear() { - assertNotReadonly(); - this.value.clear(); - } - - @Override - public boolean isEmpty() { - return this.value.isEmpty(); - } - - @Override - public boolean isEqualTo(Parameter> otherValue) { - return this.value.equals(otherValue.getValue()); - } - - @Override - public boolean isEqualTo(List otherValue) { - return this.value.equals(otherValue); - } - - @Override - public int size() { - return this.value.size(); - } - - @Override - public boolean contains(Double value) { - return this.value.contains(value); - } - - @Override - public boolean containsAll(List values) { - return this.value.containsAll(values); } @Override @@ -209,6 +87,11 @@ public class FloatListParameter extends AbstractParameter> implemen return visitor.visitFloatListParam(this); } + @Override + protected List parseString(String value) { + return parseFromString(value); + } + public static List parseFromString(String value) { if (value.isEmpty()) { return Collections.emptyList(); diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/IntegerListParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/IntegerListParameter.java index d5aad4337..69f1d4cd4 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/IntegerListParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/IntegerListParameter.java @@ -17,20 +17,16 @@ package li.strolch.model.parameter; import java.util.ArrayList; import java.util.Collections; -import java.util.Iterator; import java.util.List; import li.strolch.model.StrolchValueType; import li.strolch.model.visitor.StrolchElementVisitor; import li.strolch.utils.dbc.DBC; -import li.strolch.utils.helper.StringHelper; /** * @author Robert von Burg */ -public class IntegerListParameter extends AbstractParameter> implements ListParameter { - - protected List value; +public class IntegerListParameter extends AbstractListParameter implements ListParameter { /** * Empty constructor @@ -56,131 +52,13 @@ public class IntegerListParameter extends AbstractParameter> imple } @Override - public String getValueAsString() { - if (this.value.isEmpty()) { - return StringHelper.EMPTY; - } - - StringBuilder sb = new StringBuilder(); - Iterator iter = this.value.iterator(); - while (iter.hasNext()) { - - sb.append(iter.next()); - - if (iter.hasNext()) { - sb.append(VALUE_SEPARATOR2); - sb.append(" "); - } - } - - return sb.toString(); - } - - @SuppressWarnings("unchecked") - @Override - public List getValue() { - return new ArrayList<>(this.value); + protected String elementToString(Integer element) { + return element.toString(); } @Override - public void setValue(List values) { - assertNotReadonly(); - validateValue(values); - for (Integer value : values) { - DBC.PRE.assertNotNull("null values not allowed!", value); - } - this.value = new ArrayList<>(values); - } - - @Override - public void setValueFrom(Parameter> parameter) { - assertNotReadonly(); - this.value = new ArrayList<>(parameter.getValue()); - } - - @Override - public void setValueFromString(String valueAsString) { - setValue(parseFromString(valueAsString)); - } - - @Override - public void addValue(Integer value) { - assertNotReadonly(); + protected void validateElement(Integer value) { DBC.PRE.assertNotNull("null values not allowed!", value); - this.value.add(value); - } - - @Override - public void addAllValues(List values) { - assertNotReadonly(); - for (Integer value : values) { - DBC.PRE.assertNotNull("null values not allowed!", value); - this.value.add(value); - } - } - - @Override - public void addAllValuesIfNotContains(List values) { - assertNotReadonly(); - for (Integer value : values) { - DBC.PRE.assertNotNull("null values not allowed!", value); - if (!this.value.contains(value)) - this.value.add(value); - } - } - - @Override - public boolean addValueIfNotContains(Integer value) { - assertNotReadonly(); - DBC.PRE.assertNotNull("null values not allowed!", value); - - if (this.value.contains(value)) - return false; - - this.value.add(value); - return true; - } - - @Override - public boolean removeValue(Integer value) { - assertNotReadonly(); - return this.value.remove(value); - } - - @Override - public void clear() { - assertNotReadonly(); - this.value.clear(); - } - - @Override - public boolean isEmpty() { - return this.value.isEmpty(); - } - - @Override - public boolean isEqualTo(Parameter> otherValue) { - return this.value.equals(otherValue.getValue()); - } - - @Override - public boolean isEqualTo(List otherValue) { - return this.value.equals(otherValue); - } - - @Override - public int size() { - return this.value.size(); - } - - @Override - public boolean contains(Integer value) { - return this.value.contains(value); - } - - @Override - public boolean containsAll(List values) { - return this.value.containsAll(values); } @Override @@ -209,6 +87,11 @@ public class IntegerListParameter extends AbstractParameter> imple return visitor.visitIntegerListParam(this); } + @Override + protected List parseString(String value) { + return parseFromString(value); + } + public static List parseFromString(String value) { if (value.isEmpty()) { return Collections.emptyList(); @@ -232,5 +115,4 @@ public class IntegerListParameter extends AbstractParameter> imple DBC.PRE.assertEquals("Not same Parameter types!", this.getType(), o.getType()); return Integer.compare(this.getValue().size(), ((IntegerListParameter) o).getValue().size()); } - } diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/ListParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/ListParameter.java index 8f2a39a6a..bcc82a1ae 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/ListParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/ListParameter.java @@ -15,6 +15,7 @@ */ package li.strolch.model.parameter; +import java.util.Collection; import java.util.List; /** @@ -27,6 +28,14 @@ public interface ListParameter extends Parameter> { String VALUE_SEPARATOR1 = ";"; //$NON-NLS-1$ String VALUE_SEPARATOR2 = ","; //$NON-NLS-1$ + /** + * Set the internal value to have the content of the collection + * + * @param values + * the values to set + */ + void setValue(Collection values); + /** * Adds a single value to the {@link List} of values * diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/LongListParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/LongListParameter.java index 795870797..aeef5e0c5 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/LongListParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/LongListParameter.java @@ -17,20 +17,16 @@ package li.strolch.model.parameter; import java.util.ArrayList; import java.util.Collections; -import java.util.Iterator; import java.util.List; import li.strolch.model.StrolchValueType; import li.strolch.model.visitor.StrolchElementVisitor; import li.strolch.utils.dbc.DBC; -import li.strolch.utils.helper.StringHelper; /** * @author Robert von Burg */ -public class LongListParameter extends AbstractParameter> implements ListParameter { - - protected List value; +public class LongListParameter extends AbstractListParameter implements ListParameter { /** * Empty constructor @@ -56,132 +52,13 @@ public class LongListParameter extends AbstractParameter> implements } @Override - public String getValueAsString() { - if (this.value.isEmpty()) { - return StringHelper.EMPTY; - } - - StringBuilder sb = new StringBuilder(); - Iterator iter = this.value.iterator(); - while (iter.hasNext()) { - - sb.append(iter.next()); - - if (iter.hasNext()) { - sb.append(VALUE_SEPARATOR2); - sb.append(" "); - } - } - - return sb.toString(); - } - - @SuppressWarnings("unchecked") - @Override - public List getValue() { - return new ArrayList<>(this.value); + protected String elementToString(Long element) { + return element.toString(); } @Override - public void setValue(List values) { - assertNotReadonly(); - validateValue(values); - for (Long value : values) { - DBC.PRE.assertNotNull("null values not allowed!", value); - } - this.value = new ArrayList<>(values); - } - - @Override - public void setValueFrom(Parameter> parameter) { - assertNotReadonly(); - this.value = new ArrayList<>(parameter.getValue()); - } - - @Override - public void setValueFromString(String valueAsString) { - setValue(parseFromString(valueAsString)); - } - - @Override - public void addValue(Long value) { - assertNotReadonly(); + protected void validateElement(Long value) { DBC.PRE.assertNotNull("null values not allowed!", value); - this.value.add(value); - } - - @Override - public void addAllValues(List values) { - assertNotReadonly(); - for (Long value : values) { - DBC.PRE.assertNotNull("null values not allowed!", value); - this.value.add(value); - } - this.value.addAll(values); - } - - @Override - public void addAllValuesIfNotContains(List values) { - assertNotReadonly(); - for (Long value : values) { - DBC.PRE.assertNotNull("null values not allowed!", value); - if (!this.value.contains(value)) - this.value.add(value); - } - } - - @Override - public boolean addValueIfNotContains(Long value) { - assertNotReadonly(); - DBC.PRE.assertNotNull("null values not allowed!", value); - - if (this.value.contains(value)) - return false; - - this.value.add(value); - return true; - } - - @Override - public boolean removeValue(Long value) { - assertNotReadonly(); - return this.value.remove(value); - } - - @Override - public void clear() { - assertNotReadonly(); - this.value.clear(); - } - - @Override - public boolean isEmpty() { - return this.value.isEmpty(); - } - - @Override - public boolean isEqualTo(Parameter> otherValue) { - return this.value.equals(otherValue.getValue()); - } - - @Override - public boolean isEqualTo(List otherValue) { - return this.value.equals(otherValue); - } - - @Override - public int size() { - return this.value.size(); - } - - @Override - public boolean contains(Long value) { - return this.value.contains(value); - } - - @Override - public boolean containsAll(List values) { - return this.value.containsAll(values); } @Override @@ -210,6 +87,11 @@ public class LongListParameter extends AbstractParameter> implements return visitor.visitLongListParam(this); } + @Override + protected List parseString(String value) { + return parseFromString(value); + } + public static List parseFromString(String value) { if (value.isEmpty()) { return Collections.emptyList(); @@ -233,5 +115,4 @@ public class LongListParameter extends AbstractParameter> implements DBC.PRE.assertEquals("Not same Parameter types!", this.getType(), o.getType()); return Integer.compare(this.getValue().size(), ((LongListParameter) o).getValue().size()); } - } diff --git a/li.strolch.model/src/main/java/li/strolch/model/parameter/StringListParameter.java b/li.strolch.model/src/main/java/li/strolch/model/parameter/StringListParameter.java index 9e0eebc7f..07bf515dd 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/parameter/StringListParameter.java +++ b/li.strolch.model/src/main/java/li/strolch/model/parameter/StringListParameter.java @@ -17,20 +17,16 @@ package li.strolch.model.parameter; import java.util.ArrayList; import java.util.Collections; -import java.util.Iterator; import java.util.List; import li.strolch.model.StrolchValueType; import li.strolch.model.visitor.StrolchElementVisitor; import li.strolch.utils.dbc.DBC; -import li.strolch.utils.helper.StringHelper; /** * @author Robert von Burg */ -public class StringListParameter extends AbstractParameter> implements ListParameter { - - protected List value; +public class StringListParameter extends AbstractListParameter implements ListParameter { /** * Empty constructor @@ -56,131 +52,13 @@ public class StringListParameter extends AbstractParameter> impleme } @Override - public String getValueAsString() { - if (this.value.isEmpty()) { - return StringHelper.EMPTY; - } - - StringBuilder sb = new StringBuilder(); - Iterator iter = this.value.iterator(); - while (iter.hasNext()) { - - sb.append(iter.next()); - - if (iter.hasNext()) { - sb.append(VALUE_SEPARATOR2); - sb.append(" "); - } - } - - return sb.toString(); - } - - @SuppressWarnings("unchecked") - @Override - public List getValue() { - return new ArrayList<>(this.value); + protected String elementToString(String element) { + return element; } @Override - public void setValue(List values) { - assertNotReadonly(); - validateValue(values); - for (String value : values) { - DBC.PRE.assertNotEmpty("empty values not allowed!", value); - } - this.value = new ArrayList<>(values); - } - - @Override - public void setValueFrom(Parameter> parameter) { - assertNotReadonly(); - this.value = new ArrayList<>(parameter.getValue()); - } - - @Override - public void setValueFromString(String valueAsString) { - setValue(parseFromString(valueAsString)); - } - - @Override - public void addValue(String value) { - assertNotReadonly(); + protected void validateElement(String value) { DBC.PRE.assertNotEmpty("empty values not allowed!", value); - this.value.add(value); - } - - @Override - public void addAllValues(List values) { - assertNotReadonly(); - for (String value : values) { - DBC.PRE.assertNotEmpty("empty values not allowed!", value); - this.value.add(value); - } - } - - @Override - public void addAllValuesIfNotContains(List values) { - assertNotReadonly(); - for (String value : values) { - DBC.PRE.assertNotEmpty("empty values not allowed!", value); - if (!this.value.contains(value)) - this.value.add(value); - } - } - - @Override - public boolean addValueIfNotContains(String value) { - assertNotReadonly(); - DBC.PRE.assertNotEmpty("empty values not allowed!", value); - - if (this.value.contains(value)) - return false; - - this.value.add(value); - return true; - } - - @Override - public boolean removeValue(String value) { - assertNotReadonly(); - return this.value.remove(value); - } - - @Override - public void clear() { - assertNotReadonly(); - this.value.clear(); - } - - @Override - public boolean isEmpty() { - return this.value.isEmpty(); - } - - @Override - public boolean isEqualTo(Parameter> otherValue) { - return this.value.equals(otherValue.getValue()); - } - - @Override - public boolean isEqualTo(List otherValue) { - return this.value.equals(otherValue); - } - - @Override - public int size() { - return this.value.size(); - } - - @Override - public boolean contains(String value) { - return this.value.contains(value); - } - - @Override - public boolean containsAll(List values) { - return this.value.containsAll(values); } @Override @@ -209,6 +87,11 @@ public class StringListParameter extends AbstractParameter> impleme return visitor.visitStringListParam(this); } + @Override + protected List parseString(String value) { + return parseFromString(value); + } + public static List parseFromString(String value) { if (value.isEmpty()) { return Collections.emptyList();