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
11 package com.ibm.icu.util;
13 import java.nio.CharBuffer;
16 * Builder class for CharsTrie.
18 * <p>This class is not intended for public subclassing.
21 * @author Markus W. Scherer
23 public final class CharsTrieBuilder extends StringTrieBuilder {
25 * Constructs an empty builder.
28 public CharsTrieBuilder() {}
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.
40 public CharsTrieBuilder add(CharSequence s, int value) {
46 * Builds a CharsTrie for the add()ed data.
47 * Once built, no further data can be add()ed until clear() is called.
49 * <p>A CharsTrie cannot be empty. At least one (string, value) pair
50 * must have been add()ed.
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.
59 public CharsTrie build(StringTrieBuilder.Option buildOption) {
60 return new CharsTrie(buildCharSequence(buildOption), 0);
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.
67 * <p>A CharsTrie cannot be empty. At least one (string, value) pair
68 * must have been add()ed.
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.
77 public CharSequence buildCharSequence(StringTrieBuilder.Option buildOption) {
78 buildChars(buildOption);
79 return CharBuffer.wrap(chars, chars.length-charsLength, charsLength);
82 private void buildChars(StringTrieBuilder.Option buildOption) {
83 // Create and char-serialize the trie for the elements.
87 buildImpl(buildOption);
91 * Removes all (string, value) pairs.
92 * New data can then be add()ed and a new trie can be built.
96 public CharsTrieBuilder clear() {
106 * @deprecated This API is ICU internal only.
109 protected boolean matchNodesCanHaveValues() /*const*/ { return true; }
114 * @deprecated This API is ICU internal only.
117 protected int getMaxBranchLinearSubNodeLength() /*const*/ { return CharsTrie.kMaxBranchLinearSubNodeLength; }
121 * @deprecated This API is ICU internal only.
124 protected int getMinLinearMatch() /*const*/ { return CharsTrie.kMinLinearMatch; }
128 * @deprecated This API is ICU internal only.
131 protected int getMaxLinearMatchLength() /*const*/ { return CharsTrie.kMaxLinearMatchLength; }
133 private void ensureCapacity(int length) {
134 if(length>chars.length) {
135 int newCapacity=chars.length;
138 } while(newCapacity<=length);
139 char[] newChars=new char[newCapacity];
140 System.arraycopy(chars, chars.length-charsLength,
141 newChars, newChars.length-charsLength, charsLength);
148 * @deprecated This API is ICU internal only.
151 protected int write(int unit) {
152 int newLength=charsLength+1;
153 ensureCapacity(newLength);
154 charsLength=newLength;
155 chars[chars.length-charsLength]=(char)unit;
161 * @deprecated This API is ICU internal only.
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;
170 chars[charsOffset++]=strings.charAt(offset++);
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);
183 // For writeValueAndFinal(), writeValueAndType() and writeDeltaTo().
184 private final char[] intUnits=new char[3];
189 * @deprecated This API is ICU internal only.
192 protected int writeValueAndFinal(int i, boolean isFinal) {
193 if(0<=i && i<=CharsTrie.kMaxOneUnitValue) {
194 return write(i|(isFinal ? CharsTrie.kValueIsFinal : 0));
197 if(i<0 || i>CharsTrie.kMaxTwoUnitValue) {
198 intUnits[0]=(char)(CharsTrie.kThreeUnitValueLead);
199 intUnits[1]=(char)(i>>16);
202 // } else if(i<=CharsTrie.kMaxOneUnitValue) {
203 // intUnits[0]=(char)(i);
206 intUnits[0]=(char)(CharsTrie.kMinTwoUnitValueLead+(i>>16));
210 intUnits[0]=(char)(intUnits[0]|(isFinal ? CharsTrie.kValueIsFinal : 0));
211 return write(intUnits, length);
216 * @deprecated This API is ICU internal only.
219 protected int writeValueAndType(boolean hasValue, int value, int node) {
224 if(value<0 || value>CharsTrie.kMaxTwoUnitNodeValue) {
225 intUnits[0]=(char)(CharsTrie.kThreeUnitNodeValueLead);
226 intUnits[1]=(char)(value>>16);
227 intUnits[2]=(char)value;
229 } else if(value<=CharsTrie.kMaxOneUnitNodeValue) {
230 intUnits[0]=(char)((value+1)<<6);
233 intUnits[0]=(char)(CharsTrie.kMinTwoUnitNodeValueLead+((value>>10)&0x7fc0));
234 intUnits[1]=(char)value;
237 intUnits[0]|=(char)node;
238 return write(intUnits, length);
243 * @deprecated This API is ICU internal only.
246 protected int writeDeltaTo(int jumpTarget) {
247 int i=charsLength-jumpTarget;
249 if(i<=CharsTrie.kMaxOneUnitDelta) {
253 if(i<=CharsTrie.kMaxTwoUnitDelta) {
254 intUnits[0]=(char)(CharsTrie.kMinTwoUnitDeltaLead+(i>>16));
257 intUnits[0]=(char)(CharsTrie.kThreeUnitDeltaLead);
258 intUnits[1]=(char)(i>>16);
261 intUnits[length++]=(char)i;
262 return write(intUnits, length);
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;