KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > tokens > TokenLong


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solutions Corp. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on 2004-11-7
10  *
11  * Author Ben Yu
12  */

13 package jfun.parsec.tokens;
14
15 import jfun.parsec.Tokenizer;
16
17 /**
18  * The token that represents an integer that is within 64 bits.
19  * For bigger integer, use TokenDecimal instead.
20  * @author Ben Yu
21  *
22  * 2004-11-7
23  */

24 @Deprecated JavaDoc
25 public class TokenLong implements java.io.Serializable JavaDoc{
26   private TokenLong(){}
27   private static int toDecDigit(final char c){
28     return c - '0';
29   }
30   private static int toOctDigit(final char c){
31     return c - '0';
32   }
33   private static int toHexDigit(final char c){
34     if(c>='0' && c<='9') return c-'0';
35     if(c>='a' && c<='h') return c-'a'+10;
36     else return c-'A'+10;
37   }
38   private static final Tokenizer dTokenizer = new Tokenizer(){
39     public Object JavaDoc toToken(final CharSequence JavaDoc cs, final int from, final int len){
40       final int end = from + len;
41       long n = 0;
42       for(int i=from; i<end; i++){
43         n = n*10 + toDecDigit(cs.charAt(i));
44       }
45       return new Long JavaDoc(n);
46     }
47   };
48   private static final Tokenizer oTokenizer = new Tokenizer(){
49     public Object JavaDoc toToken(final CharSequence JavaDoc cs, final int from, final int len){
50       final int end = from + len;
51       long n = 0;
52       for(int i=from; i<end; i++){
53         n = n*8 + toOctDigit(cs.charAt(i));
54       }
55       return new Long JavaDoc(n);
56     }
57   };
58   private static final Tokenizer hTokenizer = new Tokenizer(){
59     public Object JavaDoc toToken(final CharSequence JavaDoc cs, final int from, final int len){
60       if(len < 3) throw new IllegalStateException JavaDoc("illegal hex number");
61       final int end = from + len;
62       long n = 0;
63       for(int i=from+2; i<end; i++){
64         n = n*16 + toHexDigit(cs.charAt(i));
65       }
66       return new Long JavaDoc(n);
67     }
68   };
69   /**
70    * creates a Tokenizer instance that can parse a decimal integer string.
71    * @return the Tokenizer instance.
72    */

73   public static Tokenizer getDecTokenizer(){return dTokenizer;}
74   /**
75    * creates a Tokenizer instance that can parse a octal integer string.
76    * @return the Tokenizer instance.
77    */

78   public static Tokenizer getOctTokenizer(){return oTokenizer;}
79   /**
80    * creates a Tokenizer instance that can parse a hex integer string.
81    * @return the Tokenizer instance.
82    */

83   public static Tokenizer getHexTokenizer(){return hTokenizer;}
84 }
85
Popular Tags