KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > impl > USerializedSet


1 /*
2  *******************************************************************************
3  * Copyright (C) 2002-2004, International Business Machines
4  * Corporation and others. All Rights Reserved.
5  *******************************************************************************
6 */

7
8 package com.ibm.icu.impl;
9 /**
10  * @version 1.1
11  * @author Markus W. Scherer
12  * Ram: Add documentation, remove unwanted methods, improve coverage.
13  */

14
15 /**
16  * Simple class for handling serialized USet/UnicodeSet structures
17  * without object creation. See ICU4C icu/source/common/uset.c.
18  *
19  * @internal
20  */

21 public final class USerializedSet {
22     /**
23      * Fill in the given serialized set object.
24      * @param src pointer to start of array
25      * @param srcStart pointer to start of serialized data (length value)
26      * @return true if the given array is valid, otherwise false
27      * @draft ICU 2.4
28      */

29     public final boolean getSet(char src[], int srcStart) {
30         // leave most argument checking up to Java exceptions
31
array=null;
32         arrayOffset=bmpLength=length=0;
33
34         length=src[srcStart++];
35         
36         
37         if((length&0x8000) >0) {
38             /* there are supplementary values */
39             length&=0x7fff;
40             if(src.length<(srcStart+1+length)) {
41                 length=0;
42                 throw new IndexOutOfBoundsException JavaDoc();
43             }
44             bmpLength=src[srcStart++];
45         } else {
46             /* only BMP values */
47             if(src.length<(srcStart+length)) {
48                 length=0;
49                 throw new IndexOutOfBoundsException JavaDoc();
50             }
51             bmpLength=length;
52         }
53         array = new char[length];
54         System.arraycopy(src,srcStart,array,0,length);
55         //arrayOffset=srcStart;
56
return true;
57     }
58     
59     /**
60      * Set the USerializedSet to contain the given character (and nothing
61      * else).
62      * @draft ICU 2.4
63      */

64     public final void setToOne(int c) {
65         if( 0x10ffff<c) {
66             return;
67         }
68
69         if(c<0xffff) {
70             bmpLength=length=2;
71             array[0]=(char)c;
72             array[1]=(char)(c+1);
73         } else if(c==0xffff) {
74             bmpLength=1;
75             length=3;
76             array[0]=0xffff;
77             array[1]=1;
78             array[2]=0;
79         } else if(c<0x10ffff) {
80             bmpLength=0;
81             length=4;
82             array[0]=(char)(c>>16);
83             array[1]=(char)c;
84             ++c;
85             array[2]=(char)(c>>16);
86             array[3]=(char)c;
87         } else /* c==0x10ffff */ {
88             bmpLength=0;
89             length=2;
90             array[0]=0x10;
91             array[1]=0xffff;
92         }
93     }
94         
95     /**
96      * Returns a range of characters contained in the given serialized
97      * set.
98      * @param rangeIndex a non-negative integer in the range <code>0..
99      * getSerializedRangeCount()-1</code>
100      * @param range variable to receive the data in the range
101      * @return true if rangeIndex is valid, otherwise false
102      * @draft ICU 2.4
103      */

104     public final boolean getRange(int rangeIndex, int[] range) {
105         if( rangeIndex<0) {
106             return false;
107         }
108         if(array==null){
109             array = new char[8];
110         }
111         if(range==null || range.length <2){
112             throw new IllegalArgumentException JavaDoc();
113         }
114         rangeIndex*=2; /* address start/limit pairs */
115         if(rangeIndex<bmpLength) {
116             range[0]=array[rangeIndex++];
117             if(rangeIndex<bmpLength) {
118                 range[1]=array[rangeIndex];
119             } else if(rangeIndex<length) {
120                 range[1]=(((int)array[rangeIndex])<<16)|array[rangeIndex+1];
121             } else {
122                 range[1]=0x110000;
123             }
124             range[1]-=1;
125             return true;
126         } else {
127             rangeIndex-=bmpLength;
128             rangeIndex*=2; /* address pairs of pairs of units */
129             length-=bmpLength;
130             if(rangeIndex<length) {
131                 int offset=arrayOffset+bmpLength;
132                 range[0]=(((int)array[offset+rangeIndex])<<16)|array[offset+rangeIndex+1];
133                 rangeIndex+=2;
134                 if(rangeIndex<length) {
135                     range[1]=(((int)array[offset+rangeIndex])<<16)|array[offset+rangeIndex+1];
136                 } else {
137                     range[1]=0x110000;
138                 }
139                 range[1]-=1;
140                 return true;
141             } else {
142                 return false;
143             }
144         }
145     }
146     
147     /**
148      * Returns true if the given USerializedSet contains the given
149      * character.
150      * @param c the character to test for
151      * @return true if set contains c
152      * @draft ICU 2.4
153      */

154     ///CLOVER:OFF
155
public final boolean contains(int c) {
156         
157         if(c>0x10ffff) {
158             return false;
159         }
160             
161         if(c<=0xffff) {
162             int i;
163             /* find c in the BMP part */
164             for(i=0; i<bmpLength && (char)c>=array[i]; ++i) {}
165             return (boolean)((i&1) != 0);
166         } else {
167             int i;
168             /* find c in the supplementary part */
169             char high=(char)(c>>16), low=(char)c;
170             for(i=bmpLength;
171                 i<length && (high>array[i] || (high==array[i] && low>=array[i+1]));
172                 i+=2) {}
173         
174             /* count pairs of 16-bit units even per BMP and check if the number of pairs is odd */
175             return (boolean)(((i+bmpLength)&2)!=0);
176         }
177     }
178     ///CLOVER:ON
179
/**
180      * Returns the number of disjoint ranges of characters contained in
181      * the given serialized set. Ignores any strings contained in the
182      * set.
183      * @return a non-negative integer counting the character ranges
184      * contained in set
185      * @draft ICU 2.4
186      */

187     public final int countRanges() {
188         return (bmpLength+(length-bmpLength)/2+1)/2;
189     }
190     
191     private char array[] = new char[8];
192     private int arrayOffset, bmpLength, length;
193 }
194
Popular Tags