KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > parser > FloatParser


1 package prefuse.data.parser;
2
3 /**
4  * DataParser instance that parses float values from a text string. Float
5  * values can be explicitly coded for by using a 'f' at the end of a
6  * number. For example "1.0" could parse as a double or a float, but
7  * "1.0f" will only parse as a float.
8  *
9  * @author <a HREF="http://jheer.org">jeffrey heer</a>
10  */

11 public class FloatParser implements DataParser {
12     
13     /**
14      * Returns float.class.
15      * @see prefuse.data.parser.DataParser#getType()
16      */

17     public Class JavaDoc getType() {
18         return float.class;
19     }
20     
21     /**
22      * @see prefuse.data.parser.DataParser#format(java.lang.Object)
23      */

24     public String JavaDoc format(Object JavaDoc value) {
25         if ( value == null ) return null;
26         if ( !(value instanceof Number JavaDoc) )
27             throw new IllegalArgumentException JavaDoc(
28               "This class can only format Objects of type Number.");
29         return String.valueOf(((Number JavaDoc)value).floatValue())+"f";
30     }
31     
32     /**
33      * @see prefuse.data.parser.DataParser#canParse(java.lang.String)
34      */

35     public boolean canParse(String JavaDoc text) {
36         try {
37             Float.parseFloat(text);
38             return true;
39         } catch ( NumberFormatException JavaDoc e ) {
40             return false;
41         }
42     }
43
44     /**
45      * @see prefuse.data.parser.DataParser#parse(java.lang.String)
46      */

47     public Object JavaDoc parse(String JavaDoc text) throws DataParseException {
48         return new Float JavaDoc(parseFloat(text));
49     }
50     
51     /**
52      * Parse a float value from a text string.
53      * @param text the text string to parse
54      * @return the parsed float value
55      * @throws DataParseException if an error occurs during parsing
56      */

57     public static float parseFloat(String JavaDoc text) throws DataParseException {
58         try {
59             return Float.parseFloat(text);
60         } catch ( NumberFormatException JavaDoc e ) {
61             throw new DataParseException(e);
62         }
63     }
64     
65 } // end of class FloatParser
66
Popular Tags