KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

17     public Class JavaDoc getType() {
18         return long.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).longValue())+"L";
30     }
31     
32     /**
33      * @see prefuse.data.parser.DataParser#canParse(java.lang.String)
34      */

35     public boolean canParse(String JavaDoc text) {
36         try {
37             parseLong(text);
38             return true;
39         } catch ( DataParseException 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 Long JavaDoc(parseLong(text));
49     }
50     
51     /**
52      * Parse a long value from a text string.
53      * @param text the text string to parse
54      * @return the parsed long value
55      * @throws DataParseException if an error occurs during parsing
56      */

57     public static long parseLong(String JavaDoc text) throws DataParseException {
58         try {
59             // allow trailing 'L' characters to signify a long
60
if ( text.length() > 0 ) {
61                 char c = text.charAt(text.length()-1);
62                 if ( c == 'l' || c == 'L' )
63                     text = text.substring(0,text.length()-1);
64             }
65             // parse the string
66
return Long.parseLong(text);
67         } catch ( NumberFormatException JavaDoc e ) {
68             throw new DataParseException(e);
69         }
70     }
71     
72 } // end of class LongParser
73
Popular Tags