KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > TokenIdSet


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.lexer;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.AbstractSet JavaDoc;
25 import java.util.NoSuchElementException JavaDoc;
26 import java.util.Set JavaDoc;
27 import org.netbeans.api.lexer.Language;
28 import org.netbeans.api.lexer.TokenId;
29
30 /**
31  * A set of token ids.
32  * <br/>
33  * It is immutable in terms of a collection mutability although physically
34  * the set can be mutated.
35  *
36  * @author Miloslav Metelka
37  * @version 1.00
38  */

39
40 public final class TokenIdSet<T extends TokenId> extends AbstractSet JavaDoc<T> {
41
42     /**
43      * Find the maximum ordinal among the given token ids.
44      */

45     public static int findMaxOrdinal(Collection JavaDoc<? extends TokenId> ids) {
46         int maxOrdinal = -1;
47         for (TokenId id : ids) {
48             maxOrdinal = Math.max(maxOrdinal, id.ordinal());
49         }
50         return maxOrdinal;
51     }
52
53     public static <T extends TokenId> void checkIdsFromLanguage(Collection JavaDoc<T> ids, Set JavaDoc<T> languageIds) {
54         for (T id : ids) {
55             if (id != null && !languageIds.contains(id)) {
56                 throw new IllegalArgumentException JavaDoc(id + "not contained in " + languageIds); // NOI18N
57
}
58         }
59     }
60     
61
62     final T[] indexedIds;
63
64     private int size = -1;
65
66     /**
67      * Create new token id set.
68      *
69      * @param ids collection of token ids to be contained in this set. There may be nulls in the array
70      * and they will be skipped. All the ids must belong to the languageIds.
71      * @param languageIds language ids used to verify that the passed ids
72      * really belong to the given language. It's also used to get the maximum
73      * ordinal of the language.
74      */

75     public TokenIdSet(Collection JavaDoc<T> ids, int maxOrdinal, boolean checkDupOrdinals) {
76         indexedIds = allocateIds(maxOrdinal + 1);
77         if (ids != null) {
78             for (T id : ids) {
79                 if (id != null) {
80                     if (checkDupOrdinals && indexedIds[id.ordinal()] != null) {
81                         throw new IllegalStateException JavaDoc(id // NOI18N
82
+ " has duplicate ordinal with " + indexedIds[id.ordinal()]); // NOI18N
83
}
84                     indexedIds[id.ordinal()] = id;
85                 }
86             }
87         }
88     }
89
90     @SuppressWarnings JavaDoc("unchecked") private T[] allocateIds(int size) {
91         return (T[])new TokenId[size];
92     }
93
94     public boolean add(T id) {
95         T origId = indexedIds[id.ordinal()];
96         indexedIds[id.ordinal()] = id;
97         size = -1;
98         return (origId != null);
99     }
100
101     public boolean remove(T id) {
102         T origId = indexedIds[id.ordinal()];
103         indexedIds[id.ordinal()] = null;
104         size = -1;
105         return (origId != null);
106     }
107
108     public T[] indexedIds() {
109         return indexedIds;
110     }
111
112     public int size() {
113         int cnt = size;
114         if (cnt < 0) {
115             // Compute size by iteration as both the constructor's and indexedIds arrays
116
// may contain nulls.
117
cnt = 0;
118             for (Iterator JavaDoc it = iterator(); it.hasNext();) {
119                 it.next();
120                 cnt++;
121             }
122             size = cnt;
123         }
124
125         return cnt;
126     }
127
128     public Iterator JavaDoc<T> iterator() {
129         return new SkipNullsIterator();
130     }
131
132     public boolean containsTokenId(TokenId id) {
133         int ordinal = id.ordinal();
134         return (ordinal >= 0 && ordinal < indexedIds.length && indexedIds[ordinal] == id);
135     }
136
137     public boolean contains(Object JavaDoc o) {
138         return (o instanceof TokenId)
139             ? containsTokenId((TokenId)o)
140             : false;
141     }
142     
143     public String JavaDoc toString() {
144         StringBuilder JavaDoc sb = new StringBuilder JavaDoc("{\n");
145         for (Iterator JavaDoc it = iterator(); it.hasNext();) {
146             TokenId id = (TokenId) it.next();
147             sb.append(" ");
148             sb.append(LexerUtilsConstants.idToString(id));
149             sb.append('\n');
150         }
151         sb.append("}\n");
152         return sb.toString();
153     }
154
155     /** Iterator over an array that skips the null values. */
156     private final class SkipNullsIterator implements Iterator JavaDoc<T> {
157         
158         private int index;
159
160         private int lastRetIndex = -1;
161         
162         SkipNullsIterator() {
163         }
164         
165         public boolean hasNext() {
166             while (index < indexedIds.length) {
167                 if (indexedIds[index] != null) {
168                     return true;
169                 }
170                 index++;
171             }
172             return false;
173         }
174         
175         public T next() {
176             while (index < indexedIds.length) {
177                 T tokenId = indexedIds[index++];
178                 if (tokenId != null) {
179                     lastRetIndex = index - 1;
180                     return tokenId;
181                 }
182             }
183             
184             throw new NoSuchElementException JavaDoc();
185         }
186         
187         public void remove() {
188             if (lastRetIndex >= 0) {
189                 indexedIds[lastRetIndex] = null;
190                 size = -1;
191             } else {
192                 throw new IllegalStateException JavaDoc(); // nothing returned yet
193
}
194         }
195         
196     }
197
198 }
199
200
Popular Tags