KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > dtd > models > CMStateSet


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.impl.dtd.models;
59
60
61 /**
62  * This class is a very simple bitset class. The DFA content model code needs
63  * to support a bit set, but the java BitSet class is way, way overkill. Our
64  * bitset never needs to be expanded after creation, hash itself, etc...
65  *
66  * Since the vast majority of content models will never require more than 64
67  * bits, and since allocation of anything in Java is expensive, this class
68  * provides a hybrid implementation that uses two ints for instances that use
69  * 64 bits or fewer. It has a byte array reference member which will only be
70  * used if more than 64 bits are required.
71  *
72  * Note that the code that uses this class will never perform operations
73  * on sets of different sizes, so that check does not have to be made here.
74  *
75  * @version $Id: CMStateSet.java,v 1.3 2002/01/29 01:15:10 lehors Exp $
76  */

77 // made this class public so it can be accessed by
78
// the XS content models from the schema package -neilg.
79
public class CMStateSet
80 {
81     // -------------------------------------------------------------------
82
// Constructors
83
// -------------------------------------------------------------------
84
public CMStateSet(int bitCount)
85     {
86         // Store the required bit count and insure its legal
87
fBitCount = bitCount;
88         if (fBitCount < 0)
89             throw new RuntimeException JavaDoc("ImplementationMessages.VAL_CMSI");
90
91         //
92
// See if we need to allocate the byte array or whether we can live
93
// within the 64 bit high performance scheme.
94
//
95
if (fBitCount > 64)
96         {
97             fByteCount = fBitCount / 8;
98             if (fBitCount % 8 != 0)
99                 fByteCount++;
100             fByteArray = new byte[fByteCount];
101         }
102
103         // Init all the bits to zero
104
zeroBits();
105     }
106
107
108     // -------------------------------------------------------------------
109
// Public inherited methods
110
// -------------------------------------------------------------------
111
public String JavaDoc toString()
112     {
113         StringBuffer JavaDoc strRet = new StringBuffer JavaDoc();
114         try
115         {
116             strRet.append("{");
117             for (int index = 0; index < fBitCount; index++)
118             {
119                 if (getBit(index))
120                     strRet.append(" " + index);
121             }
122             strRet.append(" }");
123         }
124
125         catch(RuntimeException JavaDoc exToCatch)
126         {
127             //
128
// We know this won't happen but we have to catch it to avoid it
129
// having to be in our 'throws' list.
130
//
131
}
132         return strRet.toString();
133     }
134
135
136     // -------------------------------------------------------------------
137
// Package final methods
138
// -------------------------------------------------------------------
139
// the XS content models from the schema package -neilg.
140
public final void intersection(CMStateSet setToAnd)
141     {
142         if (fBitCount < 65)
143         {
144             fBits1 &= setToAnd.fBits1;
145             fBits2 &= setToAnd.fBits2;
146         }
147          else
148         {
149             for (int index = fByteCount - 1; index >= 0; index--)
150                 fByteArray[index] &= setToAnd.fByteArray[index];
151         }
152     }
153
154     public final boolean getBit(int bitToGet)
155     {
156         if (bitToGet >= fBitCount)
157             throw new RuntimeException JavaDoc("ImplementationMessages.VAL_CMSI");
158
159         if (fBitCount < 65)
160         {
161             final int mask = (0x1 << (bitToGet % 32));
162             if (bitToGet < 32)
163                 return (fBits1 & mask) != 0;
164             else
165                 return (fBits2 & mask) != 0;
166         }
167          else
168         {
169             // Create the mask and byte values
170
final byte mask = (byte)(0x1 << (bitToGet % 8));
171             final int ofs = bitToGet >> 3;
172
173             // And access the right bit and byte
174
return ((fByteArray[ofs] & mask) != 0);
175         }
176     }
177
178     public final boolean isEmpty()
179     {
180         if (fBitCount < 65)
181         {
182             return ((fBits1 == 0) && (fBits2 == 0));
183         }
184          else
185         {
186             for (int index = fByteCount - 1; index >= 0; index--)
187             {
188                 if (fByteArray[index] != 0)
189                     return false;
190             }
191         }
192         return true;
193     }
194
195     final boolean isSameSet(CMStateSet setToCompare)
196     {
197         if (fBitCount != setToCompare.fBitCount)
198             return false;
199
200         if (fBitCount < 65)
201         {
202             return ((fBits1 == setToCompare.fBits1)
203             && (fBits2 == setToCompare.fBits2));
204         }
205
206         for (int index = fByteCount - 1; index >= 0; index--)
207         {
208             if (fByteArray[index] != setToCompare.fByteArray[index])
209                 return false;
210         }
211         return true;
212     }
213
214 // the XS content models from the schema package -neilg.
215
public final void union(CMStateSet setToOr)
216     {
217         if (fBitCount < 65)
218         {
219             fBits1 |= setToOr.fBits1;
220             fBits2 |= setToOr.fBits2;
221         }
222          else
223         {
224             for (int index = fByteCount - 1; index >= 0; index--)
225                 fByteArray[index] |= setToOr.fByteArray[index];
226         }
227     }
228
229     public final void setBit(int bitToSet)
230     {
231         if (bitToSet >= fBitCount)
232             throw new RuntimeException JavaDoc("ImplementationMessages.VAL_CMSI");
233
234         if (fBitCount < 65)
235         {
236             final int mask = (0x1 << (bitToSet % 32));
237             if (bitToSet < 32)
238             {
239                 fBits1 &= ~mask;
240                 fBits1 |= mask;
241             }
242              else
243             {
244                 fBits2 &= ~mask;
245                 fBits2 |= mask;
246             }
247         }
248          else
249         {
250             // Create the mask and byte values
251
final byte mask = (byte)(0x1 << (bitToSet % 8));
252             final int ofs = bitToSet >> 3;
253
254             // And access the right bit and byte
255
fByteArray[ofs] &= ~mask;
256             fByteArray[ofs] |= mask;
257         }
258     }
259
260 // the XS content models from the schema package -neilg.
261
public final void setTo(CMStateSet srcSet)
262     {
263         // They have to be the same size
264
if (fBitCount != srcSet.fBitCount)
265             throw new RuntimeException JavaDoc("ImplementationMessages.VAL_CMSI");
266         
267         if (fBitCount < 65)
268         {
269             fBits1 = srcSet.fBits1;
270             fBits2 = srcSet.fBits2;
271         }
272          else
273         {
274             for (int index = fByteCount - 1; index >= 0; index--)
275                 fByteArray[index] = srcSet.fByteArray[index];
276         }
277     }
278
279     // had to make this method public so it could be accessed from
280
// schema package - neilg.
281
public final void zeroBits()
282     {
283         if (fBitCount < 65)
284         {
285             fBits1 = 0;
286             fBits2 = 0;
287         }
288          else
289         {
290             for (int index = fByteCount - 1; index >= 0; index--)
291                 fByteArray[index] = 0;
292         }
293     }
294
295
296     // -------------------------------------------------------------------
297
// Private data members
298
//
299
// fBitCount
300
// The count of bits that the outside world wants to support,
301
// so its the max bit index plus one.
302
//
303
// fByteCount
304
// If the bit count is > 64, then we use the fByteArray member to
305
// store the bits, and this indicates its size in bytes. Otherwise
306
// its value is meaningless.
307
//
308
// fBits1
309
// fBits2
310
// When the bit count is < 64 (very common), these hold the bits.
311
// Otherwise, the fByteArray member holds htem.
312
// -------------------------------------------------------------------
313
int fBitCount;
314     int fByteCount;
315     int fBits1;
316     int fBits2;
317     byte[] fByteArray;
318     /* Optimization(Jan, 2001) */
319     public boolean equals(Object JavaDoc o) {
320     if (!(o instanceof CMStateSet)) return false;
321     return isSameSet((CMStateSet)o);
322     }
323
324     public int hashCode() {
325         if (fBitCount < 65)
326         {
327             return fBits1+ fBits2 * 31;
328         }
329          else
330         {
331             int hash = 0;
332             for (int index = fByteCount - 1; index >= 0; index--)
333                 hash = fByteArray[index] + hash * 31;
334             return hash;
335         }
336     }
337    /* Optimization(Jan, 2001) */
338 };
339
Popular Tags