JAL-3878 Add builders to each service parameter class
[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 import static java.util.Objects.requireNonNullElse;
26
27 /**
28  * @author jimp
29  * 
30  */
31 public class IntegerParameter extends Option implements ParameterI
32 {
33   public static class Builder extends Option.Builder
34   {
35     // assigning them the opposite way results in no limits unless both are set
36     protected int min = Integer.MAX_VALUE;
37
38     protected int max = Integer.MIN_VALUE;
39
40     @Override
41     public void setDefaultValue(String defaultValue)
42     {
43       throw new UnsupportedOperationException();
44     }
45
46     public void setDefaultValue(Integer defaultValue)
47     {
48       if (defaultValue != null)
49         super.setDefaultValue(defaultValue.toString());
50       else
51         super.setDefaultValue(null);
52     }
53
54     @Override
55     public void setValue(String value)
56     {
57       throw new UnsupportedOperationException();
58     }
59
60     public void setValue(Integer value)
61     {
62       if (value != null)
63         super.setValue(value.toString());
64       else
65         super.setValue(null);
66     }
67
68     public void setMin(Integer min)
69     {
70       this.min = requireNonNullElse(min, Integer.MAX_VALUE);
71     }
72
73     public void setMax(Integer max)
74     {
75       this.max = requireNonNullElse(max, Integer.MIN_VALUE);
76     }
77
78     public void setBounds(Integer min, Integer max)
79     {
80       setMin(min);
81       setMax(max);
82     }
83
84     public IntegerParameter build()
85     {
86       return new IntegerParameter(this);
87     }
88   }
89
90   public static Builder newBuilder()
91   {
92     return new Builder();
93   }
94
95   int defval;
96
97   int min;
98
99   int max;
100
101   @Override
102   public ValueConstrainI getValidValue()
103   {
104     return new ValueConstrainI()
105     {
106
107       @Override
108       public ValueType getType()
109       {
110         return ValueType.Integer;
111       }
112
113       @Override
114       public Number getMin()
115       {
116         return min < max ? min : null;
117       }
118
119       @Override
120       public Number getMax()
121       {
122         return min < max ? max : null;
123       }
124     };
125   }
126
127   protected IntegerParameter(Builder builder)
128   {
129     super(builder);
130     min = builder.min;
131     max = builder.max;
132     if (defvalue != null)
133       defval = Integer.parseInt(defvalue);
134   }
135
136   public IntegerParameter(IntegerParameter parm)
137   {
138     super(parm);
139     max = parm.max;
140     min = parm.min;
141   }
142
143   public IntegerParameter(String name, String description, boolean required,
144       int defValue, int min, int max)
145   {
146     super(name, description, required, String.valueOf(defValue), null, null,
147         null);
148     defval = defValue;
149     this.min = min;
150     this.max = max;
151   }
152
153   public IntegerParameter(String name, String description, boolean required,
154       int defValue, int value, int min, int max)
155   {
156     super(name, description, required, String.valueOf(defValue),
157         String.valueOf(value), null, null);
158     defval = defValue;
159     this.min = min;
160     this.max = max;
161   }
162
163   @Override
164   public IntegerParameter copy()
165   {
166     return new IntegerParameter(this);
167   }
168
169 }