JAL-3878 Add web service reference to actions.
[jalview.git] / src / jalview / ws2 / actions / BaseAction.java
1 package jalview.ws2.actions;
2
3 import java.util.EnumSet;
4 import java.util.Objects;
5
6 import jalview.ws2.actions.api.ActionI;
7 import jalview.ws2.api.CredentialType;
8 import jalview.ws2.api.WebService;
9
10 /**
11  * An abstract base class storing common data and implementing their getters
12  * defined in {@link ActionI} interface. The concrete action implementations are
13  * encouraged to extend this class and provide their own {@code perform} and
14  * {@code isActive} implementations.
15  * 
16  * @author mmwarowny
17  * @param <R>
18  *          task result type
19  */
20 public abstract class BaseAction<R> implements ActionI<R>
21 {
22   public static abstract class Builder<A extends BaseAction<?>>
23   {
24     protected WebService<A> webService;
25
26     protected String name;
27
28     protected String tooltip = "";
29
30     protected String subcategory = null;
31
32     protected int minSequences = -1;
33
34     protected int maxSequences = -1;
35
36     protected boolean allowProtein = true;
37
38     protected boolean allowNucleotide = true;
39
40     protected EnumSet<CredentialType> requiredCredentials = EnumSet.noneOf(CredentialType.class);
41
42     public Builder(String name)
43     {
44       this.name = name;
45     }
46
47     public void webService(WebService<A> val)
48     {
49       this.webService = val;
50     }
51
52     public void tooltip(String val)
53     {
54       tooltip = val;
55     }
56
57     public void subcategory(String val)
58     {
59       subcategory = val;
60     }
61
62     public void minSequences(int val)
63     {
64       minSequences = val;
65     }
66
67     public void maxSequecnes(int val)
68     {
69       maxSequences = val;
70     }
71
72     public void allowProtein(boolean val)
73     {
74       allowProtein = val;
75     }
76
77     public void allowNucleotide(boolean val)
78     {
79       allowNucleotide = val;
80     }
81
82     public void addRequiredCredential(CredentialType val)
83     {
84       requiredCredentials.add(val);
85     }
86
87     public void requiredCredentials(EnumSet<CredentialType> val)
88     {
89       requiredCredentials = val;
90     }
91   }
92
93   protected final WebService<? extends ActionI<R>> webService;
94
95   protected final String name;
96
97   protected final String tooltip;
98
99   protected final String subcategory;
100
101   protected final int minSequences;
102
103   protected final int maxSequences;
104
105   protected final boolean allowProtein;
106
107   protected final boolean allowNucleotide;
108
109   protected final EnumSet<CredentialType> requiredCredentials;
110
111   protected BaseAction(Builder<? extends BaseAction<R>> builder)
112   {
113     Objects.requireNonNull(builder.webService);
114     this.webService = builder.webService;
115     this.name = builder.name;
116     this.tooltip = builder.tooltip;
117     this.subcategory = builder.subcategory;
118     this.minSequences = builder.minSequences;
119     this.maxSequences = builder.maxSequences;
120     this.allowProtein = builder.allowProtein;
121     this.allowNucleotide = builder.allowNucleotide;
122     this.requiredCredentials = builder.requiredCredentials;
123   }
124
125   @Override
126   public WebService<? extends ActionI<R>> getWebService()
127   {
128     return webService;
129   }
130
131   @Override
132   public String getName()
133   {
134     return name;
135   }
136
137   @Override
138   public String getTooltip()
139   {
140     return tooltip;
141   }
142
143   @Override
144   public String getSubcategory()
145   {
146     return subcategory;
147   }
148
149   @Override
150   public int getMinSequences()
151   {
152     return minSequences;
153   }
154
155   @Override
156   public int getMaxSequences()
157   {
158     return maxSequences;
159   }
160
161   @Override
162   public boolean doAllowProtein()
163   {
164     return allowProtein;
165   }
166
167   @Override
168   public boolean doAllowNucleotide()
169   {
170     return allowNucleotide;
171   }
172
173   @Override
174   public EnumSet<CredentialType> getRequiredCredentials()
175   {
176     return requiredCredentials;
177   }
178 }