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