Merge branch 'mmw/JAL-4199-task-execution-update' into development/Release_2_12_Branch
[jalview.git] / src / jalview / ws / params / simple / BooleanOption.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 java.net.URL;
24 import java.util.Arrays;
25 import java.util.List;
26
27 import jalview.ws.params.ArgumentI;
28
29 public class BooleanOption extends Option
30 {
31   public static class Builder extends Option.Builder
32   {
33     private boolean defaultValue = false;
34
35     private boolean value = false;
36
37     private String reprValue = null;
38
39     public void setDefaultValue(Boolean defaultValue)
40     {
41       this.defaultValue = defaultValue != null ? defaultValue : false;
42     }
43
44     public void setValue(Boolean value)
45     {
46       this.value = value != null ? value : false;
47     }
48
49     public void setReprValue(String reprValue)
50     {
51       this.reprValue = reprValue;
52     }
53
54     @Override
55     public void setPossibleValues(List<String> possibleValues)
56     {
57       throw new UnsupportedOperationException("cannot set possible values for boolean");
58     }
59
60     @Override
61     public BooleanOption build()
62     {
63       return new BooleanOption(this);
64     }
65   }
66
67   public static Builder newBuilder()
68   {
69     return new Builder();
70   }
71
72   protected BooleanOption(Builder builder)
73   {
74     super(builder);
75     String reprValue = builder.reprValue != null ? builder.reprValue : name;
76     defvalue = builder.defaultValue ? reprValue : null;
77     value = builder.value ? reprValue : null;
78     possibleVals = List.of(reprValue);
79     displayVals = List.of(label);
80   }
81
82   public BooleanOption(String name, String descr, boolean required,
83       Boolean defVal, Boolean val, URL link)
84   {
85     super(name, descr, required, (defVal != null && defVal ? name : null),
86         (val != null && val ? name : null), Arrays.asList(name), link);
87   }
88
89   public BooleanOption(String name, String description, String label,
90       boolean isrequired, Boolean defValue, String reprValue, URL link)
91   {
92     super(name, description, label, isrequired,
93         defValue != null && defValue ? reprValue : null,
94         defValue != null && defValue ? reprValue : null,
95         Arrays.asList(reprValue), link);
96   }
97
98   public BooleanOption(String name, String description, String label,
99       boolean isrequired, Boolean defValue, URL link)
100   {
101     this(name, description, label, isrequired, defValue, String.valueOf(true), link);
102   }
103   
104   public static Boolean parseBoolean(ArgumentI argument)
105   {
106     return argument.getValue() != null && !argument.getValue().isEmpty() ?
107             true : false;
108   }
109 }