From daa41ed992f70362e0464ee49921ca879c7f2b8c Mon Sep 17 00:00:00 2001 From: Mateusz Warowny Date: Fri, 25 Mar 2022 14:23:58 +0100 Subject: [PATCH] JAL-3878 Add builders to each service parameter class --- src/jalview/ws/params/simple/BooleanOption.java | 53 ++++++++++ src/jalview/ws/params/simple/DoubleParameter.java | 88 +++++++++++++++- src/jalview/ws/params/simple/FileParameter.java | 20 +++- src/jalview/ws/params/simple/IntegerParameter.java | 80 +++++++++++++- .../ws/params/simple/LogarithmicParameter.java | 94 ++++++++++++++++- src/jalview/ws/params/simple/Option.java | 109 +++++++++++++++++++- .../ws/params/simple/RadioChoiceParameter.java | 20 +++- src/jalview/ws/params/simple/StringParameter.java | 33 ++++-- 8 files changed, 470 insertions(+), 27 deletions(-) diff --git a/src/jalview/ws/params/simple/BooleanOption.java b/src/jalview/ws/params/simple/BooleanOption.java index df17296..cd492cf 100644 --- a/src/jalview/ws/params/simple/BooleanOption.java +++ b/src/jalview/ws/params/simple/BooleanOption.java @@ -22,9 +22,62 @@ package jalview.ws.params.simple; import java.net.URL; import java.util.Arrays; +import java.util.List; + +import static java.util.Objects.requireNonNullElse; public class BooleanOption extends Option { + public static class Builder extends Option.Builder + { + private boolean defaultValue = false; + + private boolean value = false; + + private String reprValue = null; + + public void setDefaultValue(Boolean defaultValue) + { + this.defaultValue = requireNonNullElse(defaultValue, false); + } + + public void setValue(Boolean value) + { + this.value = requireNonNullElse(value, false); + } + + public void setReprValue(String reprValue) + { + this.reprValue = reprValue; + } + + @Override + public void setPossibleValues(List possibleValues) + { + throw new UnsupportedOperationException("cannot set possible values for boolean"); + } + + @Override + public BooleanOption build() + { + return new BooleanOption(this); + } + } + + public static Builder newBuilder() + { + return new Builder(); + } + + protected BooleanOption(Builder builder) + { + super(builder); + String reprValue = requireNonNullElse(builder.reprValue, name); + defvalue = builder.defaultValue ? reprValue : null; + value = builder.value ? reprValue : null; + possibleVals = List.of(reprValue); + displayVals = List.of(label); + } public BooleanOption(String name, String descr, boolean required, Boolean defVal, Boolean val, URL link) diff --git a/src/jalview/ws/params/simple/DoubleParameter.java b/src/jalview/ws/params/simple/DoubleParameter.java index 6b76170..68029a3 100644 --- a/src/jalview/ws/params/simple/DoubleParameter.java +++ b/src/jalview/ws/params/simple/DoubleParameter.java @@ -2,6 +2,7 @@ package jalview.ws.params.simple; import jalview.ws.params.ParameterI; import jalview.ws.params.ValueConstrainI; +import static java.util.Objects.requireNonNullElse; /** * @@ -10,6 +11,76 @@ import jalview.ws.params.ValueConstrainI; */ public class DoubleParameter extends Option implements ParameterI { + public static class Builder extends Option.Builder + { + // setting them the opposite way disables limits until both are set. + protected double min = Double.POSITIVE_INFINITY; + + protected double max = Double.NEGATIVE_INFINITY; + + /** + * Setting string on double parameter is not allowed, use + * {@link #setValue(Double)} instead. + */ + @Override + public void setValue(String value) + { + throw new UnsupportedOperationException(); + } + + public void setValue(Double value) + { + if (value != null) + super.setValue(value.toString()); + else + super.setValue(null); + } + + /** + * Setting string on double parameter is not allowed, use + * {@link #setDefaultValue(Double)} instead. + */ + @Override + public void setDefaultValue(String defaultValue) + { + throw new UnsupportedOperationException(); + } + + public void setDefaultValue(Double defaultValue) + { + if (defaultValue != null) + super.setDefaultValue(defaultValue.toString()); + else + super.setDefaultValue(null); + } + + public void setMin(Double min) + { + this.min = requireNonNullElse(min, Double.POSITIVE_INFINITY); + } + + public void setMax(Double max) + { + this.max = requireNonNullElse(max, Double.NEGATIVE_INFINITY); + } + + public void setBounds(Double min, Double max) + { + setMin(min); + setMax(max); + } + + public DoubleParameter build() + { + return new DoubleParameter(this); + } + } + + public static Builder newBuilder() + { + return new Builder(); + } + double defval; double min; @@ -41,6 +112,15 @@ public class DoubleParameter extends Option implements ParameterI }; } + protected DoubleParameter(Builder builder) + { + super(builder); + this.min = builder.min; + this.max = builder.max; + if (defvalue != null) + defval = Double.parseDouble(defvalue); + } + public DoubleParameter(DoubleParameter parm) { super(parm); @@ -49,20 +129,20 @@ public class DoubleParameter extends Option implements ParameterI } public DoubleParameter(String name, String description, boolean required, - Double defValue, double min, double max) + Double defValue, double min, double max) { super(name, description, required, String.valueOf(defValue), null, null, - null); + null); defval = defValue; this.min = min; this.max = max; } public DoubleParameter(String name, String description, boolean required, - Double defValue, Double value, double min, double max) + Double defValue, Double value, double min, double max) { super(name, description, required, String.valueOf(defValue), - String.valueOf(value), null, null); + String.valueOf(value), null, null); defval = defValue; this.min = min; this.max = max; diff --git a/src/jalview/ws/params/simple/FileParameter.java b/src/jalview/ws/params/simple/FileParameter.java index aa8e7ad..3e17e68 100644 --- a/src/jalview/ws/params/simple/FileParameter.java +++ b/src/jalview/ws/params/simple/FileParameter.java @@ -11,9 +11,27 @@ import jalview.ws.params.ValueConstrainI; */ public class FileParameter extends StringParameter { + public static class Builder extends StringParameter.Builder + { + @Override + public FileParameter build() + { + return new FileParameter(this); + } + } + + public static Builder newBuilder() + { + return new Builder(); + } + + public FileParameter(Builder builder) + { + super(builder); + } public FileParameter(String name, String description, boolean required, - String defValue, String value) + String defValue, String value) { super(name, description, required, defValue, value); } diff --git a/src/jalview/ws/params/simple/IntegerParameter.java b/src/jalview/ws/params/simple/IntegerParameter.java index b3a01b2..af6c881 100644 --- a/src/jalview/ws/params/simple/IntegerParameter.java +++ b/src/jalview/ws/params/simple/IntegerParameter.java @@ -22,6 +22,7 @@ package jalview.ws.params.simple; import jalview.ws.params.ParameterI; import jalview.ws.params.ValueConstrainI; +import static java.util.Objects.requireNonNullElse; /** * @author jimp @@ -29,6 +30,68 @@ import jalview.ws.params.ValueConstrainI; */ public class IntegerParameter extends Option implements ParameterI { + public static class Builder extends Option.Builder + { + // assigning them the opposite way results in no limits unless both are set + protected int min = Integer.MAX_VALUE; + + protected int max = Integer.MIN_VALUE; + + @Override + public void setDefaultValue(String defaultValue) + { + throw new UnsupportedOperationException(); + } + + public void setDefaultValue(Integer defaultValue) + { + if (defaultValue != null) + super.setDefaultValue(defaultValue.toString()); + else + super.setDefaultValue(null); + } + + @Override + public void setValue(String value) + { + throw new UnsupportedOperationException(); + } + + public void setValue(Integer value) + { + if (value != null) + super.setValue(value.toString()); + else + super.setValue(null); + } + + public void setMin(Integer min) + { + this.min = requireNonNullElse(min, Integer.MAX_VALUE); + } + + public void setMax(Integer max) + { + this.max = requireNonNullElse(max, Integer.MIN_VALUE); + } + + public void setBounds(Integer min, Integer max) + { + setMin(min); + setMax(max); + } + + public IntegerParameter build() + { + return new IntegerParameter(this); + } + } + + public static Builder newBuilder() + { + return new Builder(); + } + int defval; int min; @@ -61,6 +124,15 @@ public class IntegerParameter extends Option implements ParameterI }; } + protected IntegerParameter(Builder builder) + { + super(builder); + min = builder.min; + max = builder.max; + if (defvalue != null) + defval = Integer.parseInt(defvalue); + } + public IntegerParameter(IntegerParameter parm) { super(parm); @@ -69,20 +141,20 @@ public class IntegerParameter extends Option implements ParameterI } public IntegerParameter(String name, String description, boolean required, - int defValue, int min, int max) + int defValue, int min, int max) { super(name, description, required, String.valueOf(defValue), null, null, - null); + null); defval = defValue; this.min = min; this.max = max; } public IntegerParameter(String name, String description, boolean required, - int defValue, int value, int min, int max) + int defValue, int value, int min, int max) { super(name, description, required, String.valueOf(defValue), - String.valueOf(value), null, null); + String.valueOf(value), null, null); defval = defValue; this.min = min; this.max = max; diff --git a/src/jalview/ws/params/simple/LogarithmicParameter.java b/src/jalview/ws/params/simple/LogarithmicParameter.java index af80181..12a7be1 100644 --- a/src/jalview/ws/params/simple/LogarithmicParameter.java +++ b/src/jalview/ws/params/simple/LogarithmicParameter.java @@ -1,5 +1,7 @@ package jalview.ws.params.simple; +import static java.util.Objects.requireNonNullElse; + import jalview.ws.params.ParameterI; import jalview.ws.params.ValueConstrainI; @@ -11,6 +13,72 @@ import jalview.ws.params.ValueConstrainI; */ public class LogarithmicParameter extends Option implements ParameterI { + public static class Builder extends Option.Builder + { + // setting them the opposite way disables limits until both are set. + protected double min = Double.POSITIVE_INFINITY; + + protected double max = Double.NEGATIVE_INFINITY; + + /** + * Setting string on double parameter is not allowed, use + * {@link #setValue(Double)} instead. + */ + @Override + public void setValue(String value) + { + throw new UnsupportedOperationException(); + } + + public void setValue(Double value) + { + if (value != null) + super.setValue(value.toString()); + else + super.setValue(null); + } + + /** + * Setting string on double parameter is not allowed, use + * {@link #setDefaultValue(Double)} instead. + */ + @Override + public void setDefaultValue(String defaultValue) + { + throw new UnsupportedOperationException(); + } + + public void setDefaultValue(Double defaultValue) + { + if (defaultValue != null) + super.setDefaultValue(defaultValue.toString()); + else + super.setDefaultValue(null); + } + + public void setMin(Double min) + { + this.min = requireNonNullElse(min, Double.POSITIVE_INFINITY); + } + + public void setMax(Double max) + { + this.max = requireNonNullElse(max, Double.NEGATIVE_INFINITY); + } + + public void setBounds(Double min, Double max) + { + setMin(min); + setMax(max); + } + + @Override + public LogarithmicParameter build() + { + return new LogarithmicParameter(this); + } + } + final double defval; final double min; @@ -43,6 +111,22 @@ public class LogarithmicParameter extends Option implements ParameterI }; } + public static Builder newBuilder() + { + return new Builder(); + } + + public LogarithmicParameter(Builder builder) + { + super(builder); + this.min = builder.min; + this.max = builder.max; + if (defvalue != null) + defval = Double.parseDouble(defvalue); + else + defval = 0.0; + } + public LogarithmicParameter(LogarithmicParameter parm) { super(parm); @@ -52,21 +136,21 @@ public class LogarithmicParameter extends Option implements ParameterI } public LogarithmicParameter(String name, String description, - boolean required, Double defValue, double min, double max) + boolean required, Double defValue, double min, double max) { super(name, description, required, String.valueOf(defValue), null, null, - null); + null); defval = defValue; this.min = min; this.max = max; } public LogarithmicParameter(String name, String description, - boolean required, Double defValue, double value, double min, - double max) + boolean required, Double defValue, double value, double min, + double max) { super(name, description, required, String.valueOf(defValue), - String.valueOf(value), null, null); + String.valueOf(value), null, null); defval = defValue; this.min = min; this.max = max; diff --git a/src/jalview/ws/params/simple/Option.java b/src/jalview/ws/params/simple/Option.java index 40e3804..78150cd 100644 --- a/src/jalview/ws/params/simple/Option.java +++ b/src/jalview/ws/params/simple/Option.java @@ -25,9 +25,93 @@ import jalview.ws.params.OptionI; import java.net.URL; import java.util.ArrayList; import java.util.List; +import static java.util.Objects.requireNonNull; +import static java.util.Objects.requireNonNullElse; public class Option implements OptionI { + /** + * A builder class which avoids multiple telescoping parameters nightmare. + * + * @author mmwarowny + * + */ + public static class Builder + { + protected String name = null; + + protected String label = null; + + protected String description = ""; + + protected boolean required = false; + + protected String defaultValue = null; + + protected String value = null; + + protected List possibleValues = null; + + protected List displayValues = null; + + protected URL detailsUrl = null; + + public void setName(String name) + { + this.name = name; + } + + public void setLabel(String label) + { + this.label = label; + } + + public void setDescription(String description) + { + this.description = description; + } + + public void setRequired(boolean required) + { + this.required = required; + } + + public void setDefaultValue(String defaultValue) + { + this.defaultValue = defaultValue; + } + + public void setValue(String value) + { + this.value = value; + } + + public void setPossibleValues(List possibleValues) + { + this.possibleValues = possibleValues; + } + + public void setDisplayValues(List displayValues) + { + this.displayValues = displayValues; + } + + public void setDetailsUrl(URL detailsUrl) + { + this.detailsUrl = detailsUrl; + } + + public Option build() + { + return new Option(this); + } + } + + public static Builder newBuilder() + { + return new Builder(); + } + String name; String label; @@ -55,6 +139,21 @@ public class Option implements OptionI URL fdetails; + protected Option(Builder builder) + { + requireNonNull(builder.name); + name = builder.name; + label = requireNonNullElse(builder.label, name); + description = builder.description; + required = builder.required; + defvalue = builder.defaultValue; + value = builder.value; + possibleVals = new ArrayList<>(builder.possibleValues); + displayVals = new ArrayList<>( + requireNonNullElse(builder.displayValues, possibleVals)); + fdetails = builder.detailsUrl; + } + /** * Copy constructor * @@ -99,8 +198,8 @@ public class Option implements OptionI * @param fdetails */ public Option(String name2, String description2, boolean isrequired, - String defValue, String val, List possibleVals, - List displayNames, URL fdetails) + String defValue, String val, List possibleVals, + List displayNames, URL fdetails) { name = name2; description = description2; @@ -130,11 +229,11 @@ public class Option implements OptionI * @param fdetails */ public Option(String name2, String description2, boolean isrequired, - String defValue, String val, List possibleVals, - URL fdetails) + String defValue, String val, List possibleVals, + URL fdetails) { this(name2, description2, isrequired, defValue, val, possibleVals, null, - fdetails); + fdetails); } @Override diff --git a/src/jalview/ws/params/simple/RadioChoiceParameter.java b/src/jalview/ws/params/simple/RadioChoiceParameter.java index 4fdb05e..9567858 100644 --- a/src/jalview/ws/params/simple/RadioChoiceParameter.java +++ b/src/jalview/ws/params/simple/RadioChoiceParameter.java @@ -28,6 +28,24 @@ import java.util.List; */ public class RadioChoiceParameter extends StringParameter { + public static class Builder extends StringParameter.Builder + { + @Override + public RadioChoiceParameter build() + { + return new RadioChoiceParameter(this); + } + } + + public static Builder newBuilder() + { + return new Builder(); + } + + public RadioChoiceParameter(Builder builder) + { + super(builder); + } /** * Constructor @@ -38,7 +56,7 @@ public class RadioChoiceParameter extends StringParameter * @param def */ public RadioChoiceParameter(String name, String description, - List options, String def) + List options, String def) { super(name, description, true, def, def, options, null); } diff --git a/src/jalview/ws/params/simple/StringParameter.java b/src/jalview/ws/params/simple/StringParameter.java index d3d899c..ad3834a 100644 --- a/src/jalview/ws/params/simple/StringParameter.java +++ b/src/jalview/ws/params/simple/StringParameter.java @@ -7,6 +7,15 @@ import java.util.List; public class StringParameter extends Option implements ParameterI { + public static class Builder extends Option.Builder + { + @Override + public StringParameter build() + { + return new StringParameter(this); + } + } + @Override public ValueConstrainI getValidValue() { @@ -42,6 +51,16 @@ public class StringParameter extends Option implements ParameterI } + public static Builder newBuilder() + { + return new Builder(); + } + + protected StringParameter(Builder builder) + { + super(builder); + } + public StringParameter(StringParameter parm) { this.name = parm.name; @@ -51,18 +70,18 @@ public class StringParameter extends Option implements ParameterI } public StringParameter(String name, String description, boolean required, - String defValue) + String defValue) { super(name, description, required, String.valueOf(defValue), null, null, - null); + null); this.defvalue = defValue; } public StringParameter(String name, String description, boolean required, - String defValue, String value) + String defValue, String value) { super(name, description, required, String.valueOf(defValue), - String.valueOf(value), null, null); + String.valueOf(value), null, null); this.defvalue = defValue; } @@ -79,10 +98,10 @@ public class StringParameter extends Option implements ParameterI * @param displayNames */ public StringParameter(String name2, String description2, - boolean isrequired, String defValue, String value, - List possibleVals, List displayNames) + boolean isrequired, String defValue, String value, + List possibleVals, List displayNames) { super(name2, description2, isrequired, defValue, value, possibleVals, - displayNames, null); + displayNames, null); } } -- 1.7.10.2