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