KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.data.parser;
2
3 /**
4  * DataParser instance that parses double values from a text string.
5  *
6  * @author <a HREF="http://jheer.org">jeffrey heer</a>
7  */

8 public class DoubleParser implements DataParser {
9     
10     private boolean m_blockExplicitFloats = true;
11     
12     /**
13      * Returns double.class.
14      * @see prefuse.data.parser.DataParser#getType()
15      */

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

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

34     public boolean canParse(String JavaDoc text) {
35         try {
36             if ( m_blockExplicitFloats && text.endsWith("f") ) {
37                 // don't try to convert floats
38
return false;
39             }
40             Double.parseDouble(text);
41             return true;
42         } catch ( NumberFormatException JavaDoc e ) {
43             return false;
44         }
45     }
46     
47     /**
48      * @see prefuse.data.parser.DataParser#parse(java.lang.String)
49      */

50     public Object JavaDoc parse(String JavaDoc text) throws DataParseException {
51         return new Double JavaDoc(parseDouble(text));
52     }
53     
54     /**
55      * Parse a double value from a text string.
56      * @param text the text string to parse
57      * @return the parsed double value
58      * @throws DataParseException if an error occurs during parsing
59      */

60     public static double parseDouble(String JavaDoc text) throws DataParseException {
61         try {
62             return Double.parseDouble(text);
63         } catch ( NumberFormatException JavaDoc e ) {
64             throw new DataParseException(e);
65         }
66     }
67     
68 } // end of class DoubleParser
69
Popular Tags