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