1 // 2017 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 package com.ibm.icu.impl.number; 4 5 import com.ibm.icu.impl.StandardPlural; 6 7 /** 8 * A ParameterizedModifier by itself is NOT a Modifier. Rather, it wraps a data structure containing two or more 9 * Modifiers and returns the modifier appropriate for the current situation. 10 */ 11 public class ParameterizedModifier { 12 private final Modifier positive; 13 private final Modifier negative; 14 final Modifier[] mods; 15 boolean frozen; 16 17 /** 18 * This constructor populates the ParameterizedModifier with a single positive and negative form. 19 * 20 * <p> 21 * If this constructor is used, a plural form CANNOT be passed to {@link #getModifier}. 22 */ 23 public ParameterizedModifier(Modifier positive, Modifier negative) { 24 this.positive = positive; 25 this.negative = negative; 26 this.mods = null; 27 this.frozen = true; 28 } 29 30 /** 31 * This constructor prepares the ParameterizedModifier to be populated with a positive and negative Modifier for 32 * multiple plural forms. 33 * 34 * <p> 35 * If this constructor is used, a plural form MUST be passed to {@link #getModifier}. 36 */ 37 public ParameterizedModifier() { 38 this.positive = null; 39 this.negative = null; 40 this.mods = new Modifier[2 * StandardPlural.COUNT]; 41 this.frozen = false; 42 } 43 44 public void setModifier(boolean isNegative, StandardPlural plural, Modifier mod) { 45 assert !frozen; 46 mods[getModIndex(isNegative, plural)] = mod; 47 } 48 49 public void freeze() { 50 frozen = true; 51 } 52 53 public Modifier getModifier(boolean isNegative) { 54 assert frozen; 55 assert mods == null; 56 return isNegative ? negative : positive; 57 } 58 59 public Modifier getModifier(boolean isNegative, StandardPlural plural) { 60 assert frozen; 61 assert positive == null; 62 return mods[getModIndex(isNegative, plural)]; 63 } 64 65 private static int getModIndex(boolean isNegative, StandardPlural plural) { 66 return plural.ordinal() * 2 + (isNegative ? 1 : 0); 67 } 68 } 69