JAL-3878 Create web service class - container for service metadata and actions
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Wed, 16 Feb 2022 17:15:34 +0000 (18:15 +0100)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Thu, 17 Feb 2022 17:12:43 +0000 (18:12 +0100)
src/jalview/ws2/api/WebService.java [new file with mode: 0644]

diff --git a/src/jalview/ws2/api/WebService.java b/src/jalview/ws2/api/WebService.java
new file mode 100644 (file)
index 0000000..39aef4a
--- /dev/null
@@ -0,0 +1,173 @@
+package jalview.ws2.api;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import jalview.ws.params.ParamDatastoreI;
+import jalview.ws2.actions.Action;
+
+import static java.util.Objects.requireNonNull;
+
+public class WebService<T extends Action>
+{
+  public static class Builder<T extends Action>
+  {
+    private URL url;
+
+    private String clientName;
+
+    private String category;
+
+    private String name;
+
+    private String description = "";
+
+    private ParamDatastoreI paramDatastore;
+
+    private List<T> actions = new ArrayList<>();
+
+    private Class<T> actionClass;
+
+    public Builder<T> url(URL val)
+    {
+      url = val;
+      return this;
+    }
+
+    public Builder<T> clientName(String val)
+    {
+      clientName = val;
+      return this;
+    }
+
+    public Builder<T> category(String val)
+    {
+      category = val;
+      return this;
+    }
+
+    public Builder<T> name(String val)
+    {
+      name = val;
+      return this;
+    }
+
+    public Builder<T> description(String val)
+    {
+      description = val;
+      return this;
+    }
+
+    public Builder<T> paramDatastore(ParamDatastoreI val)
+    {
+      paramDatastore = val;
+      return this;
+    }
+
+    public Builder<T> actions(List<T> val)
+    {
+      actions = val;
+      return this;
+    }
+
+    public Builder<T> action(T val)
+    {
+      actions.add(val);
+      return this;
+    }
+
+    public Builder<T> actionClass(Class<T> val)
+    {
+      actionClass = val;
+      return this;
+    }
+
+    public WebService<T> build()
+    {
+      requireNonNull(url);
+      requireNonNull(clientName);
+      requireNonNull(category);
+      requireNonNull(name);
+      requireNonNull(paramDatastore);
+      requireNonNull(actions);
+      if (actions.size() == 0)
+        throw new IllegalArgumentException("Empty actions list");
+      requireNonNull(actionClass);
+      return new WebService<T>(this);
+    }
+  }
+
+  private final URL url;
+
+  private final String clientName;
+
+  private final String category;
+
+  private final String name;
+
+  private final String description;
+
+  private final ParamDatastoreI paramDatastore;
+
+  private final List<T> actions;
+
+  private final Class<T> actionClass;
+
+  WebService(Builder<T> builder)
+  {
+    this.url = builder.url;
+    this.clientName = builder.clientName;
+    this.category = builder.category;
+    this.name = builder.name;
+    this.description = builder.description;
+    this.paramDatastore = builder.paramDatastore;
+    this.actions = Collections.unmodifiableList(builder.actions);
+    this.actionClass = builder.actionClass;
+  }
+  
+  public static <T extends Action> Builder<T> newBuilder()
+  {
+    return new Builder<T>();
+  }
+
+  URL getUrl()
+  {
+    return url;
+  }
+
+  String getClientName()
+  {
+    return clientName;
+  }
+
+  String getCategory()
+  {
+    return category;
+  }
+
+  String getName()
+  {
+    return name;
+  }
+
+  String getDescription()
+  {
+    return description;
+  }
+
+  ParamDatastoreI getParamDatastore()
+  {
+    return paramDatastore;
+  }
+
+  List<T> getActions()
+  {
+    return actions;
+  }
+
+  Class<T> getActionClass()
+  {
+    return actionClass;
+  }
+}