JAL-4131 Replace usages of requireNonNullElse
[jalview.git] / src / jalview / ws / params / simple / IntegerParameter.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ws.params.simple;
22
23 import jalview.ws.params.ParameterI;
24 import jalview.ws.params.ValueConstrainI;
25
26 /**
27  * @author jimp
28  * 
29  */
30 public class IntegerParameter extends Option implements ParameterI
31 {
32   public static class Builder extends Option.Builder
33   {
34     // assigning them the opposite way results in no limits unless both are set
35     protected int min = Integer.MAX_VALUE;
36
37     protected int max = Integer.MIN_VALUE;
38
39     @Override
40     public void setDefaultValue(String defaultValue)
41     {
42       throw new UnsupportedOperationException();
43     }
44
45     public void setDefaultValue(Integer defaultValue)
46     {
47       if (defaultValue != null)
48         super.setDefaultValue(defaultValue.toString());
49       else
50         super.setDefaultValue(null);
51     }
52
53     @Override
54     public void setValue(String value)
55     {
56       throw new UnsupportedOperationException();
57     }
58
59     public void setValue(Integer value)
60     {
61       if (value != null)
62         super.setValue(value.toString());
63       else
64         super.setValue(null);
65     }
66
67     public void setMin(Integer min)
68     {
69       this.min = min != null ? min : Integer.MAX_VALUE;
70     }
71
72     public void setMax(Integer max)
73     { 
74       this.max = max != null ? max : Integer.MIN_VALUE;
75     }
76
77     public void setBounds(Integer min, Integer max)
78     {
79       setMin(min);
80       setMax(max);
81     }
82
83     public IntegerParameter build()
84     {
85       return new IntegerParameter(this);
86     }
87   }
88
89   public static Builder newBuilder()
90   {
91     return new Builder();
92   }
93
94   int defval;
95
96   int min;
97
98   int max;
99
100   @Override
101   public ValueConstrainI getValidValue()
102   {
103     return new ValueConstrainI()
104     {
105
106       @Override
107       public ValueType getType()
108       {
109         return ValueType.Integer;
110       }
111
112       @Override
113       public Number getMin()
114       {
115         return min < max ? min : null;
116       }
117
118       @Override
119       public Number getMax()
120       {
121         return min < max ? max : null;
122       }
123     };
124   }
125
126   protected IntegerParameter(Builder builder)
127   {
128     super(builder);
129     min = builder.min;
130     max = builder.max;
131     if (defvalue != null)
132       defval = Integer.parseInt(defvalue);
133   }
134
135   public IntegerParameter(IntegerParameter parm)
136   {
137     super(parm);
138     max = parm.max;
139     min = parm.min;
140   }
141
142   public IntegerParameter(String name, String description, boolean required,
143       int defValue, int min, int max)
144   {
145     super(name, description, required, String.valueOf(defValue), null, null,
146         null);
147     defval = defValue;
148     this.min = min;
149     this.max = max;
150   }
151
152   public IntegerParameter(String name, String description, boolean required,
153       int defValue, int value, int min, int max)
154   {
155     super(name, description, required, String.valueOf(defValue),
156         String.valueOf(value), null, null);
157     defval = defValue;
158     this.min = min;
159     this.max = max;
160   }
161
162   @Override
163   public IntegerParameter copy()
164   {
165     return new IntegerParameter(this);
166   }
167
168 }