KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JFlex > CharSetEnumerator


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * JFlex 1.4.1 *
3  * Copyright (C) 1998-2004 Gerwin Klein <lsf@jflex.de> *
4  * All rights reserved. *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License. See the file *
8  * COPYRIGHT for more information. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License along *
16  * with this program; if not, write to the Free Software Foundation, Inc., *
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
18  * *
19  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

20
21 package JFlex;
22
23
24 /**
25  * Enumerator for the elements of a CharSet.
26  *
27  * Does not implement java.util.Enumeration, but supports the same protocol.
28  *
29  * @author Gerwin Klein
30  * @version JFlex 1.4.1, $Revision: 2.3 $, $Date: 2004/11/06 23:03:30 $
31  */

32 final public class CharSetEnumerator {
33
34   private int index;
35   private int offset;
36   private long mask = 1;
37
38   private CharSet set;
39   
40   public CharSetEnumerator(CharSet characters) {
41     set = characters;
42
43     while (index < set.bits.length && set.bits[index] == 0)
44       index++;
45
46     if (index >= set.bits.length) return;
47         
48     while (offset <= CharSet.MOD && ((set.bits[index] & mask) == 0)) {
49       mask<<= 1;
50       offset++;
51     }
52   }
53
54   private void advance() {
55     do {
56       offset++;
57       mask<<= 1;
58     } while (offset <= CharSet.MOD && ((set.bits[index] & mask) == 0));
59
60     if (offset > CharSet.MOD) {
61       do
62         index++;
63       while (index < set.bits.length && set.bits[index] == 0);
64         
65       if (index >= set.bits.length) return;
66         
67       offset = 0;
68       mask = 1;
69       
70       while (offset <= CharSet.MOD && ((set.bits[index] & mask) == 0)) {
71         mask<<= 1;
72         offset++;
73       }
74     }
75   }
76
77   public boolean hasMoreElements() {
78     return index < set.bits.length;
79   }
80
81   public int nextElement() {
82     int x = (index << CharSet.BITS) + offset;
83     advance();
84     return x;
85   }
86
87 }
88
89
Popular Tags