JAL-4131 Replace usages of requireNonNullElse
[jalview.git] / src / jalview / ws / params / simple / Option.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.OptionI;
24
25 import java.net.URL;
26 import java.util.ArrayList;
27 import java.util.List;
28 import static java.util.Objects.requireNonNull;
29
30 public class Option implements OptionI
31 {
32   /**
33    * A builder class which avoids multiple telescoping parameters nightmare.
34    * 
35    * @author mmwarowny
36    *
37    */
38   public static class Builder
39   {
40     protected String name = null;
41
42     protected String label = null;
43
44     protected String description = "";
45
46     protected boolean required = false;
47
48     protected String defaultValue = null;
49
50     protected String value = null;
51
52     protected List<String> possibleValues = null;
53
54     protected List<String> displayValues = null;
55
56     protected URL detailsUrl = null;
57
58     public void setName(String name)
59     {
60       this.name = name;
61     }
62
63     public void setLabel(String label)
64     {
65       this.label = label;
66     }
67
68     public void setDescription(String description)
69     {
70       this.description = description;
71     }
72
73     public void setRequired(boolean required)
74     {
75       this.required = required;
76     }
77
78     public void setDefaultValue(String defaultValue)
79     {
80       this.defaultValue = defaultValue;
81     }
82
83     public void setValue(String value)
84     {
85       this.value = value;
86     }
87
88     public void setPossibleValues(List<String> possibleValues)
89     {
90       this.possibleValues = possibleValues;
91     }
92
93     public void setDisplayValues(List<String> displayValues)
94     {
95       this.displayValues = displayValues;
96     }
97
98     public void setDetailsUrl(URL detailsUrl)
99     {
100       this.detailsUrl = detailsUrl;
101     }
102
103     public Option build()
104     {
105       return new Option(this);
106     }
107   }
108
109   public static Builder newBuilder()
110   {
111     return new Builder();
112   }
113
114   String name;
115
116   String label;
117
118   /*
119    * current value in string format, or "null" if undefined
120    */
121   String value;
122
123   /*
124    * default value in string format, or "null" if undefined
125    */
126   String defvalue;
127
128   String description;
129
130   List<String> possibleVals;
131
132   /*
133    * optional display names corresponding to possibleVals
134    */
135   List<String> displayVals;
136
137   boolean required;
138
139   URL fdetails;
140
141   protected Option(Builder builder)
142   {
143     requireNonNull(builder.name);
144     name = builder.name;
145     label = builder.label != null ? builder.label : name;
146     description = builder.description;
147     required = builder.required;
148     defvalue = builder.defaultValue;
149     value = builder.value;
150     if (builder.possibleValues != null)
151       possibleVals = new ArrayList<>(builder.possibleValues);
152     if (builder.displayValues != null)
153       displayVals = new ArrayList<>(builder.displayValues);
154     else
155       displayVals = possibleVals;
156     if (possibleVals == null && displayVals != null)
157       throw new IllegalArgumentException(
158           "cannot use displayValues if possibleValues is null");
159     if (possibleVals != null && possibleVals.size() != displayVals.size())
160       throw new IllegalArgumentException(
161           "displayValues size does not match possibleValues");
162     fdetails = builder.detailsUrl;
163   }
164
165   /**
166    * Copy constructor
167    * 
168    * @param opt
169    */
170   public Option(Option opt)
171   {
172     name = opt.name;
173     label = opt.label;
174     value = opt.value;
175     defvalue = opt.defvalue;
176     description = opt.description;
177     if (opt.possibleVals != null)
178     {
179       possibleVals = new ArrayList<>(opt.possibleVals);
180     }
181     required = opt.required;
182     // URLs are singletons - so we copy by reference. nasty but true.
183     fdetails = opt.fdetails;
184   }
185
186   public Option()
187   {
188   }
189
190   public Option(String name, String description, String label, boolean isrequired,
191       String defValue, String val, List<String> possibleVals, URL fdetails)
192   {
193     this(name, description, isrequired, defValue, val, possibleVals, fdetails);
194     this.label = label;
195   }
196
197   /**
198    * Constructor including display names for possible values
199    * 
200    * @param name2
201    * @param description2
202    * @param isrequired
203    * @param defValue
204    * @param val
205    * @param possibleVals
206    * @param fdetails
207    */
208   public Option(String name2, String description2, boolean isrequired,
209       String defValue, String val, List<String> possibleVals,
210       List<String> displayNames, URL fdetails)
211   {
212     name = name2;
213     description = description2;
214     this.value = val;
215     this.required = isrequired;
216     this.defvalue = defValue;
217     if (possibleVals != null)
218     {
219       this.possibleVals = new ArrayList<>(possibleVals);
220     }
221     if (displayNames != null)
222     {
223       this.displayVals = new ArrayList<>(displayNames);
224     }
225     this.fdetails = fdetails;
226   }
227
228   /**
229    * Constructor
230    * 
231    * @param name2
232    * @param description2
233    * @param isrequired
234    * @param defValue
235    * @param val
236    * @param possibleVals
237    * @param fdetails
238    */
239   public Option(String name2, String description2, boolean isrequired,
240       String defValue, String val, List<String> possibleVals,
241       URL fdetails)
242   {
243     this(name2, description2, isrequired, defValue, val, possibleVals, null,
244         fdetails);
245   }
246
247   @Override
248   public OptionI copy()
249   {
250     Option opt = new Option(this);
251     return opt;
252   }
253
254   /**
255    * toString method to help identify options in the debugger only
256    */
257   @Override
258   public String toString()
259   {
260     return this.getClass().getName() + ":" + name;
261   }
262
263   @Override
264   public String getName()
265   {
266     return name;
267   }
268
269   @Override
270   public String getLabel()
271   {
272     return label != null ? label : name;
273   }
274
275   @Override
276   public String getValue()
277   {
278     return value == null ? defvalue : value;
279   }
280
281   @Override
282   public void setValue(String selectedItem)
283   {
284     value = selectedItem;
285   }
286
287   @Override
288   public URL getFurtherDetails()
289   {
290     return fdetails;
291   }
292
293   @Override
294   public boolean isRequired()
295   {
296     return required;
297   }
298
299   @Override
300   public String getDescription()
301   {
302     return description;
303   }
304
305   @Override
306   public List<String> getPossibleValues()
307   {
308     return possibleVals;
309   }
310
311   @Override
312   public List<String> getDisplayNames()
313   {
314     return displayVals;
315   }
316 }