JAL-3878 Add fullName to BaseAction class which combines service and action name
[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()
43     {
44     }
45
46     public void name(String val)
47     {
48       this.name = val;
49     }
50
51     public void webService(WebService<A> val)
52     {
53       this.webService = val;
54     }
55
56     public void tooltip(String val)
57     {
58       tooltip = val;
59     }
60
61     public void subcategory(String val)
62     {
63       subcategory = val;
64     }
65
66     public void minSequences(int val)
67     {
68       minSequences = val;
69     }
70
71     public void maxSequecnes(int val)
72     {
73       maxSequences = val;
74     }
75
76     public void allowProtein(boolean val)
77     {
78       allowProtein = val;
79     }
80
81     public void allowNucleotide(boolean val)
82     {
83       allowNucleotide = val;
84     }
85
86     public void addRequiredCredential(CredentialType val)
87     {
88       requiredCredentials.add(val);
89     }
90
91     public void requiredCredentials(EnumSet<CredentialType> val)
92     {
93       requiredCredentials = val;
94     }
95   }
96
97   protected final WebService<? extends ActionI<R>> webService;
98
99   protected final String name;
100
101   protected final String tooltip;
102
103   protected final String subcategory;
104
105   protected final int minSequences;
106
107   protected final int maxSequences;
108
109   protected final boolean allowProtein;
110
111   protected final boolean allowNucleotide;
112
113   protected final EnumSet<CredentialType> requiredCredentials;
114
115   protected BaseAction(Builder<? extends BaseAction<R>> builder)
116   {
117     Objects.requireNonNull(builder.webService);
118     this.webService = builder.webService;
119     Objects.requireNonNull(builder.name);
120     this.name = builder.name;
121     this.tooltip = builder.tooltip;
122     this.subcategory = builder.subcategory;
123     this.minSequences = builder.minSequences;
124     this.maxSequences = builder.maxSequences;
125     this.allowProtein = builder.allowProtein;
126     this.allowNucleotide = builder.allowNucleotide;
127     this.requiredCredentials = builder.requiredCredentials;
128   }
129
130   @Override
131   public WebService<? extends ActionI<R>> getWebService()
132   {
133     return webService;
134   }
135
136   @Override
137   public String getName()
138   {
139     return name;
140   }
141
142   /**
143    * Returns a full name of the action which comprises of the service name and
144    * the action name if present.
145    * 
146    * @return full name of this action
147    */
148   public String getFullName()
149   {
150     if (name == null || name.isEmpty())
151       return webService.getName();
152     else
153       return webService.getName() + " " + name;
154   }
155
156   @Override
157   public String getTooltip()
158   {
159     return tooltip;
160   }
161
162   @Override
163   public String getSubcategory()
164   {
165     return subcategory;
166   }
167
168   @Override
169   public int getMinSequences()
170   {
171     return minSequences;
172   }
173
174   @Override
175   public int getMaxSequences()
176   {
177     return maxSequences;
178   }
179
180   @Override
181   public boolean doAllowProtein()
182   {
183     return allowProtein;
184   }
185
186   @Override
187   public boolean doAllowNucleotide()
188   {
189     return allowNucleotide;
190   }
191
192   @Override
193   public EnumSet<CredentialType> getRequiredCredentials()
194   {
195     return requiredCredentials;
196   }
197 }