aboutsummaryrefslogtreecommitdiffstats
path: root/tools/java/org/unicode/cldr/util/PluralRanges.java
blob: 839eefdc87eb9c96ecf30c1e93a1c2165fc16107 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package org.unicode.cldr.util;

import java.util.EnumSet;

import org.unicode.cldr.util.SupplementalDataInfo.PluralInfo.Count;

import com.ibm.icu.impl.Utility;
import com.ibm.icu.util.Freezable;
import com.ibm.icu.util.Output;
import com.ibm.icu.util.ULocale;

/**
 * Utility class for returning the plural category for a range of numbers, such as 1–5, so that appropriate messages can be chosen.
 * The rules for determining this value vary widely across locales.
 * @author markdavis
 */
public final class PluralRanges implements Comparable<PluralRanges>, Freezable<PluralRanges> {

    /**
     * Internal class for mapping from two Count values to another.
     * @internal
     * @deprecated
     */
    public static final class Matrix implements Comparable<Matrix>, Cloneable {
        private byte[] data = new byte[Count.LENGTH * Count.LENGTH];
        {
            for (int i = 0; i < data.length; ++i) {
                data[i] = -1;
            }
        }

        /**
         * Internal method for setting.
         * @internal
         * @deprecated
         */
        public void set(Count start, Count end, Count result) {
            data[start.ordinal() * Count.LENGTH + end.ordinal()] = result == null ? (byte) -1 : (byte) result.ordinal();
        }

        /**
         * Internal method for setting; throws exception if already set.
         * @internal
         * @deprecated
         */
        public void setIfNew(Count start, Count end, Count result) {
            byte old = data[start.ordinal() * Count.LENGTH + end.ordinal()];
            if (old >= 0) {
                throw new IllegalArgumentException("Previously set value for <" +
                    start + ", " + end + ", " + Count.VALUES.get(old) + ">");
            }
            data[start.ordinal() * Count.LENGTH + end.ordinal()] = result == null ? (byte) -1 : (byte) result.ordinal();
        }

        /**
         * Internal method for getting.
         * @internal
         * @deprecated
         */
        public Count get(Count start, Count end) {
            byte result = data[start.ordinal() * Count.LENGTH + end.ordinal()];
            return result < 0 ? null : Count.VALUES.get(result);
        }

        /**
         * Internal method to see if <*,end> values are all the same.
         * @internal
         * @deprecated
         */
        public Count endSame(Count end) {
            Count first = null;
            for (Count start : Count.VALUES) {
                Count item = get(start, end);
                if (item == null) {
                    continue;
                }
                if (first == null) {
                    first = item;
                    continue;
                }
                if (first != item) {
                    return null;
                }
            }
            return first;
        }

        /**
         * Internal method to see if <start,*> values are all the same.
         * @internal
         * @deprecated
         */
        public Count startSame(Count start, EnumSet<Count> endDone, Output<Boolean> emit) {
            emit.value = false;
            Count first = null;
            for (Count end : Count.VALUES) {
                Count item = get(start, end);
                if (item == null) {
                    continue;
                }
                if (first == null) {
                    first = item;
                    continue;
                }
                if (first != item) {
                    return null;
                }
                // only emit if we didn't cover with the 'end' values
                if (!endDone.contains(end)) {
                    emit.value = true;
                }
            }
            return first;
        }

        @Override
        public int hashCode() {
            int result = 0;
            for (int i = 0; i < data.length; ++i) {
                result = result * 37 + data[i];
            }
            return result;
        }

        @Override
        public boolean equals(Object other) {
            if (!(other instanceof Matrix)) {
                return false;
            }
            return 0 == compareTo((Matrix) other);
        }

        @Override
        public int compareTo(Matrix o) {
            for (int i = 0; i < data.length; ++i) {
                int diff = data[i] - o.data[i];
                if (diff != 0) {
                    return diff;
                }
            }
            return 0;
        }

        @Override
        public Matrix clone() {
            Matrix result = new Matrix();
            result.data = data.clone();
            return result;
        }
    }

    private Matrix matrix = new Matrix();
    private boolean[] explicit = new boolean[Count.LENGTH];

    /**
     * Returns a
     * @return
     */
    public static PluralRanges getInstance(ULocale locale) {
        return null;
    }

    /**
     * Internal method for building. If the start or end are null, it means everything of that type.
     * @param rangeStart
     * @param rangeEnd
     * @param result
     * @internal
     * @deprecated
     */
    public void add(Count rangeStart, Count rangeEnd, Count result) {
        explicit[result.ordinal()] = true;
        if (rangeStart == null) {
            for (Count rs : Count.values()) {
                if (rangeEnd == null) {
                    for (Count re : Count.values()) {
                        matrix.setIfNew(rs, re, result);
                    }
                } else {
                    explicit[rangeEnd.ordinal()] = true;
                    matrix.setIfNew(rs, rangeEnd, result);
                }
            }
        } else if (rangeEnd == null) {
            explicit[rangeStart.ordinal()] = true;
            for (Count re : Count.values()) {
                matrix.setIfNew(rangeStart, re, result);
            }
        } else {
            explicit[rangeStart.ordinal()] = true;
            explicit[rangeEnd.ordinal()] = true;
            matrix.setIfNew(rangeStart, rangeEnd, result);
        }
    }

    /**
     * Internal method to show a range in XML format.
     * @param start
     * @param end
     * @param result
     * @return
     * @internal
     * @deprecated
     */
    public static String showRange(Count start, Count end, Count result) {
        String startEnd = "start=\"" + start + "\"" + Utility.repeat(" ", 5 - start.toString().length())
            + " end=\"" + end + "\"" + Utility.repeat(" ", 5 - end.toString().length());
        return result == null
            ? "<!--         " + startEnd + " result=? -->"
            : "<pluralRange " + startEnd + " result=\"" + result + "\"/>";
    }

    /**
     * Returns the appropriate plural category for a range from start to end.
     * If there is no available data, then 'other' is returned.
     * @param start
     * @param end
     * @return
     */
    public Count get(Count start, Count end) {
        Count result = matrix.get(start, end);
        return result == null ? Count.other : result;
    }

    /**
     * Returns the appropriate plural category for a range from start to end. If the combination does not
     * explicitly occur in the data, returns null.
     * @param start
     * @param end
     * @return
     */
    public Count getExplicit(Count start, Count end) {
        return matrix.get(start, end);
    }

    /**
     * Internal method to determines whether the Count was explicitly used in any add statement.
     * @param count
     * @return
     * @internal
     * @deprecated
     */
    public boolean isExplicitlySet(Count count) {
        return explicit[count.ordinal()];
    }

    @Override
    public boolean equals(Object other) {
        return other instanceof PluralRanges ? matrix.equals((PluralRanges) other) : false;
    }

    @Override
    public int hashCode() {
        return matrix.hashCode();
    }

    @Override
    public int compareTo(PluralRanges that) {
        return matrix.compareTo(that.matrix);
    }

    volatile boolean isFrozen;

    @Override
    public boolean isFrozen() {
        return isFrozen;
    }

    @Override
    public PluralRanges freeze() {
        isFrozen = true;
        return this;
    }

    @Override
    public PluralRanges cloneAsThawed() {
        PluralRanges result = new PluralRanges();
        result.explicit = explicit.clone();
        result.matrix = matrix.clone();
        return result;
    }
}