Next version of JABA
[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                     option.setDefaultValue(getValue(optionName));\r
115                 }\r
116                 options.add(option);\r
117             }\r
118         }\r
119         return options;\r
120     }\r
121 \r
122     boolean containValue(String option) {\r
123         if (option.trim().contains(SPACE)) {\r
124             return true;\r
125         }\r
126         return false;\r
127     }\r
128 \r
129     String getName(String option) {\r
130         option = option.trim();\r
131         if (containValue(option)) {\r
132             return option.substring(0, option.indexOf(SPACE)).trim();\r
133         }\r
134         return option;\r
135     }\r
136 \r
137     String getValue(String option) {\r
138         assert containValue(option);\r
139         option = option.trim();\r
140         return option.substring(option.indexOf(SPACE) + 1).trim();\r
141     }\r
142 \r
143     @Override\r
144     public String toString() {\r
145         String value = "Preset name: '" + name + "'" + SysPrefs.newlinechar;\r
146         value += "Description: " + description + SysPrefs.newlinechar;\r
147         value += "Options: " + SysPrefs.newlinechar;\r
148         for (String oname : this.option) {\r
149             value += oname + SysPrefs.newlinechar;\r
150         }\r
151         value += SysPrefs.newlinechar;\r
152         return value;\r
153     }\r
154 \r
155     @Override\r
156     public int hashCode() {\r
157         final int prime = 31;\r
158         int result = 1;\r
159         result = prime * result\r
160                 + ((description == null) ? 0 : description.hashCode());\r
161         result = prime * result + ((name == null) ? 0 : name.hashCode());\r
162         result = prime * result + ((option == null) ? 0 : option.hashCode());\r
163         return result;\r
164     }\r
165 \r
166     @Override\r
167     public boolean equals(Object obj) {\r
168         if (this == obj)\r
169             return true;\r
170         if (obj == null)\r
171             return false;\r
172         if (getClass() != obj.getClass())\r
173             return false;\r
174         Preset other = (Preset) obj;\r
175         if (description == null) {\r
176             if (other.description != null)\r
177                 return false;\r
178         } else if (!description.equals(other.description))\r
179             return false;\r
180         if (name == null) {\r
181             if (other.name != null)\r
182                 return false;\r
183         } else if (!name.equals(other.name))\r
184             return false;\r
185         if (option == null) {\r
186             if (other.option != null)\r
187                 return false;\r
188         } else if (!option.equals(other.option))\r
189             return false;\r
190         return true;\r
191     }\r
192 \r
193 }\r