1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 ******************************************************************************* 5 * Copyright (C) 2015, International Business Machines Corporation and 6 * others. All Rights Reserved. 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.impl; 10 11 import java.util.Arrays; 12 import java.util.Collections; 13 import java.util.List; 14 15 /** 16 * Standard CLDR plural form/category constants. 17 * See http://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules 18 */ 19 public enum StandardPlural { 20 ZERO("zero"), 21 ONE("one"), 22 TWO("two"), 23 FEW("few"), 24 MANY("many"), 25 OTHER("other"), 26 EQ_0("=0"), 27 EQ_1("=1"); 28 29 /** 30 * Numeric index of OTHER, same as OTHER.ordinal(). 31 */ 32 public static final int OTHER_INDEX = OTHER.ordinal(); 33 34 /** 35 * Unmodifiable List of all standard plural form constants. 36 * List version of {@link #values()}. 37 */ 38 public static final List<StandardPlural> VALUES = 39 Collections.unmodifiableList(Arrays.asList(values())); 40 41 /** 42 * Number of standard plural forms/categories. 43 */ 44 public static final int COUNT = VALUES.size(); 45 46 private final String keyword; 47 StandardPlural(String kw)48 private StandardPlural(String kw) { 49 keyword = kw; 50 } 51 52 /** 53 * @return the lowercase CLDR keyword string for the plural form 54 */ getKeyword()55 public final String getKeyword() { 56 return keyword; 57 } 58 59 /** 60 * @param keyword for example "few" or "other" 61 * @return the plural form corresponding to the keyword, or null 62 */ orNullFromString(CharSequence keyword)63 public static final StandardPlural orNullFromString(CharSequence keyword) { 64 switch (keyword.length()) { 65 case 1: 66 if (keyword.charAt(0) == '0') { 67 return EQ_0; 68 } else if (keyword.charAt(0) == '1') { 69 return EQ_1; 70 } 71 break; 72 case 2: 73 if ("=0".contentEquals(keyword)) { 74 return EQ_0; 75 } else if ("=1".contentEquals(keyword)) { 76 return EQ_1; 77 } 78 break; 79 case 3: 80 if ("one".contentEquals(keyword)) { 81 return ONE; 82 } else if ("two".contentEquals(keyword)) { 83 return TWO; 84 } else if ("few".contentEquals(keyword)) { 85 return FEW; 86 } 87 break; 88 case 4: 89 if ("many".contentEquals(keyword)) { 90 return MANY; 91 } else if ("zero".contentEquals(keyword)) { 92 return ZERO; 93 } 94 break; 95 case 5: 96 if ("other".contentEquals(keyword)) { 97 return OTHER; 98 } 99 break; 100 default: 101 break; 102 } 103 return null; 104 } 105 106 /** 107 * @param keyword for example "few" or "other" 108 * @return the plural form corresponding to the keyword, or OTHER 109 */ orOtherFromString(CharSequence keyword)110 public static final StandardPlural orOtherFromString(CharSequence keyword) { 111 StandardPlural p = orNullFromString(keyword); 112 return p != null ? p : OTHER; 113 } 114 115 /** 116 * @param keyword for example "few" or "other" 117 * @return the plural form corresponding to the keyword 118 * @throws IllegalArgumentException if the keyword is not a plural form 119 */ fromString(CharSequence keyword)120 public static final StandardPlural fromString(CharSequence keyword) { 121 StandardPlural p = orNullFromString(keyword); 122 if (p != null) { 123 return p; 124 } else { 125 throw new IllegalArgumentException(keyword.toString()); 126 } 127 } 128 129 /** 130 * @param keyword for example "few" or "other" 131 * @return the index of the plural form corresponding to the keyword, or a negative value 132 */ indexOrNegativeFromString(CharSequence keyword)133 public static final int indexOrNegativeFromString(CharSequence keyword) { 134 StandardPlural p = orNullFromString(keyword); 135 return p != null ? p.ordinal() : -1; 136 } 137 138 /** 139 * @param keyword for example "few" or "other" 140 * @return the index of the plural form corresponding to the keyword, or OTHER_INDEX 141 */ indexOrOtherIndexFromString(CharSequence keyword)142 public static final int indexOrOtherIndexFromString(CharSequence keyword) { 143 StandardPlural p = orNullFromString(keyword); 144 return p != null ? p.ordinal() : OTHER.ordinal(); 145 } 146 147 /** 148 * @param keyword for example "few" or "other" 149 * @return the index of the plural form corresponding to the keyword 150 * @throws IllegalArgumentException if the keyword is not a plural form 151 */ indexFromString(CharSequence keyword)152 public static final int indexFromString(CharSequence keyword) { 153 StandardPlural p = orNullFromString(keyword); 154 if (p != null) { 155 return p.ordinal(); 156 } else { 157 throw new IllegalArgumentException(keyword.toString()); 158 } 159 } 160 } 161