[Major] Refactored ListParameter hierarchy, adding abstract class, reducing methods

This commit is contained in:
Robert von Burg 2019-04-10 09:18:23 +02:00
parent 30a2f38732
commit 749e1f3b0a
7 changed files with 256 additions and 507 deletions

View File

@ -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

View File

@ -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<E> extends AbstractParameter<List<E>> implements ListParameter<E> {
protected List<E> 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<E> 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<E> 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<E> 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<E> 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<E> getValue() {
return new ArrayList<>(this.value);
}
@Override
public void setValue(List<E> values) {
assertNotReadonly();
validateValue(values);
this.value = new ArrayList<>(values);
}
@Override
public void setValue(Collection<E> values) {
assertNotReadonly();
validateValue(values);
this.value = new ArrayList<>(values);
}
@Override
public void setValueFrom(Parameter<List<E>> 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<E> values) {
assertNotReadonly();
for (E value : values) {
validateElement(value);
this.value.add(value);
}
}
@Override
public void addAllValuesIfNotContains(List<E> 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<List<E>> otherValue) {
return this.value.equals(otherValue.getValue());
}
@Override
public boolean isEqualTo(List<E> 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<E> values) {
return this.value.containsAll(values);
}
}

View File

@ -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 <eitch@eitchnet.ch>
*/
public class FloatListParameter extends AbstractParameter<List<Double>> implements ListParameter<Double> {
protected List<Double> value;
public class FloatListParameter extends AbstractListParameter<Double> implements ListParameter<Double> {
/**
* Empty constructor
@ -56,131 +52,13 @@ public class FloatListParameter extends AbstractParameter<List<Double>> implemen
}
@Override
public String getValueAsString() {
if (this.value.isEmpty()) {
return StringHelper.EMPTY;
}
StringBuilder sb = new StringBuilder();
Iterator<Double> 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<Double> getValue() {
return new ArrayList<>(this.value);
protected String elementToString(Double element) {
return element.toString();
}
@Override
public void setValue(List<Double> 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<List<Double>> 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<Double> values) {
assertNotReadonly();
for (Double value : values) {
DBC.PRE.assertNotNull("null values not allowed!", value);
this.value.add(value);
}
}
@Override
public void addAllValuesIfNotContains(List<Double> 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<List<Double>> otherValue) {
return this.value.equals(otherValue.getValue());
}
@Override
public boolean isEqualTo(List<Double> 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<Double> values) {
return this.value.containsAll(values);
}
@Override
@ -209,6 +87,11 @@ public class FloatListParameter extends AbstractParameter<List<Double>> implemen
return visitor.visitFloatListParam(this);
}
@Override
protected List<Double> parseString(String value) {
return parseFromString(value);
}
public static List<Double> parseFromString(String value) {
if (value.isEmpty()) {
return Collections.emptyList();

View File

@ -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 <eitch@eitchnet.ch>
*/
public class IntegerListParameter extends AbstractParameter<List<Integer>> implements ListParameter<Integer> {
protected List<Integer> value;
public class IntegerListParameter extends AbstractListParameter<Integer> implements ListParameter<Integer> {
/**
* Empty constructor
@ -56,131 +52,13 @@ public class IntegerListParameter extends AbstractParameter<List<Integer>> imple
}
@Override
public String getValueAsString() {
if (this.value.isEmpty()) {
return StringHelper.EMPTY;
}
StringBuilder sb = new StringBuilder();
Iterator<Integer> 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<Integer> getValue() {
return new ArrayList<>(this.value);
protected String elementToString(Integer element) {
return element.toString();
}
@Override
public void setValue(List<Integer> 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<List<Integer>> 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<Integer> values) {
assertNotReadonly();
for (Integer value : values) {
DBC.PRE.assertNotNull("null values not allowed!", value);
this.value.add(value);
}
}
@Override
public void addAllValuesIfNotContains(List<Integer> 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<List<Integer>> otherValue) {
return this.value.equals(otherValue.getValue());
}
@Override
public boolean isEqualTo(List<Integer> 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<Integer> values) {
return this.value.containsAll(values);
}
@Override
@ -209,6 +87,11 @@ public class IntegerListParameter extends AbstractParameter<List<Integer>> imple
return visitor.visitIntegerListParam(this);
}
@Override
protected List<Integer> parseString(String value) {
return parseFromString(value);
}
public static List<Integer> parseFromString(String value) {
if (value.isEmpty()) {
return Collections.emptyList();
@ -232,5 +115,4 @@ public class IntegerListParameter extends AbstractParameter<List<Integer>> imple
DBC.PRE.assertEquals("Not same Parameter types!", this.getType(), o.getType());
return Integer.compare(this.getValue().size(), ((IntegerListParameter) o).getValue().size());
}
}

View File

@ -15,6 +15,7 @@
*/
package li.strolch.model.parameter;
import java.util.Collection;
import java.util.List;
/**
@ -27,6 +28,14 @@ public interface ListParameter<E> extends Parameter<List<E>> {
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<E> values);
/**
* Adds a single value to the {@link List} of values
*

View File

@ -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 <eitch@eitchnet.ch>
*/
public class LongListParameter extends AbstractParameter<List<Long>> implements ListParameter<Long> {
protected List<Long> value;
public class LongListParameter extends AbstractListParameter<Long> implements ListParameter<Long> {
/**
* Empty constructor
@ -56,132 +52,13 @@ public class LongListParameter extends AbstractParameter<List<Long>> implements
}
@Override
public String getValueAsString() {
if (this.value.isEmpty()) {
return StringHelper.EMPTY;
}
StringBuilder sb = new StringBuilder();
Iterator<Long> 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<Long> getValue() {
return new ArrayList<>(this.value);
protected String elementToString(Long element) {
return element.toString();
}
@Override
public void setValue(List<Long> 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<List<Long>> 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<Long> 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<Long> 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<List<Long>> otherValue) {
return this.value.equals(otherValue.getValue());
}
@Override
public boolean isEqualTo(List<Long> 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<Long> values) {
return this.value.containsAll(values);
}
@Override
@ -210,6 +87,11 @@ public class LongListParameter extends AbstractParameter<List<Long>> implements
return visitor.visitLongListParam(this);
}
@Override
protected List<Long> parseString(String value) {
return parseFromString(value);
}
public static List<Long> parseFromString(String value) {
if (value.isEmpty()) {
return Collections.emptyList();
@ -233,5 +115,4 @@ public class LongListParameter extends AbstractParameter<List<Long>> implements
DBC.PRE.assertEquals("Not same Parameter types!", this.getType(), o.getType());
return Integer.compare(this.getValue().size(), ((LongListParameter) o).getValue().size());
}
}

View File

@ -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 <eitch@eitchnet.ch>
*/
public class StringListParameter extends AbstractParameter<List<String>> implements ListParameter<String> {
protected List<String> value;
public class StringListParameter extends AbstractListParameter<String> implements ListParameter<String> {
/**
* Empty constructor
@ -56,131 +52,13 @@ public class StringListParameter extends AbstractParameter<List<String>> impleme
}
@Override
public String getValueAsString() {
if (this.value.isEmpty()) {
return StringHelper.EMPTY;
}
StringBuilder sb = new StringBuilder();
Iterator<String> 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<String> getValue() {
return new ArrayList<>(this.value);
protected String elementToString(String element) {
return element;
}
@Override
public void setValue(List<String> 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<List<String>> 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<String> values) {
assertNotReadonly();
for (String value : values) {
DBC.PRE.assertNotEmpty("empty values not allowed!", value);
this.value.add(value);
}
}
@Override
public void addAllValuesIfNotContains(List<String> 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<List<String>> otherValue) {
return this.value.equals(otherValue.getValue());
}
@Override
public boolean isEqualTo(List<String> 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<String> values) {
return this.value.containsAll(values);
}
@Override
@ -209,6 +87,11 @@ public class StringListParameter extends AbstractParameter<List<String>> impleme
return visitor.visitStringListParam(this);
}
@Override
protected List<String> parseString(String value) {
return parseFromString(value);
}
public static List<String> parseFromString(String value) {
if (value.isEmpty()) {
return Collections.emptyList();