utilities for cloning and manipulating JabaWS parameter objects.
[jalview.git] / src / jalview / ws / jws2 / ParameterUtils.java
1 package jalview.ws.jws2;
2
3 import java.util.HashSet;
4 import java.util.List;
5
6 import compbio.metadata.*;
7 public class ParameterUtils
8 {
9
10   public static Option copyOption(Option option)
11   {
12     Option copy = new Option(option.getName(), option.getDescription());
13     setOptionFrom(copy, option);
14     return copy;
15   }
16   public static void setOptionFrom(Option copy, Option option)
17   {
18     copy.setName(option.getName());
19     copy.setDescription(option.getDescription());
20     copy.setFurtherDetails(option.getFurtherDetails());
21     copy.setRequired(option.isRequired());
22     List<String> names = option.getOptionNames();
23     if (names!=null){
24       if (names.size()==1)
25       {
26         HashSet<String> st = new HashSet();
27         st.add(names.get(0));
28         copy.setOptionNames(st);
29       } else {
30       copy.addOptionNames(names.toArray(new String[]{}));
31       }
32     }
33     try {
34       if (option.getDefaultValue()!=null)
35       {
36         copy.setDefaultValue(option.getDefaultValue());
37       }
38     } catch (Exception ex)
39     {
40       ex.printStackTrace();
41     }
42   }
43
44   public static ValueConstrain copyValueConstrain(ValueConstrain vc)
45   {
46     try {ValueConstrain copy = new ValueConstrain();
47     if (vc.getMax()!=null) {
48       copy.setMax(vc.getMax().toString());
49     }
50     if (vc.getMin()!=null) {
51       copy.setMin(vc.getMin().toString());
52     }
53     if (vc.getType()!=null) {
54       copy.setType(vc.getType());
55     }
56     return copy;
57     }
58     catch (Exception e)
59     {
60       e.printStackTrace();
61       throw new Error("Implementation error: could not copy ValueConstrain!");
62     }
63   }
64   public static Parameter copyParameter(Parameter parameter)
65   {
66     Parameter copy = new Parameter(parameter.getName(), parameter.getDescription());
67     if (parameter.getValidValue()!=null) {
68       copy.setValidValue(copyValueConstrain(parameter.getValidValue()));
69     }
70     List<String> pv = parameter.getPossibleValues();
71     if (pv!=null)
72     {
73       copy.addPossibleValues(pv.toArray(new String[]{}));
74     }
75     setOptionFrom(copy, parameter);
76     return copy;
77   }
78
79 }