1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.limmen.crs.model;
17
18 import java.text.ParseException;
19 import java.text.SimpleDateFormat;
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.List;
23
24 import org.limmen.crs.Constants;
25 import org.limmen.crs.exception.InvalidSettingTypeException;
26 import org.limmen.crs.model.convert.ValueConverter;
27
28 /***
29 * The Setting class is responsible for holding the actual configuration setting
30 * and guarding it's type.
31 *
32 * @author Ivo Limmen
33 * @see SettingType
34 */
35 public class Setting implements java.io.Serializable {
36
37 private static final long serialVersionUID = 3208460995141327059L;
38
39 private final List<Documentation> documentation = new ArrayList<Documentation>();
40
41 private final String id;
42
43 private SettingType type;
44
45 /***
46 * The string representation of the value of this setting.
47 */
48 private String value;
49
50 public Setting(String id) {
51
52 this(id, null, SettingType.STRING);
53 }
54
55 public Setting(String id, Object value) {
56
57 this(id, value, SettingType.STRING);
58 }
59
60 public Setting(String id, Object value, SettingType type) {
61
62 super();
63
64 this.id = id;
65 setType(type);
66 setValue(value);
67 }
68
69 Documentation addDocumentation(Documentation documentation) {
70
71 getDocumentation().add(documentation);
72
73 return documentation;
74 }
75
76 @Override
77 public boolean equals(Object obj) {
78
79 if (obj.getClass().equals(getClass())) {
80
81 Setting other = (Setting) obj;
82
83 if (getId().equals(other.getId())) {
84
85 if (getType().equals(other.getType())) {
86
87 if (getValue().equals(other.getValue())) {
88
89 return true;
90 }
91 }
92 }
93 }
94
95 return false;
96 }
97
98 public Boolean getBooleanValue() throws InvalidSettingTypeException {
99
100 if (!getType().equals(SettingType.BOOL)) {
101
102 throw new InvalidSettingTypeException(
103 String.format(
104 "Setting %0$s is not of requested type boolean (%1$s)",
105 getId(),
106 getType()));
107 }
108
109 return Boolean.parseBoolean(getValue());
110 }
111
112 public Date getDateValue() throws InvalidSettingTypeException {
113
114 if (!getType().equals(SettingType.DATE)) {
115
116 throw new InvalidSettingTypeException(
117 String.format(
118 "Setting %0$s is not of requested type date (%1$s)",
119 getId(),
120 getType()));
121 }
122
123 SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.DATE_FORMAT);
124
125 try {
126 return dateFormat.parse(getValue());
127 }
128 catch (ParseException pe) {
129
130 throw new InvalidSettingTypeException(pe);
131 }
132 }
133
134 List<Documentation> getDocumentation() {
135
136 return documentation;
137 }
138
139 public Double getDoubleValue() throws InvalidSettingTypeException {
140
141 if (!getType().equals(SettingType.DOUBLE)) {
142
143 throw new InvalidSettingTypeException(
144 String.format(
145 "Setting %0$s is not of requested type double (%1$s)",
146 getId(),
147 getType()));
148 }
149
150 return Double.parseDouble(getValue());
151 }
152
153 public String getId() {
154
155 return this.id;
156 }
157
158 public Integer getIntegerValue() throws InvalidSettingTypeException {
159
160 if (!getType().equals(SettingType.INTEGER)) {
161
162 throw new InvalidSettingTypeException(
163 String.format(
164 "Setting %0$s is not of requested type integer (%1$s)",
165 getId(),
166 getType()));
167 }
168
169 return Integer.parseInt(getValue());
170 }
171
172 public String[] getMultiStringValue() throws InvalidSettingTypeException {
173
174 if (!getType().equals(SettingType.MULTISTRING)) {
175
176 throw new InvalidSettingTypeException(
177 String.format(
178 "Setting %0$s is not of requested type multistring (%1$s)",
179 getId(),
180 getType()));
181 }
182
183 return getValue().split(Constants.MULTISTRING_DELIMITER);
184 }
185
186 public String getStringValue() throws InvalidSettingTypeException {
187
188 if (!getType().equals(SettingType.STRING)) {
189
190 throw new InvalidSettingTypeException(
191 String.format(
192 "Setting %0$s is not of requested type string (%1$s)",
193 getId(),
194 getType()));
195 }
196
197 return getValue();
198 }
199
200 public SettingType getType() {
201
202 return type;
203 }
204
205 public String getValue() {
206
207 return this.value;
208 }
209
210 @Override
211 public int hashCode() {
212
213 final int prime = 31;
214 int result = 1;
215 result = prime * result + ((id == null) ? 0 : id.hashCode());
216 result = prime * result + ((type == null) ? 0 : type.hashCode());
217 result = prime * result + ((value == null) ? 0 : value.hashCode());
218 return result;
219 }
220
221 public void setType(SettingType type) {
222
223 this.type = type;
224 }
225
226 public void setValue(Object value) {
227
228 this.value = ValueConverter.convertFrom(value, getType());
229 }
230 }