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