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