JAL-3878 Create service agnostic param datastore and set
[jalview.git] / src / jalview / ws2 / params / SimpleParamSet.java
1 package jalview.ws2.params;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.List;
7
8 import jalview.ws.params.ArgumentI;
9 import jalview.ws.params.WsParamSetI;
10
11 /**
12  * A simple, web service client agnostic, representation of parameter sets.
13  * Instances are created from the service data fetched from the server or from
14  * the user preset files. This implementation of {@link WsParamSetI} is meant to
15  * decouple parameter set representation form specific clients.
16  * 
17  * @author mmwarowny
18  *
19  */
20 public class SimpleParamSet implements WsParamSetI
21 {
22   /**
23    * A convenience builder of {@link SimpleParamSet} objects.
24    * 
25    * @author mmwarowny
26    */
27   public static class Builder
28   {
29     private String name = "default";
30
31     private String description = "";
32
33     private List<String> applicableUrls = new ArrayList<>();
34
35     private boolean modifiable = false;
36
37     private List<ArgumentI> arguments = new ArrayList<>();
38
39     public Builder()
40     {
41     }
42
43     /**
44      * Set a name of parameter set.
45      * 
46      * @param val
47      *          name
48      */
49     public void name(String val)
50     {
51       name = val;
52     }
53
54     /**
55      * Set a description of parameter set.
56      * 
57      * @param val
58      *          description
59      */
60     public void description(String val)
61     {
62       description = val;
63     }
64
65     /**
66      * Add a url to applicable urls for parameter set.
67      * 
68      * @param val
69      *          applicable url
70      */
71     public void url(String val)
72     {
73       applicableUrls.add(val);
74     }
75
76     /**
77      * Set all applicable urls for parameter set. Current url list will be
78      * replaced by provided urls.
79      * 
80      * @param val
81      *          applicable urls
82      */
83     public void urls(String[] val)
84     {
85       applicableUrls.clear();
86       for (String url : val)
87         applicableUrls.add(url);
88     }
89
90     /**
91      * Set modifiable flag for parameter set.
92      * 
93      * @param val
94      *          modifiable
95      */
96     public void modifiable(boolean val)
97     {
98       modifiable = val;
99     }
100
101     /**
102      * Add an argument to the preset arguments.
103      * 
104      * @param val
105      *          argument to be added
106      */
107     public void argument(ArgumentI val)
108     {
109       arguments.add(val);
110     }
111
112     /**
113      * Set arguments for parameter set. Current parameters list will be
114      * replaced by provided arguments.
115      * 
116      * @param val
117      *          arguments to be added
118      */
119     public void arguments(List<? extends ArgumentI> val)
120     {
121       arguments.clear();
122       arguments.addAll(val);
123     }
124
125     /**
126      * Build a new {@link SimpleParamSet} object from the current state of this
127      * builder.
128      * 
129      * @return new paramset instance
130      */
131     public SimpleParamSet build()
132     {
133       return new SimpleParamSet(this);
134     }
135   }
136
137   protected String name;
138
139   protected String description;
140
141   protected String[] applicableUrls;
142
143   protected String sourceFile;
144
145   protected boolean modifiable;
146
147   protected List<ArgumentI> arguments;
148
149   protected SimpleParamSet(Builder builder)
150   {
151     this.name = builder.name;
152     this.description = builder.description;
153     this.applicableUrls = builder.applicableUrls.toArray(new String[0]);
154     this.sourceFile = null;
155     this.modifiable = builder.modifiable;
156     setArguments(builder.arguments);
157   }
158
159   /**
160    * Create a copy of the provided paramset. The new instance has the same
161    * properties as the original paramset. The arguments list is a shallow copy
162    * of the original arguments.
163    * 
164    * @param copy
165    */
166   public SimpleParamSet(WsParamSetI copy)
167   {
168     this.name = copy.getName();
169     this.description = copy.getDescription();
170     var urls = copy.getApplicableUrls();
171     this.applicableUrls = Arrays.copyOf(urls, urls.length);
172     this.sourceFile = copy.getSourceFile();
173     this.modifiable = copy.isModifiable();
174     setArguments(copy.getArguments());
175   }
176
177   /**
178    * Create a new instance of the parameter set builder.
179    * 
180    * @return new parameter set builder
181    */
182   public static Builder newBuilder()
183   {
184     return new Builder();
185   }
186
187   @Override
188   public String getName()
189   {
190     return name;
191   }
192
193   /**
194    * Set a human readable name for this parameter set.
195    * 
196    * @param name
197    *          new name
198    */
199   public void setName(String name)
200   {
201     this.name = name;
202   }
203
204   @Override
205   public String getDescription()
206   {
207     return description;
208   }
209
210   /**
211    * Set additional notes for this parameter set.
212    * 
213    * @param description
214    *          additional notes
215    */
216   public void setDescription(String description)
217   {
218     this.description = description;
219   }
220
221   @Override
222   public String[] getApplicableUrls()
223   {
224     return applicableUrls;
225   }
226
227   /**
228    * Set the list of service endpoints which this parameter set is valid for.
229    * 
230    * @param urls
231    *          new service endpoints
232    */
233   public void setApplicableUrls(String[] urls)
234   {
235     this.applicableUrls = urls;
236   }
237
238   @Override
239   public String getSourceFile()
240   {
241     return sourceFile;
242   }
243
244   @Override
245   public void setSourceFile(String newFile)
246   {
247     this.sourceFile = newFile;
248   }
249
250   @Override
251   public boolean isModifiable()
252   {
253     return this.modifiable;
254   }
255
256   /**
257    * Set whether this parameter set is modifiable or not.
258    * 
259    * @param modifiable
260    *          new modifiable value
261    */
262   public void setModifiable(boolean modifiable)
263   {
264     this.modifiable = modifiable;
265   }
266
267   @Override
268   public List<ArgumentI> getArguments()
269   {
270     return this.arguments;
271   }
272
273   @Override
274   public void setArguments(List<ArgumentI> args)
275   {
276     if (!isModifiable())
277       throw new UnsupportedOperationException(
278           "Attempting to modify an unmodifiable parameter set");
279     this.arguments = Collections.unmodifiableList(new ArrayList<>(args));
280   }
281 }