JAL-3878 Add builders to each service parameter class
[jalview.git] / src / jalview / ws / params / simple / LogarithmicParameter.java
index 01744e7..12a7be1 100644 (file)
@@ -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,14 +13,78 @@ 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;
 
   final double max;
 
-  final double base; // todo is this even needed?
-
   @Override
   public ValueConstrainI getValidValue()
   {
@@ -45,37 +111,49 @@ 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);
     max = parm.max;
     min = parm.min;
     defval = 0D;
-    base = parm.base;
   }
 
   public LogarithmicParameter(String name, String description,
-          boolean required, Double defValue, double min, double max,
-          double base)
+      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;
-    this.base = base;
   }
 
   public LogarithmicParameter(String name, String description,
-          boolean required, Double defValue, double value, double min,
-          double max, double base)
+      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;
-    this.base = base;
   }
 
   @Override
@@ -83,9 +161,4 @@ public class LogarithmicParameter extends Option implements ParameterI
   {
     return new LogarithmicParameter(this);
   }
-
-  public double getBase()
-  {
-    return base;
-  }
 }