1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // © 2017 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html 4 package android.icu.text; 5 6 import java.util.Locale; 7 8 import android.icu.impl.CaseMapImpl; 9 import android.icu.impl.UCaseProps; 10 import android.icu.lang.UCharacter; 11 import android.icu.util.ULocale; 12 13 /** 14 * Low-level case mapping options and methods. Immutable. 15 * "Setters" return instances with the union of the current and new options set. 16 * 17 * This class is not intended for public subclassing. 18 */ 19 public abstract class CaseMap { 20 /** 21 * @deprecated This API is ICU internal only. 22 * @hide original deprecated declaration 23 * @hide draft / provisional / internal are hidden on Android 24 */ 25 @Deprecated 26 protected int internalOptions; 27 CaseMap(int opt)28 private CaseMap(int opt) { internalOptions = opt; } 29 getCaseLocale(Locale locale)30 private static int getCaseLocale(Locale locale) { 31 if (locale == null) { 32 locale = Locale.getDefault(); 33 } 34 return UCaseProps.getCaseLocale(locale); 35 } 36 37 /** 38 * @return Lowercasing object with default options. 39 */ toLower()40 public static Lower toLower() { return Lower.DEFAULT; } 41 /** 42 * @return Uppercasing object with default options. 43 */ toUpper()44 public static Upper toUpper() { return Upper.DEFAULT; } 45 /** 46 * @return Titlecasing object with default options. 47 */ toTitle()48 public static Title toTitle() { return Title.DEFAULT; } 49 /** 50 * @return Case folding object with default options. 51 */ fold()52 public static Fold fold() { return Fold.DEFAULT; } 53 54 /** 55 * Returns an instance that behaves like this one but 56 * omits unchanged text when case-mapping with {@link Edits}. 57 * 58 * @return an options object with this option. 59 */ omitUnchangedText()60 public abstract CaseMap omitUnchangedText(); 61 62 /** 63 * Lowercasing options and methods. Immutable. 64 * 65 * @see #toLower() 66 */ 67 public static final class Lower extends CaseMap { 68 private static final Lower DEFAULT = new Lower(0); 69 private static final Lower OMIT_UNCHANGED = new Lower(CaseMapImpl.OMIT_UNCHANGED_TEXT); Lower(int opt)70 private Lower(int opt) { super(opt); } 71 72 /** 73 * {@inheritDoc} 74 */ 75 @Override omitUnchangedText()76 public Lower omitUnchangedText() { 77 return OMIT_UNCHANGED; 78 } 79 80 /** 81 * Lowercases a string. 82 * Casing is locale-dependent and context-sensitive. 83 * The result may be longer or shorter than the original. 84 * 85 * @param locale The locale ID. Can be null for {@link Locale#getDefault}. 86 * (See {@link ULocale#toLocale}.) 87 * @param src The original string. 88 * @return the result string. 89 * 90 * @see UCharacter#toLowerCase(Locale, String) 91 */ apply(Locale locale, CharSequence src)92 public String apply(Locale locale, CharSequence src) { 93 return CaseMapImpl.toLower(getCaseLocale(locale), internalOptions, src); 94 } 95 96 /** 97 * Lowercases a string and optionally records edits (see {@link #omitUnchangedText}). 98 * Casing is locale-dependent and context-sensitive. 99 * The result may be longer or shorter than the original. 100 * 101 * @param locale The locale ID. Can be null for {@link Locale#getDefault}. 102 * (See {@link ULocale#toLocale}.) 103 * @param src The original string. 104 * @param dest A buffer for the result string. Must not be null. 105 * @param edits Records edits for index mapping, working with styled text, 106 * and getting only changes (if any). 107 * This function calls edits.reset() first. edits can be null. 108 * @return dest with the result string (or only changes) appended. 109 * 110 * @see UCharacter#toLowerCase(Locale, String) 111 */ apply( Locale locale, CharSequence src, A dest, Edits edits)112 public <A extends Appendable> A apply( 113 Locale locale, CharSequence src, A dest, Edits edits) { 114 return CaseMapImpl.toLower(getCaseLocale(locale), internalOptions, src, dest, edits); 115 } 116 } 117 118 /** 119 * Uppercasing options and methods. Immutable. 120 * 121 * @see #toUpper() 122 */ 123 public static final class Upper extends CaseMap { 124 private static final Upper DEFAULT = new Upper(0); 125 private static final Upper OMIT_UNCHANGED = new Upper(CaseMapImpl.OMIT_UNCHANGED_TEXT); Upper(int opt)126 private Upper(int opt) { super(opt); } 127 128 /** 129 * {@inheritDoc} 130 */ 131 @Override omitUnchangedText()132 public Upper omitUnchangedText() { 133 return OMIT_UNCHANGED; 134 } 135 136 /** 137 * Uppercases a string. 138 * Casing is locale-dependent and context-sensitive. 139 * The result may be longer or shorter than the original. 140 * 141 * @param locale The locale ID. Can be null for {@link Locale#getDefault}. 142 * (See {@link ULocale#toLocale}.) 143 * @param src The original string. 144 * @return the result string. 145 * 146 * @see UCharacter#toUpperCase(Locale, String) 147 */ apply(Locale locale, CharSequence src)148 public String apply(Locale locale, CharSequence src) { 149 return CaseMapImpl.toUpper(getCaseLocale(locale), internalOptions, src); 150 } 151 152 /** 153 * Uppercases a string and optionally records edits (see {@link #omitUnchangedText}). 154 * Casing is locale-dependent and context-sensitive. 155 * The result may be longer or shorter than the original. 156 * 157 * @param locale The locale ID. Can be null for {@link Locale#getDefault}. 158 * (See {@link ULocale#toLocale}.) 159 * @param src The original string. 160 * @param dest A buffer for the result string. Must not be null. 161 * @param edits Records edits for index mapping, working with styled text, 162 * and getting only changes (if any). 163 * This function calls edits.reset() first. edits can be null. 164 * @return dest with the result string (or only changes) appended. 165 * 166 * @see UCharacter#toUpperCase(Locale, String) 167 */ apply( Locale locale, CharSequence src, A dest, Edits edits)168 public <A extends Appendable> A apply( 169 Locale locale, CharSequence src, A dest, Edits edits) { 170 return CaseMapImpl.toUpper(getCaseLocale(locale), internalOptions, src, dest, edits); 171 } 172 } 173 174 /** 175 * Titlecasing options and methods. Immutable. 176 * 177 * @see #toTitle() 178 */ 179 public static final class Title extends CaseMap { 180 private static final Title DEFAULT = new Title(0); 181 private static final Title OMIT_UNCHANGED = new Title(CaseMapImpl.OMIT_UNCHANGED_TEXT); Title(int opt)182 private Title(int opt) { super(opt); } 183 184 /** 185 * Returns an instance that behaves like this one but 186 * titlecases the string as a whole rather than each word. 187 * (Titlecases only the character at index 0, possibly adjusted.) 188 * 189 * <p>It is an error to specify multiple titlecasing iterator options together, 190 * including both an option and an explicit BreakIterator. 191 * 192 * @return an options object with this option. 193 * @see #adjustToCased() 194 */ wholeString()195 public Title wholeString() { 196 return new Title(CaseMapImpl.addTitleIteratorOption( 197 internalOptions, CaseMapImpl.TITLECASE_WHOLE_STRING)); 198 } 199 200 /** 201 * Returns an instance that behaves like this one but 202 * titlecases sentences rather than words. 203 * (Titlecases only the first character of each sentence, possibly adjusted.) 204 * 205 * <p>It is an error to specify multiple titlecasing iterator options together, 206 * including both an option and an explicit BreakIterator. 207 * 208 * @return an options object with this option. 209 * @see #adjustToCased() 210 */ sentences()211 public Title sentences() { 212 return new Title(CaseMapImpl.addTitleIteratorOption( 213 internalOptions, CaseMapImpl.TITLECASE_SENTENCES)); 214 } 215 216 /** 217 * {@inheritDoc} 218 */ 219 @Override omitUnchangedText()220 public Title omitUnchangedText() { 221 if (internalOptions == 0 || internalOptions == CaseMapImpl.OMIT_UNCHANGED_TEXT) { 222 return OMIT_UNCHANGED; 223 } 224 return new Title(internalOptions | CaseMapImpl.OMIT_UNCHANGED_TEXT); 225 } 226 227 /** 228 * Returns an instance that behaves like this one but 229 * does not lowercase non-initial parts of words when titlecasing. 230 * 231 * <p>By default, titlecasing will titlecase the character at each 232 * (possibly adjusted) BreakIterator index and 233 * lowercase all other characters up to the next iterator index. 234 * With this option, the other characters will not be modified. 235 * 236 * @return an options object with this option. 237 * @see UCharacter#TITLECASE_NO_LOWERCASE 238 * @see #adjustToCased() 239 */ noLowercase()240 public Title noLowercase() { 241 return new Title(internalOptions | UCharacter.TITLECASE_NO_LOWERCASE); 242 } 243 244 /** 245 * Returns an instance that behaves like this one but 246 * does not adjust the titlecasing BreakIterator indexes; 247 * titlecases exactly the characters at breaks from the iterator. 248 * 249 * <p>By default, titlecasing will take each break iterator index, 250 * adjust it to the next relevant character (see {@link #adjustToCased()}), 251 * and titlecase that one. 252 * 253 * <p>Other characters are lowercased. 254 * 255 * @return an options object with this option. 256 * @see UCharacter#TITLECASE_NO_BREAK_ADJUSTMENT 257 */ noBreakAdjustment()258 public Title noBreakAdjustment() { 259 return new Title(CaseMapImpl.addTitleAdjustmentOption( 260 internalOptions, UCharacter.TITLECASE_NO_BREAK_ADJUSTMENT)); 261 } 262 263 /** 264 * Returns an instance that behaves like this one but 265 * adjusts each titlecasing BreakIterator index to the next cased character. 266 * (See the Unicode Standard, chapter 3, Default Case Conversion, R3 toTitlecase(X).) 267 * 268 * <p>This used to be the default index adjustment in ICU. 269 * Since ICU 60, the default index adjustment is to the next character that is 270 * a letter, number, symbol, or private use code point. 271 * (Uncased modifier letters are skipped.) 272 * The difference in behavior is small for word titlecasing, 273 * but the new adjustment is much better for whole-string and sentence titlecasing: 274 * It yields "49ers" and "«丰(abc)»" instead of "49Ers" and "«丰(Abc)»". 275 * 276 * <p>It is an error to specify multiple titlecasing adjustment options together. 277 * 278 * @return an options object with this option. 279 * @see #noBreakAdjustment() 280 */ adjustToCased()281 public Title adjustToCased() { 282 return new Title(CaseMapImpl.addTitleAdjustmentOption( 283 internalOptions, CaseMapImpl.TITLECASE_ADJUST_TO_CASED)); 284 } 285 286 /** 287 * Titlecases a string. 288 * Casing is locale-dependent and context-sensitive. 289 * The result may be longer or shorter than the original. 290 * 291 * <p>Titlecasing uses a break iterator to find the first characters of words 292 * that are to be titlecased. It titlecases those characters and lowercases 293 * all others. (This can be modified with options bits.) 294 * 295 * @param locale The locale ID. Can be null for {@link Locale#getDefault}. 296 * (See {@link ULocale#toLocale}.) 297 * @param iter A break iterator to find the first characters of words that are to be titlecased. 298 * It is set to the source string (setText()) 299 * and used one or more times for iteration (first() and next()). 300 * If null, then a word break iterator for the locale is used 301 * (or something equivalent). 302 * @param src The original string. 303 * @return the result string. 304 * 305 * @see UCharacter#toUpperCase(Locale, String) 306 */ apply(Locale locale, BreakIterator iter, CharSequence src)307 public String apply(Locale locale, BreakIterator iter, CharSequence src) { 308 if (iter == null && locale == null) { 309 locale = Locale.getDefault(); 310 } 311 iter = CaseMapImpl.getTitleBreakIterator(locale, internalOptions, iter); 312 iter.setText(src); 313 return CaseMapImpl.toTitle(getCaseLocale(locale), internalOptions, iter, src); 314 } 315 316 /** 317 * Titlecases a string and optionally records edits (see {@link #omitUnchangedText}). 318 * Casing is locale-dependent and context-sensitive. 319 * The result may be longer or shorter than the original. 320 * 321 * <p>Titlecasing uses a break iterator to find the first characters of words 322 * that are to be titlecased. It titlecases those characters and lowercases 323 * all others. (This can be modified with options bits.) 324 * 325 * @param locale The locale ID. Can be null for {@link Locale#getDefault}. 326 * (See {@link ULocale#toLocale}.) 327 * @param iter A break iterator to find the first characters of words that are to be titlecased. 328 * It is set to the source string (setText()) 329 * and used one or more times for iteration (first() and next()). 330 * If null, then a word break iterator for the locale is used 331 * (or something equivalent). 332 * @param src The original string. 333 * @param dest A buffer for the result string. Must not be null. 334 * @param edits Records edits for index mapping, working with styled text, 335 * and getting only changes (if any). 336 * This function calls edits.reset() first. edits can be null. 337 * @return dest with the result string (or only changes) appended. 338 * 339 * @see UCharacter#toTitleCase(Locale, String, BreakIterator, int) 340 */ apply( Locale locale, BreakIterator iter, CharSequence src, A dest, Edits edits)341 public <A extends Appendable> A apply( 342 Locale locale, BreakIterator iter, CharSequence src, A dest, Edits edits) { 343 if (iter == null && locale == null) { 344 locale = Locale.getDefault(); 345 } 346 iter = CaseMapImpl.getTitleBreakIterator(locale, internalOptions, iter); 347 iter.setText(src); 348 return CaseMapImpl.toTitle( 349 getCaseLocale(locale), internalOptions, iter, src, dest, edits); 350 } 351 } 352 353 /** 354 * Case folding options and methods. Immutable. 355 * 356 * @see #fold() 357 */ 358 public static final class Fold extends CaseMap { 359 private static final Fold DEFAULT = new Fold(0); 360 private static final Fold TURKIC = new Fold(UCharacter.FOLD_CASE_EXCLUDE_SPECIAL_I); 361 private static final Fold OMIT_UNCHANGED = new Fold(CaseMapImpl.OMIT_UNCHANGED_TEXT); 362 private static final Fold TURKIC_OMIT_UNCHANGED = new Fold( 363 UCharacter.FOLD_CASE_EXCLUDE_SPECIAL_I | CaseMapImpl.OMIT_UNCHANGED_TEXT); Fold(int opt)364 private Fold(int opt) { super(opt); } 365 366 /** 367 * {@inheritDoc} 368 */ 369 @Override omitUnchangedText()370 public Fold omitUnchangedText() { 371 return (internalOptions & UCharacter.FOLD_CASE_EXCLUDE_SPECIAL_I) == 0 ? 372 OMIT_UNCHANGED : TURKIC_OMIT_UNCHANGED; 373 } 374 375 /** 376 * Returns an instance that behaves like this one but 377 * handles dotted I and dotless i appropriately for Turkic languages (tr, az). 378 * 379 * <p>Uses the Unicode CaseFolding.txt mappings marked with 'T' that 380 * are to be excluded for default mappings and 381 * included for the Turkic-specific mappings. 382 * 383 * @return an options object with this option. 384 * @see UCharacter#FOLD_CASE_EXCLUDE_SPECIAL_I 385 */ turkic()386 public Fold turkic() { 387 return (internalOptions & CaseMapImpl.OMIT_UNCHANGED_TEXT) == 0 ? 388 TURKIC : TURKIC_OMIT_UNCHANGED; 389 } 390 391 /** 392 * Case-folds a string. 393 * The result may be longer or shorter than the original. 394 * 395 * <p>Case-folding is locale-independent and not context-sensitive, 396 * but there is an option for whether to include or exclude mappings for dotted I 397 * and dotless i that are marked with 'T' in CaseFolding.txt. 398 * 399 * @param src The original string. 400 * @return the result string. 401 * 402 * @see UCharacter#foldCase(String, int) 403 */ apply(CharSequence src)404 public String apply(CharSequence src) { 405 return CaseMapImpl.fold(internalOptions, src); 406 } 407 408 /** 409 * Case-folds a string and optionally records edits (see {@link #omitUnchangedText}). 410 * The result may be longer or shorter than the original. 411 * 412 * <p>Case-folding is locale-independent and not context-sensitive, 413 * but there is an option for whether to include or exclude mappings for dotted I 414 * and dotless i that are marked with 'T' in CaseFolding.txt. 415 * 416 * @param src The original string. 417 * @param dest A buffer for the result string. Must not be null. 418 * @param edits Records edits for index mapping, working with styled text, 419 * and getting only changes (if any). 420 * This function calls edits.reset() first. edits can be null. 421 * @return dest with the result string (or only changes) appended. 422 * 423 * @see UCharacter#foldCase(String, int) 424 */ apply(CharSequence src, A dest, Edits edits)425 public <A extends Appendable> A apply(CharSequence src, A dest, Edits edits) { 426 return CaseMapImpl.fold(internalOptions, src, dest, edits); 427 } 428 } 429 } 430