2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.ws.jws2;
23 import jalview.util.MessageManager;
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.List;
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;
35 public class ParameterUtils
37 public static List<String> writeParameterSet(List<Option> optSet,
40 List<String> pset = new ArrayList<String>();
41 for (Option o : optSet)
43 pset.add(o.toCommand(pseparator));
49 * Converts options supplied via parameters file into {@code Option} objects
50 * (Refactored from compbio.ws.client.Jws2Client)
56 * @return List of Options of type T
60 * @SuppressWarnings(value = { "true" }) public static <T> List<Option<T>>
61 * processParameters(List<String> params, RunnerConfig<T> options, String
64 public static List<Option> processParameters(List<String> params,
65 RunnerConfig options, String pseparator)
67 List<Option> chosenOptions = new ArrayList<Option>();
68 for (String param : params)
71 if (isParameter(param, pseparator))
73 oname = getParamName(param, pseparator);
79 Option o = options.getArgumentByOptionName(oname);
82 System.out.println("WARN ignoring unsuppoted parameter: " + oname);
85 if (o instanceof Parameter)
87 o = copyParameter((Parameter) o);
96 o.setDefaultValue(isParameter(param, pseparator)
97 ? getParamValue(param, pseparator)
99 } catch (WrongParameterException e)
102 "Problem setting value for the parameter: " + param);
106 chosenOptions.add(o);
108 return chosenOptions;
111 static String getParamName(String fullName, String pseparator)
113 assert isParameter(fullName, pseparator);
114 return fullName.substring(0, fullName.indexOf(pseparator));
117 static String getParamValue(String fullName, String pseparator)
119 assert isParameter(fullName, pseparator);
120 return fullName.substring(fullName.indexOf(pseparator) + 1);
123 static boolean isParameter(String param, String pseparator)
125 return param.contains(pseparator);
128 public static Option copyOption(Option option)
130 Option copy = new Option(option.getName(), option.getDescription());
131 setOptionFrom(copy, option);
135 public static void setOptionFrom(Option copy, Option option)
137 copy.setName(option.getName());
138 copy.setDescription(option.getDescription());
139 copy.setBasicURL(option.getBasicURL());
140 copy.setFurtherDetails(option.getFurtherDetails());
141 copy.setRequired(option.isRequired());
142 List<String> names = option.getOptionNames();
145 if (names.size() == 1)
147 HashSet<String> st = new HashSet();
148 st.add(names.get(0));
149 copy.setOptionNames(st);
153 copy.addOptionNames(names.toArray(new String[] {}));
158 if (option.getDefaultValue() != null)
160 copy.setDefaultValue(option.getDefaultValue());
162 } catch (Exception ex)
164 ex.printStackTrace();
168 public static ValueConstrain copyValueConstrain(ValueConstrain vc)
172 ValueConstrain copy = new ValueConstrain();
173 if (vc.getMax() != null)
175 copy.setMax(vc.getMax().toString());
177 if (vc.getMin() != null)
179 copy.setMin(vc.getMin().toString());
181 if (vc.getType() != null)
183 copy.setType(vc.getType());
186 } catch (Exception e)
189 throw new Error(MessageManager.getString(
190 "error.implementation_error_couldnt_copy_value_constraint"));
194 public static Parameter copyParameter(Parameter parameter)
196 Parameter copy = new Parameter(parameter.getName(),
197 parameter.getDescription());
198 if (parameter.getValidValue() != null)
200 copy.setValidValue(copyValueConstrain(parameter.getValidValue()));
202 List<String> pv = parameter.getPossibleValues();
205 copy.addPossibleValues(pv.toArray(new String[] {}));
207 setOptionFrom(copy, parameter);