JAL-4090 JAL-1551 spotlessApply
[jalview.git] / src / jalview / ws / jws2 / ParameterUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ws.jws2;
22
23 import jalview.util.MessageManager;
24
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.List;
28
29 import compbio.metadata.Option;
30 import compbio.metadata.Parameter;
31 import compbio.metadata.RunnerConfig;
32 import compbio.metadata.ValueConstrain;
33 import compbio.metadata.WrongParameterException;
34
35 public class ParameterUtils
36 {
37   public static List<String> writeParameterSet(List<Option> optSet,
38           String pseparator)
39   {
40     List<String> pset = new ArrayList<String>();
41     for (Option o : optSet)
42     {
43       pset.add(o.toCommand(pseparator));
44     }
45     return pset;
46   }
47
48   /**
49    * Converts options supplied via parameters file into {@code Option} objects
50    * (Refactored from compbio.ws.client.Jws2Client)
51    * 
52    * @param <T>
53    *          web service type
54    * @param params
55    * @param options
56    * @return List of Options of type T
57    * 
58    */
59   /*
60    * @SuppressWarnings(value = { "true" }) public static <T> List<Option<T>>
61    * processParameters(List<String> params, RunnerConfig<T> options, String
62    * pseparator)
63    */
64   public static List<Option> processParameters(List<String> params,
65           RunnerConfig options, String pseparator)
66   {
67     List<Option> chosenOptions = new ArrayList<Option>();
68     for (String param : params)
69     {
70       String oname = null;
71       if (isParameter(param, pseparator))
72       {
73         oname = getParamName(param, pseparator);
74       }
75       else
76       {
77         oname = param;
78       }
79       Option o = options.getArgumentByOptionName(oname);
80       if (o == null)
81       {
82         jalview.bin.Console
83                 .outPrintln("WARN ignoring unsuppoted parameter: " + oname);
84         continue;
85       }
86       if (o instanceof Parameter)
87       {
88         o = copyParameter((Parameter) o);
89       }
90       else
91       {
92         o = copyOption(o);
93       }
94       {
95         try
96         {
97           o.setDefaultValue(isParameter(param, pseparator)
98                   ? getParamValue(param, pseparator)
99                   : param);
100         } catch (WrongParameterException e)
101         {
102           jalview.bin.Console.outPrintln(
103                   "Problem setting value for the parameter: " + param);
104           e.printStackTrace();
105         }
106       }
107       chosenOptions.add(o);
108     }
109     return chosenOptions;
110   }
111
112   static String getParamName(String fullName, String pseparator)
113   {
114     assert isParameter(fullName, pseparator);
115     return fullName.substring(0, fullName.indexOf(pseparator));
116   }
117
118   static String getParamValue(String fullName, String pseparator)
119   {
120     assert isParameter(fullName, pseparator);
121     return fullName.substring(fullName.indexOf(pseparator) + 1);
122   }
123
124   static boolean isParameter(String param, String pseparator)
125   {
126     return param.contains(pseparator);
127   }
128
129   public static Option copyOption(Option option)
130   {
131     Option copy = new Option(option.getName(), option.getDescription());
132     setOptionFrom(copy, option);
133     return copy;
134   }
135
136   public static void setOptionFrom(Option copy, Option option)
137   {
138     copy.setName(option.getName());
139     copy.setDescription(option.getDescription());
140     copy.setBasicURL(option.getBasicURL());
141     copy.setFurtherDetails(option.getFurtherDetails());
142     copy.setRequired(option.isRequired());
143     List<String> names = option.getOptionNames();
144     if (names != null)
145     {
146       if (names.size() == 1)
147       {
148         HashSet<String> st = new HashSet();
149         st.add(names.get(0));
150         copy.setOptionNames(st);
151       }
152       else
153       {
154         copy.addOptionNames(names.toArray(new String[] {}));
155       }
156     }
157     try
158     {
159       if (option.getDefaultValue() != null)
160       {
161         copy.setDefaultValue(option.getDefaultValue());
162       }
163     } catch (Exception ex)
164     {
165       ex.printStackTrace();
166     }
167   }
168
169   public static ValueConstrain copyValueConstrain(ValueConstrain vc)
170   {
171     try
172     {
173       ValueConstrain copy = new ValueConstrain();
174       if (vc.getMax() != null)
175       {
176         copy.setMax(vc.getMax().toString());
177       }
178       if (vc.getMin() != null)
179       {
180         copy.setMin(vc.getMin().toString());
181       }
182       if (vc.getType() != null)
183       {
184         copy.setType(vc.getType());
185       }
186       return copy;
187     } catch (Exception e)
188     {
189       e.printStackTrace();
190       throw new Error(MessageManager.getString(
191               "error.implementation_error_couldnt_copy_value_constraint"));
192     }
193   }
194
195   public static Parameter copyParameter(Parameter parameter)
196   {
197     Parameter copy = new Parameter(parameter.getName(),
198             parameter.getDescription());
199     if (parameter.getValidValue() != null)
200     {
201       copy.setValidValue(copyValueConstrain(parameter.getValidValue()));
202     }
203     List<String> pv = parameter.getPossibleValues();
204     if (pv != null)
205     {
206       copy.addPossibleValues(pv.toArray(new String[] {}));
207     }
208     setOptionFrom(copy, parameter);
209     return copy;
210   }
211
212 }