001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.lexer;
043:
044: import java.util.Collection;
045: import java.util.Iterator;
046: import java.util.AbstractSet;
047: import java.util.NoSuchElementException;
048: import java.util.Set;
049: import org.netbeans.api.lexer.Language;
050: import org.netbeans.api.lexer.TokenId;
051:
052: /**
053: * A set of token ids.
054: * <br/>
055: * It is immutable in terms of a collection mutability although physically
056: * the set can be mutated.
057: *
058: * @author Miloslav Metelka
059: * @version 1.00
060: */
061:
062: public final class TokenIdSet<T extends TokenId> extends AbstractSet<T> {
063:
064: /**
065: * Find the maximum ordinal among the given token ids.
066: */
067: public static int findMaxOrdinal(Collection<? extends TokenId> ids) {
068: int maxOrdinal = -1;
069: for (TokenId id : ids) {
070: maxOrdinal = Math.max(maxOrdinal, id.ordinal());
071: }
072: return maxOrdinal;
073: }
074:
075: public static <T extends TokenId> void checkIdsFromLanguage(
076: Collection<T> ids, Set<T> languageIds) {
077: for (T id : ids) {
078: if (id != null && !languageIds.contains(id)) {
079: throw new IllegalArgumentException(id
080: + "not contained in " + languageIds); // NOI18N
081: }
082: }
083: }
084:
085: final T[] indexedIds;
086:
087: private int size = -1;
088:
089: /**
090: * Create new token id set.
091: *
092: * @param ids collection of token ids to be contained in this set. There may be nulls in the array
093: * and they will be skipped. All the ids must belong to the languageIds.
094: * @param languageIds language ids used to verify that the passed ids
095: * really belong to the given language. It's also used to get the maximum
096: * ordinal of the language.
097: */
098: public TokenIdSet(Collection<T> ids, int maxOrdinal,
099: boolean checkDupOrdinals) {
100: indexedIds = allocateIds(maxOrdinal + 1);
101: if (ids != null) {
102: for (T id : ids) {
103: if (id != null) {
104: if (checkDupOrdinals
105: && indexedIds[id.ordinal()] != null) {
106: throw new IllegalStateException(id // NOI18N
107: + " has duplicate ordinal with "
108: + indexedIds[id.ordinal()]); // NOI18N
109: }
110: indexedIds[id.ordinal()] = id;
111: }
112: }
113: }
114: }
115:
116: @SuppressWarnings("unchecked")
117: private T[] allocateIds(int size) {
118: return (T[]) new TokenId[size];
119: }
120:
121: public boolean add(T id) {
122: T origId = indexedIds[id.ordinal()];
123: indexedIds[id.ordinal()] = id;
124: size = -1;
125: return (origId != null);
126: }
127:
128: public boolean remove(T id) {
129: T origId = indexedIds[id.ordinal()];
130: indexedIds[id.ordinal()] = null;
131: size = -1;
132: return (origId != null);
133: }
134:
135: public T[] indexedIds() {
136: return indexedIds;
137: }
138:
139: public int size() {
140: int cnt = size;
141: if (cnt < 0) {
142: // Compute size by iteration as both the constructor's and indexedIds arrays
143: // may contain nulls.
144: cnt = 0;
145: for (Iterator it = iterator(); it.hasNext();) {
146: it.next();
147: cnt++;
148: }
149: size = cnt;
150: }
151:
152: return cnt;
153: }
154:
155: public Iterator<T> iterator() {
156: return new SkipNullsIterator();
157: }
158:
159: public boolean containsTokenId(TokenId id) {
160: int ordinal = id.ordinal();
161: return (ordinal >= 0 && ordinal < indexedIds.length && indexedIds[ordinal] == id);
162: }
163:
164: public boolean contains(Object o) {
165: return (o instanceof TokenId) ? containsTokenId((TokenId) o)
166: : false;
167: }
168:
169: public String toString() {
170: StringBuilder sb = new StringBuilder("{\n");
171: for (Iterator it = iterator(); it.hasNext();) {
172: TokenId id = (TokenId) it.next();
173: sb.append(" ");
174: sb.append(LexerUtilsConstants.idToString(id));
175: sb.append('\n');
176: }
177: sb.append("}\n");
178: return sb.toString();
179: }
180:
181: /** Iterator over an array that skips the null values. */
182: private final class SkipNullsIterator implements Iterator<T> {
183:
184: private int index;
185:
186: private int lastRetIndex = -1;
187:
188: SkipNullsIterator() {
189: }
190:
191: public boolean hasNext() {
192: while (index < indexedIds.length) {
193: if (indexedIds[index] != null) {
194: return true;
195: }
196: index++;
197: }
198: return false;
199: }
200:
201: public T next() {
202: while (index < indexedIds.length) {
203: T tokenId = indexedIds[index++];
204: if (tokenId != null) {
205: lastRetIndex = index - 1;
206: return tokenId;
207: }
208: }
209:
210: throw new NoSuchElementException();
211: }
212:
213: public void remove() {
214: if (lastRetIndex >= 0) {
215: indexedIds[lastRetIndex] = null;
216: size = -1;
217: } else {
218: throw new IllegalStateException(); // nothing returned yet
219: }
220: }
221:
222: }
223:
224: }
|