KickJava   Java API By Example, From Geeks To Geeks.

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


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

8 public class IntParser implements DataParser {
9     
10     /**
11      * Returns int.class.
12      * @see prefuse.data.parser.DataParser#getType()
13      */

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

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

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

44     public Object JavaDoc parse(String JavaDoc text) throws DataParseException {
45         return new Integer JavaDoc(parseInt(text));
46     }
47     
48     /**
49      * Parse an int value from a text string.
50      * @param text the text string to parse
51      * @return the parsed int value
52      * @throws DataParseException if an error occurs during parsing
53      */

54     public static int parseInt(String JavaDoc text) throws DataParseException {
55         try {
56             return Integer.parseInt(text);
57         } catch ( NumberFormatException JavaDoc e ) {
58             throw new DataParseException(e);
59         }
60     }
61     
62 } // end of class IntParser
63
Popular Tags