JAL-4131 Replace usages of requireNonNullElse
[jalview.git] / src / jalview / ws / params / simple / DoubleParameter.java
1 package jalview.ws.params.simple;
2
3 import jalview.ws.params.ParameterI;
4 import jalview.ws.params.ValueConstrainI;
5
6 /**
7  * 
8  * @author TZVanaalten
9  *
10  */
11 public class DoubleParameter extends Option implements ParameterI
12 {
13   public static class Builder extends Option.Builder
14   {
15     // setting them the opposite way disables limits until both are set.
16     protected double min = Double.POSITIVE_INFINITY;
17
18     protected double max = Double.NEGATIVE_INFINITY;
19
20     /**
21      * Setting string on double parameter is not allowed, use
22      * {@link #setValue(Double)} instead.
23      */
24     @Override
25     public void setValue(String value)
26     {
27       throw new UnsupportedOperationException();
28     }
29
30     public void setValue(Double value)
31     {
32       if (value != null)
33         super.setValue(value.toString());
34       else
35         super.setValue(null);
36     }
37
38     /**
39      * Setting string on double parameter is not allowed, use
40      * {@link #setDefaultValue(Double)} instead.
41      */
42     @Override
43     public void setDefaultValue(String defaultValue)
44     {
45       throw new UnsupportedOperationException();
46     }
47
48     public void setDefaultValue(Double defaultValue)
49     {
50       if (defaultValue != null)
51         super.setDefaultValue(defaultValue.toString());
52       else
53         super.setDefaultValue(null);
54     }
55
56     public void setMin(Double min)
57     {
58       this.min = min != null ? min : Double.POSITIVE_INFINITY;
59     }
60
61     public void setMax(Double max)
62     {
63       this.max = max != null ? max : Double.NEGATIVE_INFINITY;
64     }
65
66     public void setBounds(Double min, Double max)
67     {
68       setMin(min);
69       setMax(max);
70     }
71
72     public DoubleParameter build()
73     {
74       return new DoubleParameter(this);
75     }
76   }
77
78   public static Builder newBuilder()
79   {
80     return new Builder();
81   }
82
83   double defval;
84
85   double min;
86
87   double max;
88
89   @Override
90   public ValueConstrainI getValidValue()
91   {
92     return new ValueConstrainI()
93     {
94       @Override
95       public ValueType getType()
96       {
97         return ValueType.Double;
98       }
99
100       @Override
101       public Number getMin()
102       {
103         return min < max ? min : null;
104       }
105
106       @Override
107       public Number getMax()
108       {
109         return min < max ? max : null;
110       }
111     };
112   }
113
114   protected DoubleParameter(Builder builder)
115   {
116     super(builder);
117     this.min = builder.min;
118     this.max = builder.max;
119     if (defvalue != null)
120       defval = Double.parseDouble(defvalue);
121   }
122
123   public DoubleParameter(DoubleParameter parm)
124   {
125     super(parm);
126     max = parm.max;
127     min = parm.min;
128   }
129
130   public DoubleParameter(String name, String description, boolean required,
131       Double defValue, double min, double max)
132   {
133     super(name, description, required, String.valueOf(defValue), null, null,
134         null);
135     defval = defValue;
136     this.min = min;
137     this.max = max;
138   }
139
140   public DoubleParameter(String name, String description, boolean required,
141       Double defValue, Double value, double min, double max)
142   {
143     super(name, description, required, String.valueOf(defValue),
144         String.valueOf(value), null, null);
145     defval = defValue;
146     this.min = min;
147     this.max = max;
148   }
149
150   @Override
151   public DoubleParameter copy()
152   {
153     return new DoubleParameter(this);
154   }
155 }