JAL-3878 Add builders to each service parameter class
[jalview.git] / src / jalview / ws / params / simple / LogarithmicParameter.java
index 4944d6b..12a7be1 100644 (file)
@@ -1,20 +1,89 @@
 package jalview.ws.params.simple;
 
+import static java.util.Objects.requireNonNullElse;
+
 import jalview.ws.params.ParameterI;
 import jalview.ws.params.ValueConstrainI;
 
 /**
+ * A model for a numeric-valued parameter which should be displayed using a
+ * logarithmic scale
  * 
  * @author TZVanaalten
- *
  */
 public class LogarithmicParameter extends Option implements ParameterI
 {
-  Double defval;
+  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);
+    }
 
-  Double min, max;
+    public void setBounds(Double min, Double max)
+    {
+      setMin(min);
+      setMax(max);
+    }
+
+    @Override
+    public LogarithmicParameter build()
+    {
+      return new LogarithmicParameter(this);
+    }
+  }
 
-  Double base;
+  final double defval;
+
+  final double min;
+
+  final double max;
 
   @Override
   public ValueConstrainI getValidValue()
@@ -31,61 +100,60 @@ public class LogarithmicParameter extends Option implements ParameterI
       @Override
       public Number getMin()
       {
-        if (min < max)
-        {
-          return min;
-        }
-        else
-        {
-          return null;
-        }
+        return min < max ? min : null;
       }
 
       @Override
       public Number getMax()
       {
-        if (min < max)
-        {
-          return max;
-        }
-        else
-        {
-          return null;
-        }
+        return min < max ? max : null;
       }
     };
   }
 
+  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;
-    base = parm.base;
+    defval = 0D;
   }
 
   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
@@ -93,9 +161,4 @@ public class LogarithmicParameter extends Option implements ParameterI
   {
     return new LogarithmicParameter(this);
   }
-
-  public Double getBase()
-  {
-    return base;
-  }
 }