001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: ValueFormatter.java,v $
023   Revision 1.4  2004/05/05 22:47:37  markl
024   comment block updates
025
026   Revision 1.3  2003/01/19 09:34:27  markl
027   Javadoc & comment header updates.
028
029   Revision 1.2  2001/03/12 02:18:29  markl
030   Source code cleanup.
031
032   Revision 1.1  1999/06/08 07:03:02  markl
033   Initial revision
034   ----------------------------------------------------------------------------
035*/
036
037package kiwi.text;
038
039/** This class provides methods for converting primitive numeric and boolean
040 * values to and from string representations.
041 *
042 * @author Mark Lindner
043 */
044
045public class ValueFormatter
046  {
047  private ValueFormatter() {}
048
049  /** Format a byte value as a string.
050   *
051   * @param v The value to format.
052   * @return A string representation of the value.
053   */
054  
055  public String format(byte v)
056    {
057    return(String.valueOf(v));
058    }
059
060  /** Parse a byte representation.
061   *
062   * @param s The string to parse.
063   * @return The parsed value.
064   * @exception kiwi.text.ParsingException If parsing failed.
065   */
066  
067  public byte parseByte(String s) throws ParsingException
068    {
069    byte v = 0;
070    
071    try
072      {
073      v = (new Byte(s)).byteValue();
074      }
075    catch(NumberFormatException ex)
076      {
077      throw(new ParsingException(ex.getMessage()));
078      }
079
080    return(v);
081    }
082
083  /** Format an integer value as a string.
084   *
085   * @param v The value to format.
086   * @return A string representation of the value.
087   */
088  
089  public String format(int v)
090    {
091    return(String.valueOf(v));
092    }
093
094  /** Parse an integer representation.
095   *
096   * @param s The string to parse.
097   * @return The parsed value.
098   * @exception kiwi.text.ParsingException If parsing failed.
099   */
100  
101  public int parseInt(String s) throws ParsingException
102    {
103    int v = 0;
104    
105    try
106      {
107      v = Integer.parseInt(s);
108      }
109    catch(NumberFormatException ex)
110      {
111      throw(new ParsingException(ex.getMessage()));
112      }
113    
114    return(v);
115    }
116
117  /** Format a long integer value as a string.
118   *
119   * @param v The value to format.
120   * @return A string representation of the value.
121   */
122  
123  public String format(long v)
124    {
125    return(String.valueOf(v));
126    }
127
128  /** Parse a long integer representation.
129   *
130   * @param s The string to parse.
131   * @return The parsed value.
132   * @exception kiwi.text.ParsingException If parsing failed.
133   */
134  
135  public long parseLong(String s) throws ParsingException
136    {
137    long v = 0;
138    
139    try
140      {
141      v = (new Long(s)).longValue();
142      }
143    catch(NumberFormatException ex)
144      {
145      throw(new ParsingException(ex.getMessage()));
146      }
147
148    return(v);
149    }
150
151  /** Format a float value as a string.
152   *
153   * @param v The value to format.
154   * @return A string representation of the value.
155   */
156  
157  public String format(float v)
158    {
159    return(String.valueOf(v));
160    }
161
162  /** Parse a float representation.
163   *
164   * @param s The string to parse.
165   * @return The parsed value.
166   * @exception kiwi.text.ParsingException If parsing failed.
167   */
168  
169  public float parseFloat(String s) throws ParsingException
170    {
171    float v = 0;
172    
173    try
174      {
175      v = (new Float(s)).floatValue();
176      }
177    catch(NumberFormatException ex)
178      {
179      throw(new ParsingException(ex.getMessage()));
180      }
181
182    return(v);
183    }
184
185  /** Format a double precision value as a string.
186   *
187   * @param v The value to format.
188   * @return A string representation of the value.
189   */
190  
191  public String format(double v)
192    {
193    return(String.valueOf(v));
194    }
195
196  /** Parse a double precision representation.
197   *
198   * @param s The string to parse.
199   * @return The parsed value.
200   * @exception kiwi.text.ParsingException If parsing failed.
201   */
202  
203  public double parseDouble(String s) throws ParsingException
204    {
205    float v = 0;
206    
207    try
208      {
209      v = (new Float(s)).floatValue();
210      }
211    catch(NumberFormatException ex)
212      {
213      throw(new ParsingException(ex.getMessage()));
214      }
215
216    return(v);
217    }
218
219  /** Format a boolean value as a string.
220   *
221   * @param v The value to format.
222   * @return A string representation of the value.
223   */
224  
225  public String format(boolean v)
226    {
227    return(String.valueOf(v));
228    }
229
230  /** Parse a boolean representation.
231   *
232   * @param s The string to parse.
233   * @return The parsed value.
234   */
235  
236  public boolean parseBoolean(String s)
237    {
238    return((new Boolean(s)).booleanValue());
239    }
240
241  }
242
243/* end of source file */