JAL-1601 Implement JPred4 WS client and discoverer
[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 = null;
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     this.name = builder.name;
120     this.tooltip = builder.tooltip;
121     this.subcategory = builder.subcategory;
122     this.minSequences = builder.minSequences;
123     this.maxSequences = builder.maxSequences;
124     this.allowProtein = builder.allowProtein;
125     this.allowNucleotide = builder.allowNucleotide;
126     this.requiredCredentials = builder.requiredCredentials;
127   }
128
129   @Override
130   public WebService<? extends ActionI<R>> getWebService()
131   {
132     return webService;
133   }
134
135   @Override
136   public String getName()
137   {
138     return name;
139   }
140
141   /**
142    * Returns a full name of the action which comprises of the service name and
143    * the action name if present.
144    * 
145    * @return full name of this action
146    */
147   public String getFullName()
148   {
149     if (name == null || name.isEmpty())
150       return webService.getName();
151     else
152       return webService.getName() + " " + name;
153   }
154
155   @Override
156   public String getTooltip()
157   {
158     return tooltip;
159   }
160
161   @Override
162   public String getSubcategory()
163   {
164     return subcategory;
165   }
166
167   @Override
168   public int getMinSequences()
169   {
170     return minSequences;
171   }
172
173   @Override
174   public int getMaxSequences()
175   {
176     return maxSequences;
177   }
178
179   @Override
180   public boolean doAllowProtein()
181   {
182     return allowProtein;
183   }
184
185   @Override
186   public boolean doAllowNucleotide()
187   {
188     return allowNucleotide;
189   }
190
191   @Override
192   public EnumSet<CredentialType> getRequiredCredentials()
193   {
194     return requiredCredentials;
195   }
196 }