be9b8f1ba9a59a2abfac059832a418f3dc22d236
[jabaws.git] / datamodel / compbio / metadata / Preset.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0\r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.metadata;\r
20 \r
21 import java.util.ArrayList;\r
22 import java.util.List;\r
23 \r
24 import javax.xml.bind.annotation.XmlAccessType;\r
25 import javax.xml.bind.annotation.XmlAccessorType;\r
26 import javax.xml.bind.annotation.XmlElement;\r
27 import javax.xml.bind.annotation.XmlElementWrapper;\r
28 import javax.xml.bind.annotation.XmlTransient;\r
29 \r
30 import compbio.util.SysPrefs;\r
31 \r
32 /**\r
33  * Collection of Options and Parameters with their values\r
34  * \r
35  * @see Option\r
36  * @see Parameter\r
37  * \r
38  * @author pvtroshin\r
39  * \r
40  *         Date December 2009\r
41  * @param <T>\r
42  *            executable type\r
43  */\r
44 @XmlAccessorType(XmlAccessType.FIELD)\r
45 public class Preset<T> {\r
46 \r
47     @XmlTransient\r
48     private static final String SPACE = " ";\r
49 \r
50     @XmlElement(required = true, nillable = false)\r
51     // @XmlID - ? require no spaces (!)\r
52     String name;\r
53 \r
54     String description;\r
55 \r
56     @XmlElement(required = true, nillable = false)\r
57     @XmlElementWrapper(name = "optlist")\r
58     List<String> option;\r
59 \r
60     public void setOptions(List<String> option) {\r
61         this.option = option;\r
62     }\r
63 \r
64     public void setName(String name) {\r
65         this.name = name;\r
66     }\r
67 \r
68     public void setDescription(String description) {\r
69         this.description = description;\r
70     }\r
71 \r
72     /**\r
73      * @return a List of Options as a String\r
74      */\r
75     public List<String> getOptions() {\r
76         return new ArrayList<String>(option);\r
77     }\r
78 \r
79     /**\r
80      * \r
81      * @return - name of the Preset\r
82      */\r
83     public String getName() {\r
84         return name;\r
85     }\r
86 \r
87     /**\r
88      * \r
89      * @return - a long description of the Preset\r
90      */\r
91     public String getDescription() {\r
92         return description;\r
93     }\r
94 \r
95     /**\r
96      * Converts list of options as String to type Option\r
97      * \r
98      * @param rconfig\r
99      * @return List of Options\r
100      * @throws WrongParameterException\r
101      *             if the value of the parameter is invalid @see\r
102      *             {@link Parameter}\r
103      */\r
104     public List<Option<T>> getArguments(RunnerConfig<T> rconfig)\r
105             throws WrongParameterException {\r
106         List<Option<T>> options = new ArrayList<Option<T>>();\r
107         for (String optionName : option) {\r
108             optionName = optionName.trim();\r
109             String oname = getName(optionName);\r
110             Option<T> option = rconfig.getArgumentByOptionName(oname);\r
111             if (option != null) {\r
112                 // Set default value to the preset value\r
113                 if (containValue(optionName)) {\r
114                     // extract and set value to the parameter \r
115                     option.setDefaultValue(getValue(optionName));\r
116                 } else {\r
117                     // set value to the option as default, as this could be a multi-option value\r
118                     option.setDefaultValue(oname);\r
119                 }\r
120                 options.add(option);\r
121             }\r
122         }\r
123         return options;\r
124     }\r
125 \r
126     boolean containValue(String option) {\r
127         if (option.trim().contains(SPACE)) {\r
128             return true;\r
129         }\r
130         return false;\r
131     }\r
132 \r
133     String getName(String option) {\r
134         option = option.trim();\r
135         if (containValue(option)) {\r
136             return option.substring(0, option.indexOf(SPACE)).trim();\r
137         }\r
138         return option;\r
139     }\r
140 \r
141     String getValue(String option) {\r
142         assert containValue(option);\r
143         option = option.trim();\r
144         return option.substring(option.indexOf(SPACE) + 1).trim();\r
145     }\r
146 \r
147     @Override\r
148     public String toString() {\r
149         String value = "Preset name: '" + name + "'" + SysPrefs.newlinechar;\r
150         value += "Description: " + description + SysPrefs.newlinechar;\r
151         value += "Options: " + SysPrefs.newlinechar;\r
152         for (String oname : this.option) {\r
153             value += oname + SysPrefs.newlinechar;\r
154         }\r
155         value += SysPrefs.newlinechar;\r
156         return value;\r
157     }\r
158 \r
159     @Override\r
160     public int hashCode() {\r
161         final int prime = 31;\r
162         int result = 1;\r
163         result = prime * result\r
164                 + ((description == null) ? 0 : description.hashCode());\r
165         result = prime * result + ((name == null) ? 0 : name.hashCode());\r
166         result = prime * result + ((option == null) ? 0 : option.hashCode());\r
167         return result;\r
168     }\r
169 \r
170     @Override\r
171     public boolean equals(Object obj) {\r
172         if (this == obj)\r
173             return true;\r
174         if (obj == null)\r
175             return false;\r
176         if (getClass() != obj.getClass())\r
177             return false;\r
178         Preset other = (Preset) obj;\r
179         if (description == null) {\r
180             if (other.description != null)\r
181                 return false;\r
182         } else if (!description.equals(other.description))\r
183             return false;\r
184         if (name == null) {\r
185             if (other.name != null)\r
186                 return false;\r
187         } else if (!name.equals(other.name))\r
188             return false;\r
189         if (option == null) {\r
190             if (other.option != null)\r
191                 return false;\r
192         } else if (!option.equals(other.option))\r
193             return false;\r
194         return true;\r
195     }\r
196 \r
197 }\r