View Javadoc

1   /*
2      Copyright 2007 Ivo Limmen
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package org.limmen.crs.model.convert;
17  
18  import java.text.ParseException;
19  import java.text.SimpleDateFormat;
20  import java.util.Date;
21  
22  import org.limmen.crs.Constants;
23  import org.limmen.crs.model.SettingType;
24  
25  public class ValueConverter {
26  
27  	private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(Constants.DATE_FORMAT);
28  
29  	public static String convertFrom(Object value, SettingType type) {
30  
31  		if (value == null) {
32  
33  			return null;
34  		}
35  		else if (value instanceof Date
36  				|| type.equals(SettingType.DATE)) {
37  
38  			return convertFromDate(value, type);
39  		}
40  		else if (value instanceof String[]
41  		      || type.equals(SettingType.MULTISTRING)) {
42  
43  			return convertFromMultiString(value, type);
44  		}
45  		else if (value instanceof Integer
46  				|| type.equals(SettingType.INTEGER)) {
47  
48  			return convertFromInteger(value, type);
49  		}
50  		else if (value instanceof Boolean
51  				|| type.equals(SettingType.BOOL)) {
52  
53  			return convertFromBoolean(value, type);
54  		}
55  		else if (value instanceof Double
56  				|| type.equals(SettingType.DOUBLE)) {
57  
58  			return convertFromDouble(value, type);
59  		}
60  		else if (value instanceof String
61  				|| type.equals(SettingType.STRING)) {
62  
63  			return convertFromString(value, type);
64  		}
65  		else {
66  
67  			throw new IllegalStateException(
68  					String.format("Unknown type ", type));
69  		}
70  	}
71  
72  	private static String convertFromBoolean(Object value, SettingType type) {
73  
74  		if (!type.equals(SettingType.BOOL)) {
75  
76  			throw new IllegalArgumentException(
77  					String.format("Setting is not of type bool (%0$s)", type));
78  		}
79  
80  		if (value instanceof Boolean) {
81  
82  			Boolean booleanValue = (Boolean) value;
83  
84  			return booleanValue.toString();
85  		}
86  		else if (value instanceof String) {
87  
88  			String stringValue = (String) value;
89  
90  			if (stringValue.equals("true")
91  					|| stringValue.equals("on")) {
92  
93  				return Boolean.TRUE.toString();
94  			}
95  			else if (stringValue.equals("false")
96  					|| stringValue.equals("off")) {
97  
98  				return Boolean.FALSE.toString();
99  			}
100 		}
101 
102 		throw new IllegalArgumentException(String.format("Value could not be parsed as bool (value=%0$s)", value.toString()));
103 	}
104 
105 	private static String convertFromDate(Object value, SettingType type) {
106 
107 		if (!type.equals(SettingType.DATE)) {
108 
109 			throw new IllegalArgumentException(
110 					String.format("Setting is not of type date (%0$s)", type));
111 		}
112 
113 		if (value instanceof Date) {
114 
115 			Date dateValue = (Date) value;
116 
117 			return DATE_FORMATTER.format(dateValue);
118 		}
119 		else if (value instanceof String) {
120 
121 			String stringValue = (String) value;
122 
123 			try {
124 
125 				DATE_FORMATTER.parse(stringValue);
126          }
127          catch (ParseException e) {
128 
129          	throw new IllegalArgumentException(
130          			String.format(
131          					"Value could not be parsed as date (value=%0$s)",
132          					value.toString()));
133          }
134 
135 			return stringValue;
136 		}
137 
138 		throw new IllegalArgumentException(
139 				String.format(
140 						"Value could not be parsed as date (value=%0$s)",
141 						value.toString()));
142 	}
143 
144 	private static String convertFromDouble(Object value, SettingType type) {
145 
146 		if (!type.equals(SettingType.DOUBLE)) {
147 
148 			throw new IllegalArgumentException(String.format("Setting is not of type double (%0$s)", type));
149 		}
150 
151 		if (value instanceof Double) {
152 
153 			Double doubleValue = (Double) value;
154 
155 			return doubleValue.toString();
156 		}
157 		else if (value instanceof String) {
158 
159 			String stringValue = (String) value;
160 
161 			try {
162 	         Double.parseDouble(stringValue);
163          }
164          catch (NumberFormatException e) {
165 
166       		throw new IllegalArgumentException(
167       				String.format(
168       						"Value could not be parsed as double (value=%0$s)",
169       						value.toString()));
170          }
171 
172          return stringValue;
173 		}
174 
175 		throw new IllegalArgumentException(
176 				String.format(
177 						"Value could not be parsed as double (value=%0$s)",
178 						value.toString()));
179 	}
180 
181 	private static String convertFromInteger(Object value, SettingType type) {
182 
183 		if (!type.equals(SettingType.INTEGER)) {
184 
185 			throw new IllegalArgumentException(String.format("Setting is not of type integer (%0$s)", type));
186 		}
187 
188 		if (value instanceof Integer) {
189 
190 			Integer integerValue = (Integer) value;
191 
192 			return integerValue.toString();
193 		}
194 		else if (value instanceof String) {
195 
196 			String stringValue = (String) value;
197 
198 			try {
199 				Integer.parseInt(stringValue);
200          }
201          catch (NumberFormatException e) {
202 
203       		throw new IllegalArgumentException(
204       				String.format(
205       						"Value could not be parsed as integer (value=%0$s)",
206       						value.toString()));
207          }
208 
209          return stringValue;
210 		}
211 
212 		throw new IllegalArgumentException(
213 				String.format(
214 						"Value could not be parsed as integer (value=%0$s)",
215 						value.toString()));
216 		}
217 
218 	private static String convertFromMultiString(Object values, SettingType type) {
219 
220 		if (!type.equals(SettingType.MULTISTRING)) {
221 
222 			throw new IllegalArgumentException(String.format("Setting is not of type multistring (%0$s)", type));
223 		}
224 
225 		if (values instanceof String[]) {
226 
227 			String[] arrayStringValue = (String[])values;
228 			StringBuilder builder = new StringBuilder();
229 
230 			for (String value : arrayStringValue) {
231 
232 				builder.append(value);
233 				builder.append(Constants.MULTISTRING_DELIMITER);
234 			}
235 
236 			return builder.substring(0, builder.length() - 1);
237 		}
238 		else if (values instanceof String) {
239 
240 			String stringValue = (String) values;
241 
242 			if (stringValue.indexOf(Constants.MULTISTRING_DELIMITER) == -1) {
243 
244 				throw new IllegalArgumentException(
245 						String.format(
246 								"Value could not be parsed as multistring (value=%0$s)",
247 								values.toString()));
248 			}
249 
250 			return stringValue;
251 		}
252 
253 		throw new IllegalArgumentException(
254 				String.format(
255 						"Value could not be parsed as multistring (value=%0$s)",
256 						values.toString()));
257 	}
258 
259 	private static String convertFromString(Object value, SettingType type) {
260 
261 		if (!type.equals(SettingType.STRING)) {
262 
263 			throw new IllegalArgumentException(String.format("Setting is not of type string (%0$s)", type));
264 		}
265 
266 		return value.toString();
267 	}
268 }