Merge branch 'JAL-3878_ws-overhaul-3' into mmw/Release_2_12_ws_merge
[jalview.git] / src / jalview / ws2 / params / SimpleParamSet.java
diff --git a/src/jalview/ws2/params/SimpleParamSet.java b/src/jalview/ws2/params/SimpleParamSet.java
new file mode 100644 (file)
index 0000000..9050c5f
--- /dev/null
@@ -0,0 +1,281 @@
+package jalview.ws2.params;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import jalview.ws.params.ArgumentI;
+import jalview.ws.params.WsParamSetI;
+
+/**
+ * A simple, web service client agnostic, representation of parameter sets.
+ * Instances are created from the service data fetched from the server or from
+ * the user preset files. This implementation of {@link WsParamSetI} is meant to
+ * decouple parameter set representation form specific clients.
+ * 
+ * @author mmwarowny
+ *
+ */
+public class SimpleParamSet implements WsParamSetI
+{
+  /**
+   * A convenience builder of {@link SimpleParamSet} objects.
+   * 
+   * @author mmwarowny
+   */
+  public static class Builder
+  {
+    private String name = "default";
+
+    private String description = "";
+
+    private List<String> applicableUrls = new ArrayList<>();
+
+    private boolean modifiable = false;
+
+    private List<ArgumentI> arguments = new ArrayList<>();
+
+    public Builder()
+    {
+    }
+
+    /**
+     * Set a name of parameter set.
+     * 
+     * @param val
+     *          name
+     */
+    public void name(String val)
+    {
+      name = val;
+    }
+
+    /**
+     * Set a description of parameter set.
+     * 
+     * @param val
+     *          description
+     */
+    public void description(String val)
+    {
+      description = val;
+    }
+
+    /**
+     * Add a url to applicable urls for parameter set.
+     * 
+     * @param val
+     *          applicable url
+     */
+    public void url(String val)
+    {
+      applicableUrls.add(val);
+    }
+
+    /**
+     * Set all applicable urls for parameter set. Current url list will be
+     * replaced by provided urls.
+     * 
+     * @param val
+     *          applicable urls
+     */
+    public void urls(String[] val)
+    {
+      applicableUrls.clear();
+      for (String url : val)
+        applicableUrls.add(url);
+    }
+
+    /**
+     * Set modifiable flag for parameter set.
+     * 
+     * @param val
+     *          modifiable
+     */
+    public void modifiable(boolean val)
+    {
+      modifiable = val;
+    }
+
+    /**
+     * Add an argument to the preset arguments.
+     * 
+     * @param val
+     *          argument to be added
+     */
+    public void argument(ArgumentI val)
+    {
+      arguments.add(val);
+    }
+
+    /**
+     * Set arguments for parameter set. Current parameters list will be
+     * replaced by provided arguments.
+     * 
+     * @param val
+     *          arguments to be added
+     */
+    public void arguments(List<? extends ArgumentI> val)
+    {
+      arguments.clear();
+      arguments.addAll(val);
+    }
+
+    /**
+     * Build a new {@link SimpleParamSet} object from the current state of this
+     * builder.
+     * 
+     * @return new paramset instance
+     */
+    public SimpleParamSet build()
+    {
+      return new SimpleParamSet(this);
+    }
+  }
+
+  protected String name;
+
+  protected String description;
+
+  protected String[] applicableUrls;
+
+  protected String sourceFile;
+
+  protected boolean modifiable;
+
+  protected List<ArgumentI> arguments;
+
+  protected SimpleParamSet(Builder builder)
+  {
+    this.name = builder.name;
+    this.description = builder.description;
+    this.applicableUrls = builder.applicableUrls.toArray(new String[0]);
+    this.sourceFile = null;
+    this.modifiable = builder.modifiable;
+    setArguments(builder.arguments);
+  }
+
+  /**
+   * Create a copy of the provided paramset. The new instance has the same
+   * properties as the original paramset. The arguments list is a shallow copy
+   * of the original arguments.
+   * 
+   * @param copy
+   */
+  public SimpleParamSet(WsParamSetI copy)
+  {
+    this.name = copy.getName();
+    this.description = copy.getDescription();
+    var urls = copy.getApplicableUrls();
+    this.applicableUrls = Arrays.copyOf(urls, urls.length);
+    this.sourceFile = copy.getSourceFile();
+    this.modifiable = copy.isModifiable();
+    setArguments(copy.getArguments());
+  }
+
+  /**
+   * Create a new instance of the parameter set builder.
+   * 
+   * @return new parameter set builder
+   */
+  public static Builder newBuilder()
+  {
+    return new Builder();
+  }
+
+  @Override
+  public String getName()
+  {
+    return name;
+  }
+
+  /**
+   * Set a human readable name for this parameter set.
+   * 
+   * @param name
+   *          new name
+   */
+  public void setName(String name)
+  {
+    this.name = name;
+  }
+
+  @Override
+  public String getDescription()
+  {
+    return description;
+  }
+
+  /**
+   * Set additional notes for this parameter set.
+   * 
+   * @param description
+   *          additional notes
+   */
+  public void setDescription(String description)
+  {
+    this.description = description;
+  }
+
+  @Override
+  public String[] getApplicableUrls()
+  {
+    return applicableUrls;
+  }
+
+  /**
+   * Set the list of service endpoints which this parameter set is valid for.
+   * 
+   * @param urls
+   *          new service endpoints
+   */
+  public void setApplicableUrls(String[] urls)
+  {
+    this.applicableUrls = urls;
+  }
+
+  @Override
+  public String getSourceFile()
+  {
+    return sourceFile;
+  }
+
+  @Override
+  public void setSourceFile(String newFile)
+  {
+    this.sourceFile = newFile;
+  }
+
+  @Override
+  public boolean isModifiable()
+  {
+    return this.modifiable;
+  }
+
+  /**
+   * Set whether this parameter set is modifiable or not.
+   * 
+   * @param modifiable
+   *          new modifiable value
+   */
+  public void setModifiable(boolean modifiable)
+  {
+    this.modifiable = modifiable;
+  }
+
+  @Override
+  public List<ArgumentI> getArguments()
+  {
+    return this.arguments;
+  }
+
+  @Override
+  public void setArguments(List<ArgumentI> args)
+  {
+    if (!isModifiable())
+      throw new UnsupportedOperationException(
+          "Attempting to modify an unmodifiable parameter set");
+    this.arguments = Collections.unmodifiableList(new ArrayList<>(args));
+  }
+}