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#License 4 package android.icu.impl.number; 5 6 import android.icu.text.NumberFormat.Field; 7 8 /** 9 * The canonical implementation of {@link Modifier}, containing a prefix and suffix string. 10 * @hide Only a subset of ICU is exposed in Android 11 */ 12 public class ConstantAffixModifier implements Modifier { 13 14 // TODO: Avoid making a new instance by default if prefix and suffix are empty 15 public static final ConstantAffixModifier EMPTY = new ConstantAffixModifier(); 16 17 private final String prefix; 18 private final String suffix; 19 private final Field field; 20 private final boolean strong; 21 22 /** 23 * Constructs an instance with the given strings. 24 * 25 * <p> 26 * The arguments need to be Strings, not CharSequences, because Strings are immutable but CharSequences are not. 27 * 28 * @param prefix 29 * The prefix string. 30 * @param suffix 31 * The suffix string. 32 * @param field 33 * The field type to be associated with this modifier. Can be null. 34 * @param strong 35 * Whether this modifier should be strongly applied. 36 * @see Field 37 */ 38 public ConstantAffixModifier(String prefix, String suffix, Field field, boolean strong) { 39 // Use an empty string instead of null if we are given null 40 // TODO: Consider returning a null modifier if both prefix and suffix are empty. 41 this.prefix = (prefix == null ? "" : prefix); 42 this.suffix = (suffix == null ? "" : suffix); 43 this.field = field; 44 this.strong = strong; 45 } 46 47 /** Constructs a new instance with an empty prefix, suffix, and field. */ 48 public ConstantAffixModifier() { 49 prefix = ""; 50 suffix = ""; 51 field = null; 52 strong = false; 53 } 54 55 @Override 56 public int apply(NumberStringBuilder output, int leftIndex, int rightIndex) { 57 // Insert the suffix first since inserting the prefix will change the rightIndex 58 int length = output.insert(rightIndex, suffix, field); 59 length += output.insert(leftIndex, prefix, field); 60 return length; 61 } 62 63 @Override 64 public int getPrefixLength() { 65 return prefix.length(); 66 } 67 68 @Override 69 public int getCodePointCount() { 70 return prefix.codePointCount(0, prefix.length()) + suffix.codePointCount(0, suffix.length()); 71 } 72 73 @Override 74 public boolean isStrong() { 75 return strong; 76 } 77 78 @Override 79 public String toString() { 80 return String.format("<ConstantAffixModifier prefix:'%s' suffix:'%s'>", prefix, suffix); 81 } 82 } 83