NativePluralRules.java revision 21557bb6a8f35a2f9889adba449cac950c9d41b9
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.icu;
18
19import java.util.Locale;
20
21/**
22 * Provides access to ICU's
23 * <a href="http://icu-project.org/apiref/icu4c/classPluralRules.html">PluralRules</a> class.
24 * This is not necessary for Java API, but is used by frameworks/base's resources system to
25 * ease localization of strings to languages with complex grammatical rules regarding number.
26 */
27public final class NativePluralRules {
28    public static final int ZERO  = 0;
29    public static final int ONE   = 1;
30    public static final int TWO   = 2;
31    public static final int FEW   = 3;
32    public static final int MANY  = 4;
33    public static final int OTHER = 5;
34
35    private final int address;
36
37    private NativePluralRules(int address) {
38        this.address = address;
39    }
40
41    @Override public void finalize() {
42        finalizeImpl(address);
43    }
44
45    public static NativePluralRules forLocale(Locale locale) {
46        return new NativePluralRules(forLocaleImpl(locale.toString()));
47    }
48
49    /**
50     * Returns the constant defined in this class corresponding
51     * to the first rule that matches the given value.
52     */
53    public int quantityForInt(int value) {
54        return quantityForIntImpl(address, value);
55    }
56
57    private static native void finalizeImpl(int address);
58    private static native int forLocaleImpl(String localeName);
59    private static native int quantityForIntImpl(int address, int value);
60}
61