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