Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ParseConverter |
|
| 4.5;4.5 |
1 | /* | |
2 | * Copyright 2003-2004 The Apache Software Foundation | |
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.apache.commons.convert1.format; | |
17 | ||
18 | ||
19 | import org.apache.commons.convert1.ConversionException; | |
20 | import org.apache.commons.convert1.Converter; | |
21 | ||
22 | import java.text.Format; | |
23 | import java.text.ParseException; | |
24 | ||
25 | /** | |
26 | * java.text.Format.parseObject(String); Object based converter. | |
27 | * | |
28 | * @author Henri Yandell | |
29 | * @version $Id: ParseConverter.java 155441 2005-02-26 13:19:22Z dirkv $ | |
30 | * @since 0.1 | |
31 | */ | |
32 | ||
33 | public final class ParseConverter implements Converter { | |
34 | ||
35 | private Format format; | |
36 | ||
37 | //H? Should this have a Class paramter fortype checking? | |
38 | 0 | public ParseConverter(Format format) { |
39 | 0 | this.format = format; |
40 | 0 | } |
41 | ||
42 | /** | |
43 | * Parse the specified input String into an Object | |
44 | * | |
45 | * @param type Data type to which this value should be converted | |
46 | * @param value The input value to be converted | |
47 | * | |
48 | * @exception ConversionException if conversion cannot be performed | |
49 | * successfully | |
50 | */ | |
51 | public Object convert(Class type, Object value) { | |
52 | 0 | if(value == null) { |
53 | 0 | return null; //H? ?? error? |
54 | } | |
55 | ||
56 | 0 | if(value.getClass() != String.class) { |
57 | 0 | throw new ConversionException("ParseConverter must parse Strings. "); |
58 | } | |
59 | ||
60 | try { | |
61 | 0 | return this.format.parseObject(value.toString()); |
62 | 0 | } catch(ParseException pe) { |
63 | 0 | throw new ConversionException("Error in parsing: "+value, pe); |
64 | } | |
65 | } | |
66 | ||
67 | ||
68 | } |