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.string;
17
18
19 import org.apache.commons.convert1.ConversionException;
20 import org.apache.commons.convert1.Converter;
21
22
23 /**
24 * <p>Standard {@link Converter} implementation that converts an incoming
25 * String into a <code>java.lang.Integer</code> object, optionally using a
26 * default value or throwing a {@link ConversionException} if a conversion
27 * error occurs.</p>
28 *
29 * @author Craig R. McClanahan
30 * @version $Id: IntegerConverter.java 155441 2005-02-26 13:19:22Z dirkv $
31 * @since 0.1
32 */
33
34 public final class IntegerConverter implements Converter {
35
36
37 // ----------------------------------------------------------- Constructors
38
39
40 /**
41 * Create a {@link Converter} that will throw a {@link ConversionException}
42 * if a conversion error occurs.
43 */
44 public IntegerConverter() {
45
46 this.defaultValue = null;
47 this.useDefault = false;
48
49 }
50
51
52 /**
53 * Create a {@link Converter} that will return the specified default value
54 * if a conversion error occurs.
55 *
56 * @param defaultValue The default value to be returned
57 */
58 public IntegerConverter(Object defaultValue) {
59
60 this.defaultValue = defaultValue;
61 this.useDefault = true;
62
63 }
64
65
66 // ----------------------------------------------------- Instance Variables
67
68
69 /**
70 * The default value specified to our Constructor, if any.
71 */
72 private Object defaultValue = null;
73
74
75 /**
76 * Should we return the default value on conversion errors?
77 */
78 private boolean useDefault = true;
79
80
81 // --------------------------------------------------------- Public Methods
82
83
84 /**
85 * Convert the specified input object into an output object of the
86 * specified type.
87 *
88 * @param type Data type to which this value should be converted
89 * @param value The input value to be converted
90 *
91 * @exception ConversionException if conversion cannot be performed
92 * successfully
93 */
94 public Object convert(Class type, Object value) {
95
96 if (value == null) {
97 if (useDefault) {
98 return (defaultValue);
99 } else {
100 throw new ConversionException("No value specified");
101 }
102 }
103
104 if (value instanceof Integer) {
105 return (value);
106 } else if(value instanceof Number) {
107 return new Integer(((Number)value).intValue());
108 }
109
110 try {
111 return (new Integer(value.toString()));
112 } catch (Exception e) {
113 if (useDefault) {
114 return (defaultValue);
115 } else {
116 throw new ConversionException(e);
117 }
118 }
119
120 }
121
122
123 }