summaryrefslogtreecommitdiff
path: root/android_icu4j/src/main/java/android/icu/impl/number/Grouper.java
blob: 932a8148c83472782f3da7ced249866a50e48b72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* GENERATED SOURCE. DO NOT MODIFY. */
// © 2017 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
package android.icu.impl.number;

import android.icu.impl.ICUData;
import android.icu.impl.ICUResourceBundle;
import android.icu.impl.number.PatternStringParser.ParsedPatternInfo;
import android.icu.number.NumberFormatter.GroupingStrategy;
import android.icu.util.ULocale;
import android.icu.util.UResourceBundle;

/**
 * A full options object for grouping sizes.
 * @hide Only a subset of ICU is exposed in Android
 */
public class Grouper {

    private static final Grouper GROUPER_NEVER = new Grouper((short) -1, (short) -1, (short) -2);
    private static final Grouper GROUPER_MIN2 = new Grouper((short) -2, (short) -2, (short) -3);
    private static final Grouper GROUPER_AUTO = new Grouper((short) -2, (short) -2, (short) -2);
    private static final Grouper GROUPER_ON_ALIGNED = new Grouper((short) -4, (short) -4, (short) 1);

    private static final Grouper GROUPER_WESTERN = new Grouper((short) 3, (short) 3, (short) 1);
    private static final Grouper GROUPER_INDIC = new Grouper((short) 3, (short) 2, (short) 1);
    private static final Grouper GROUPER_WESTERN_MIN2 = new Grouper((short) 3, (short) 3, (short) 2);
    private static final Grouper GROUPER_INDIC_MIN2 = new Grouper((short) 3, (short) 2, (short) 2);

    /**
     * Convert from the GroupingStrategy enum to a Grouper object.
     */
    public static Grouper forStrategy(GroupingStrategy grouping) {
        switch (grouping) {
        case OFF:
            return GROUPER_NEVER;
        case MIN2:
            return GROUPER_MIN2;
        case AUTO:
            return GROUPER_AUTO;
        case ON_ALIGNED:
            return GROUPER_ON_ALIGNED;
        case THOUSANDS:
            return GROUPER_WESTERN;
        default:
            throw new AssertionError();
        }
    }

    /**
     * Resolve the values in Properties to a Grouper object.
     */
    public static Grouper forProperties(DecimalFormatProperties properties) {
        if (!properties.getGroupingUsed()) {
            return GROUPER_NEVER;
        }
        short grouping1 = (short) properties.getGroupingSize();
        short grouping2 = (short) properties.getSecondaryGroupingSize();
        short minGrouping = (short) properties.getMinimumGroupingDigits();
        grouping1 = grouping1 > 0 ? grouping1 : grouping2 > 0 ? grouping2 : grouping1;
        grouping2 = grouping2 > 0 ? grouping2 : grouping1;
        return getInstance(grouping1, grouping2, minGrouping);
    }

    public static Grouper getInstance(short grouping1, short grouping2, short minGrouping) {
        if (grouping1 == -1) {
            return GROUPER_NEVER;
        } else if (grouping1 == 3 && grouping2 == 3 && minGrouping == 1) {
            return GROUPER_WESTERN;
        } else if (grouping1 == 3 && grouping2 == 2 && minGrouping == 1) {
            return GROUPER_INDIC;
        } else if (grouping1 == 3 && grouping2 == 3 && minGrouping == 2) {
            return GROUPER_WESTERN_MIN2;
        } else if (grouping1 == 3 && grouping2 == 2 && minGrouping == 2) {
            return GROUPER_INDIC_MIN2;
        } else {
            return new Grouper(grouping1, grouping2, minGrouping);
        }
    }

    private static short getMinGroupingForLocale(ULocale locale) {
        // TODO: Cache this?
        ICUResourceBundle resource = (ICUResourceBundle) UResourceBundle
                .getBundleInstance(ICUData.ICU_BASE_NAME, locale);
        String result = resource.getStringWithFallback("NumberElements/minimumGroupingDigits");
        return Short.valueOf(result);
    }

    /**
     * The primary grouping size, with the following special values:
     * <ul>
     * <li>-1 = no grouping
     * <li>-2 = needs locale data
     * <li>-4 = fall back to Western grouping if not in locale
     * </ul>
     */
    private final short grouping1;

    /**
     * The secondary grouping size, with the following special values:
     * <ul>
     * <li>-1 = no grouping
     * <li>-2 = needs locale data
     * <li>-4 = fall back to Western grouping if not in locale
     * </ul>
     */
    private final short grouping2;

    /**
     * The minimum grouping size, with the following special values:
     * <ul>
     * <li>-2 = needs locale data
     * <li>-3 = no less than 2
     * </ul>
     */
    private final short minGrouping;

    private Grouper(short grouping1, short grouping2, short minGrouping) {
        this.grouping1 = grouping1;
        this.grouping2 = grouping2;
        this.minGrouping = minGrouping;
    }

    public Grouper withLocaleData(ULocale locale, ParsedPatternInfo patternInfo) {
        short minGrouping;
        if (this.minGrouping == -2) {
            minGrouping = getMinGroupingForLocale(locale);
        } else if (this.minGrouping == -3) {
            minGrouping = (short) Math.max(2, getMinGroupingForLocale(locale));
        } else {
            minGrouping = this.minGrouping;
        }

        if (this.grouping1 != -2 && this.grouping2 != -4) {
            if (minGrouping == this.minGrouping) {
              return this;
            }
            return getInstance(this.grouping1, this.grouping2, minGrouping);
        }

        short grouping1 = (short) (patternInfo.positive.groupingSizes & 0xffff);
        short grouping2 = (short) ((patternInfo.positive.groupingSizes >>> 16) & 0xffff);
        short grouping3 = (short) ((patternInfo.positive.groupingSizes >>> 32) & 0xffff);
        if (grouping2 == -1) {
            grouping1 = this.grouping1 == -4 ? (short) 3 : (short) -1;
        }
        if (grouping3 == -1) {
            grouping2 = grouping1;
        }

        return getInstance(grouping1, grouping2, minGrouping);
    }

    public boolean groupAtPosition(int position, DecimalQuantity value) {
        assert grouping1 != -2 && grouping1 != -4;
        if (grouping1 == -1 || grouping1 == 0) {
            // Either -1 or 0 means "no grouping"
            return false;
        }
        position -= grouping1;
        return position >= 0
                && (position % grouping2) == 0
                && value.getUpperDisplayMagnitude() - grouping1 + 1 >= minGrouping;
    }

    public short getPrimary() {
        return grouping1;
    }

    public short getSecondary() {
        return grouping2;
    }
}