JAL-3878 Create abstract base class for actions.
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Fri, 18 Feb 2022 14:42:41 +0000 (15:42 +0100)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Fri, 18 Feb 2022 14:42:41 +0000 (15:42 +0100)
src/jalview/ws2/actions/BaseAction.java [new file with mode: 0644]

diff --git a/src/jalview/ws2/actions/BaseAction.java b/src/jalview/ws2/actions/BaseAction.java
new file mode 100644 (file)
index 0000000..37fbe39
--- /dev/null
@@ -0,0 +1,165 @@
+package jalview.ws2.actions;
+
+import java.util.EnumSet;
+
+import jalview.ws2.actions.api.ActionI;
+import jalview.ws2.api.CredentialType;
+
+/**
+ * An abstract base class storing common data and implementing their getters
+ * defined in {@link ActionI} interface. The concrete action implementations are
+ * encouraged to extend this class and provide their own {@code perform} and
+ * {@code isActive} implementations.
+ * 
+ * @author mmwarowny
+ *
+ * @param <T>
+ *          task result type
+ */
+public abstract class BaseAction<T> implements ActionI<T>
+{
+  public static class Builder
+  {
+    protected String name;
+
+    protected String tooltip = "";
+
+    protected String subcategory = null;
+
+    protected int minSequences = -1;
+
+    protected int maxSequences = -1;
+
+    protected boolean allowProtein = true;
+
+    protected boolean allowNucleotide = true;
+
+    protected EnumSet<CredentialType> requiredCredentials = EnumSet.noneOf(CredentialType.class);
+
+    public Builder(String name)
+    {
+      this.name = name;
+    }
+
+    public void tooltip(String val)
+    {
+      tooltip = val;
+    }
+
+    public void subcategory(String val)
+    {
+      subcategory = val;
+    }
+
+    public void minSequences(int val)
+    {
+      minSequences = val;
+    }
+
+    public void maxSequecnes(int val)
+    {
+      maxSequences = val;
+    }
+
+    public void allowProtein(boolean val)
+    {
+      allowProtein = val;
+    }
+
+    public void allowNucleotide(boolean val)
+    {
+      allowNucleotide = val;
+    }
+
+    public void addRequiredCredential(CredentialType val)
+    {
+      requiredCredentials.add(val);
+    }
+
+    public void requiredCredentials(EnumSet<CredentialType> val)
+    {
+      requiredCredentials = val;
+    }
+  }
+
+  protected final String name;
+
+  protected final String tooltip;
+
+  protected final String subcategory;
+
+  protected final int minSequences;
+
+  protected final int maxSequences;
+
+  protected final boolean allowProtein;
+
+  protected final boolean allowNucleotide;
+
+  protected final EnumSet<CredentialType> requiredCredentials;
+
+  protected BaseAction(Builder builder)
+  {
+    this.name = builder.name;
+    this.tooltip = builder.tooltip;
+    this.subcategory = builder.subcategory;
+    this.minSequences = builder.minSequences;
+    this.maxSequences = builder.maxSequences;
+    this.allowProtein = builder.allowProtein;
+    this.allowNucleotide = builder.allowNucleotide;
+    this.requiredCredentials = builder.requiredCredentials;
+  }
+
+  public static Builder newBuilder(String name)
+  {
+    return new Builder(name);
+  }
+
+  @Override
+  public String getName()
+  {
+    return name;
+  }
+
+  @Override
+  public String getTooltip()
+  {
+    return tooltip;
+  }
+
+  @Override
+  public String getSubcategory()
+  {
+    return subcategory;
+  }
+
+  @Override
+  public int getMinSequences()
+  {
+    return minSequences;
+  }
+
+  @Override
+  public int getMaxSequences()
+  {
+    return maxSequences;
+  }
+
+  @Override
+  public boolean doAllowProtein()
+  {
+    return allowProtein;
+  }
+
+  @Override
+  public boolean doAllowNucleotide()
+  {
+    return allowNucleotide;
+  }
+
+  @Override
+  public EnumSet<CredentialType> getRequiredCredentials()
+  {
+    return requiredCredentials;
+  }
+}