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