1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, The Scope team
4 * All rights reserved.
5 *
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * Neither the name "Scope" nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 *
36 * $Id: NumberStringConvertor.java,v 1.5 2002/09/05 15:41:47 ludovicc Exp $
37 */
38 package org.scopemvc.util.convertor;
39
40
41 import java.text.NumberFormat;
42 import java.text.ParseException;
43
44 /***
45 * Abstract base class for numeric StringConvertors. <p>
46 *
47 * It uses default <code>java.text.Number</code> format. New format can be set.
48 * </p>
49 *
50 * @author <A HREF="mailto:danmi@users.sourceforge.net">Daniel Michalik</A>
51 * @created 05 September 2002
52 * @version $Revision: 1.5 $ $Date: 2002/09/05 15:41:47 $
53 */
54 public abstract class NumberStringConvertor extends NullStringConvertor {
55
56 private NumberFormat format;
57
58
59 /***
60 * Creates new NumberStringConvertor with default platform number format.
61 *
62 * @see java.text.NumberFormat#getInstance()
63 */
64 public NumberStringConvertor() {
65 format = NumberFormat.getInstance();
66 }
67
68 /***
69 * @return a instance of format used in this convertor. The value is never
70 * null.
71 */
72 public NumberFormat getNumberFormat() {
73 return format;
74 }
75
76 /***
77 * @param inFormat The new numberFormat value
78 * @throws IllegalArgumentException if passed format is null.
79 */
80 public void setNumberFormat(NumberFormat inFormat)
81 throws IllegalArgumentException {
82 if (inFormat == null) {
83 throw new IllegalArgumentException("Passed number format cannot "
84 + "be null");
85 }
86 format = inFormat;
87 }
88
89
90 /***
91 * Returns instance of some subclass of {@link java.lang.Number Number} as
92 * returned by {@link java.text.NumberFormat NumberFormat}. If there is
93 * required specific numeric class, corresponding <code>XXXStringConvertor</code>
94 * should be used. Subclasses use this method and result converts to proper
95 * type. <p>
96 *
97 * Empty, <code>null</code> and {@link #getNullAsString() getNullAsString()}
98 * strings are converted into <code>null</code>. </p>
99 *
100 * @param inString TODO: Describe the Parameter
101 * @return TODO: Describe the Return Value
102 * @see DoubleStringConvertor
103 * @see FloatStringConvertor
104 * @see IntegerStringConvertor
105 * @see LongStringConvertor
106 * @throws IllegalArgumentException can't convert from String using current
107 * NumberFormat.
108 */
109 public Object stringAsValue(String inString)
110 throws IllegalArgumentException {
111 if (isNull(inString)) {
112 return null;
113 }
114 try {
115 return format.parse(inString);
116 } catch (ParseException ex) {
117 throw new IllegalArgumentException(ex.getMessage());
118 }
119 }
120
121 /***
122 * @param inValue TODO: Describe the Parameter
123 * @return text representation of numeric object. For null argument is
124 * called method {@link #getNullAsString() getNullAsString()}
125 * @throws IllegalArgumentException when argument is not subclass of
126 * java.lang.Number
127 */
128 public String valueAsString(Object inValue)
129 throws IllegalArgumentException {
130 if (inValue == null) {
131 return getNullAsString();
132 }
133 if (!(inValue instanceof Number)) {
134 throw new IllegalArgumentException("Passed object is not subclass "
135 + "of java.lang.Number. Its class is " + inValue.getClass());
136 }
137 return format.format(inValue);
138 }
139 }
This page was automatically generated by Maven