Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/scopemvc/util/convertor/NumberStringConvertor.java


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