JAL-3878 Create web service class - container for service metadata and actions
[jalview.git] / src / jalview / ws2 / api / WebService.java
1 package jalview.ws2.api;
2
3 import java.net.URL;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.List;
7 import jalview.ws.params.ParamDatastoreI;
8 import jalview.ws2.actions.Action;
9
10 import static java.util.Objects.requireNonNull;
11
12 public class WebService<T extends Action>
13 {
14   public static class Builder<T extends Action>
15   {
16     private URL url;
17
18     private String clientName;
19
20     private String category;
21
22     private String name;
23
24     private String description = "";
25
26     private ParamDatastoreI paramDatastore;
27
28     private List<T> actions = new ArrayList<>();
29
30     private Class<T> actionClass;
31
32     public Builder<T> url(URL val)
33     {
34       url = val;
35       return this;
36     }
37
38     public Builder<T> clientName(String val)
39     {
40       clientName = val;
41       return this;
42     }
43
44     public Builder<T> category(String val)
45     {
46       category = val;
47       return this;
48     }
49
50     public Builder<T> name(String val)
51     {
52       name = val;
53       return this;
54     }
55
56     public Builder<T> description(String val)
57     {
58       description = val;
59       return this;
60     }
61
62     public Builder<T> paramDatastore(ParamDatastoreI val)
63     {
64       paramDatastore = val;
65       return this;
66     }
67
68     public Builder<T> actions(List<T> val)
69     {
70       actions = val;
71       return this;
72     }
73
74     public Builder<T> action(T val)
75     {
76       actions.add(val);
77       return this;
78     }
79
80     public Builder<T> actionClass(Class<T> val)
81     {
82       actionClass = val;
83       return this;
84     }
85
86     public WebService<T> build()
87     {
88       requireNonNull(url);
89       requireNonNull(clientName);
90       requireNonNull(category);
91       requireNonNull(name);
92       requireNonNull(paramDatastore);
93       requireNonNull(actions);
94       if (actions.size() == 0)
95         throw new IllegalArgumentException("Empty actions list");
96       requireNonNull(actionClass);
97       return new WebService<T>(this);
98     }
99   }
100
101   private final URL url;
102
103   private final String clientName;
104
105   private final String category;
106
107   private final String name;
108
109   private final String description;
110
111   private final ParamDatastoreI paramDatastore;
112
113   private final List<T> actions;
114
115   private final Class<T> actionClass;
116
117   WebService(Builder<T> builder)
118   {
119     this.url = builder.url;
120     this.clientName = builder.clientName;
121     this.category = builder.category;
122     this.name = builder.name;
123     this.description = builder.description;
124     this.paramDatastore = builder.paramDatastore;
125     this.actions = Collections.unmodifiableList(builder.actions);
126     this.actionClass = builder.actionClass;
127   }
128   
129   public static <T extends Action> Builder<T> newBuilder()
130   {
131     return new Builder<T>();
132   }
133
134   URL getUrl()
135   {
136     return url;
137   }
138
139   String getClientName()
140   {
141     return clientName;
142   }
143
144   String getCategory()
145   {
146     return category;
147   }
148
149   String getName()
150   {
151     return name;
152   }
153
154   String getDescription()
155   {
156     return description;
157   }
158
159   ParamDatastoreI getParamDatastore()
160   {
161     return paramDatastore;
162   }
163
164   List<T> getActions()
165   {
166     return actions;
167   }
168
169   Class<T> getActionClass()
170   {
171     return actionClass;
172   }
173 }