]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/classes/core/src/com/ibm/icu/util/CharsTrieBuilder.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / core / src / com / ibm / icu / util / CharsTrieBuilder.java
1 /*
2 *******************************************************************************
3 *   Copyright (C) 2011-2013, International Business Machines
4 *   Corporation and others.  All Rights Reserved.
5 *******************************************************************************
6 *   created on: 2011jan07
7 *   created by: Markus W. Scherer
8 *   ported from ICU4C ucharstriebuilder/.cpp
9 */
10
11 package com.ibm.icu.util;
12
13 import java.nio.CharBuffer;
14
15 /**
16  * Builder class for CharsTrie.
17  *
18  * <p>This class is not intended for public subclassing.
19  *
20  * @stable ICU 4.8
21  * @author Markus W. Scherer
22  */
23 public final class CharsTrieBuilder extends StringTrieBuilder {
24     /**
25      * Constructs an empty builder.
26      * @stable ICU 4.8
27      */
28     public CharsTrieBuilder() {}
29
30     /**
31      * Adds a (string, value) pair.
32      * The string must be unique.
33      * The string contents will be copied; the builder does not keep
34      * a reference to the input CharSequence.
35      * @param s The input string.
36      * @param value The value associated with this char sequence.
37      * @return this
38      * @stable ICU 4.8
39      */
40     public CharsTrieBuilder add(CharSequence s, int value) {
41         addImpl(s, value);
42         return this;
43     }
44
45     /**
46      * Builds a CharsTrie for the add()ed data.
47      * Once built, no further data can be add()ed until clear() is called.
48      *
49      * <p>A CharsTrie cannot be empty. At least one (string, value) pair
50      * must have been add()ed.
51      *
52      * <p>Multiple calls to build() or buildCharSequence() return tries or sequences
53      * which share the builder's char array, without rebuilding.
54      * After clear() has been called, a new array will be used.
55      * @param buildOption Build option, see StringTrieBuilder.Option.
56      * @return A new CharsTrie for the add()ed data.
57      * @stable ICU 4.8
58      */
59     public CharsTrie build(StringTrieBuilder.Option buildOption) {
60         return new CharsTrie(buildCharSequence(buildOption), 0);
61     }
62
63     /**
64      * Builds a CharsTrie for the add()ed data and char-serializes it.
65      * Once built, no further data can be add()ed until clear() is called.
66      *
67      * <p>A CharsTrie cannot be empty. At least one (string, value) pair
68      * must have been add()ed.
69      *
70      * <p>Multiple calls to build() or buildCharSequence() return tries or sequences
71      * which share the builder's char array, without rebuilding.
72      * After clear() has been called, a new array will be used.
73      * @param buildOption Build option, see StringTrieBuilder.Option.
74      * @return A CharSequence with the char-serialized CharsTrie for the add()ed data.
75      * @stable ICU 4.8
76      */
77     public CharSequence buildCharSequence(StringTrieBuilder.Option buildOption) {
78         buildChars(buildOption);
79         return CharBuffer.wrap(chars, chars.length-charsLength, charsLength);
80     }
81
82     private void buildChars(StringTrieBuilder.Option buildOption) {
83         // Create and char-serialize the trie for the elements.
84         if(chars==null) {
85             chars=new char[1024];
86         }
87         buildImpl(buildOption);
88     }
89
90     /**
91      * Removes all (string, value) pairs.
92      * New data can then be add()ed and a new trie can be built.
93      * @return this
94      * @stable ICU 4.8
95      */
96     public CharsTrieBuilder clear() {
97         clearImpl();
98         chars=null;
99         charsLength=0;
100         return this;
101     }
102
103     /**
104      * {@inheritDoc}
105      * @internal
106      * @deprecated This API is ICU internal only.
107      */
108     @Override
109     protected boolean matchNodesCanHaveValues() /*const*/ { return true; }
110
111     /**
112      * {@inheritDoc}
113      * @internal
114      * @deprecated This API is ICU internal only.
115      */
116     @Override
117     protected int getMaxBranchLinearSubNodeLength() /*const*/ { return CharsTrie.kMaxBranchLinearSubNodeLength; }
118     /**
119      * {@inheritDoc}
120      * @internal
121      * @deprecated This API is ICU internal only.
122      */
123     @Override
124     protected int getMinLinearMatch() /*const*/ { return CharsTrie.kMinLinearMatch; }
125     /**
126      * {@inheritDoc}
127      * @internal
128      * @deprecated This API is ICU internal only.
129      */
130     @Override
131     protected int getMaxLinearMatchLength() /*const*/ { return CharsTrie.kMaxLinearMatchLength; }
132
133     private void ensureCapacity(int length) {
134         if(length>chars.length) {
135             int newCapacity=chars.length;
136             do {
137                 newCapacity*=2;
138             } while(newCapacity<=length);
139             char[] newChars=new char[newCapacity];
140             System.arraycopy(chars, chars.length-charsLength,
141                              newChars, newChars.length-charsLength, charsLength);
142             chars=newChars;
143         }
144     }
145     /**
146      * {@inheritDoc}
147      * @internal
148      * @deprecated This API is ICU internal only.
149      */
150     @Override
151     protected int write(int unit) {
152         int newLength=charsLength+1;
153         ensureCapacity(newLength);
154         charsLength=newLength;
155         chars[chars.length-charsLength]=(char)unit;
156         return charsLength;
157     }
158     /**
159      * {@inheritDoc}
160      * @internal
161      * @deprecated This API is ICU internal only.
162      */
163     @Override
164     protected int write(int offset, int length) {
165         int newLength=charsLength+length;
166         ensureCapacity(newLength);
167         charsLength=newLength;
168         int charsOffset=chars.length-charsLength;
169         while(length>0) {
170             chars[charsOffset++]=strings.charAt(offset++);
171             --length;
172         }
173         return charsLength;
174     }
175     private int write(char[] s, int length) {
176         int newLength=charsLength+length;
177         ensureCapacity(newLength);
178         charsLength=newLength;
179         System.arraycopy(s, 0, chars, chars.length-charsLength, length);
180         return charsLength;
181     }
182
183     // For writeValueAndFinal(), writeValueAndType() and writeDeltaTo().
184     private final char[] intUnits=new char[3];
185
186     /**
187      * {@inheritDoc}
188      * @internal
189      * @deprecated This API is ICU internal only.
190      */
191     @Override
192     protected int writeValueAndFinal(int i, boolean isFinal) {
193         if(0<=i && i<=CharsTrie.kMaxOneUnitValue) {
194             return write(i|(isFinal ? CharsTrie.kValueIsFinal : 0));
195         }
196         int length;
197         if(i<0 || i>CharsTrie.kMaxTwoUnitValue) {
198             intUnits[0]=(char)(CharsTrie.kThreeUnitValueLead);
199             intUnits[1]=(char)(i>>16);
200             intUnits[2]=(char)i;
201             length=3;
202         // } else if(i<=CharsTrie.kMaxOneUnitValue) {
203         //     intUnits[0]=(char)(i);
204         //     length=1;
205         } else {
206             intUnits[0]=(char)(CharsTrie.kMinTwoUnitValueLead+(i>>16));
207             intUnits[1]=(char)i;
208             length=2;
209         }
210         intUnits[0]=(char)(intUnits[0]|(isFinal ? CharsTrie.kValueIsFinal : 0));
211         return write(intUnits, length);
212     }
213     /**
214      * {@inheritDoc}
215      * @internal
216      * @deprecated This API is ICU internal only.
217      */
218     @Override
219     protected int writeValueAndType(boolean hasValue, int value, int node) {
220         if(!hasValue) {
221             return write(node);
222         }
223         int length;
224         if(value<0 || value>CharsTrie.kMaxTwoUnitNodeValue) {
225             intUnits[0]=(char)(CharsTrie.kThreeUnitNodeValueLead);
226             intUnits[1]=(char)(value>>16);
227             intUnits[2]=(char)value;
228             length=3;
229         } else if(value<=CharsTrie.kMaxOneUnitNodeValue) {
230             intUnits[0]=(char)((value+1)<<6);
231             length=1;
232         } else {
233             intUnits[0]=(char)(CharsTrie.kMinTwoUnitNodeValueLead+((value>>10)&0x7fc0));
234             intUnits[1]=(char)value;
235             length=2;
236         }
237         intUnits[0]|=(char)node;
238         return write(intUnits, length);
239     }
240     /**
241      * {@inheritDoc}
242      * @internal
243      * @deprecated This API is ICU internal only.
244      */
245     @Override
246     protected int writeDeltaTo(int jumpTarget) {
247         int i=charsLength-jumpTarget;
248         assert(i>=0);
249         if(i<=CharsTrie.kMaxOneUnitDelta) {
250             return write(i);
251         }
252         int length;
253         if(i<=CharsTrie.kMaxTwoUnitDelta) {
254             intUnits[0]=(char)(CharsTrie.kMinTwoUnitDeltaLead+(i>>16));
255             length=1;
256         } else {
257             intUnits[0]=(char)(CharsTrie.kThreeUnitDeltaLead);
258             intUnits[1]=(char)(i>>16);
259             length=2;
260         }
261         intUnits[length++]=(char)i;
262         return write(intUnits, length);
263     }
264
265     // char serialization of the trie.
266     // Grows from the back: charsLength measures from the end of the buffer!
267     private char[] chars;
268     private int charsLength;
269 }