Home | History | Annotate | Download | only in text
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2016 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 /*
      5  *******************************************************************************
      6  * Copyright (C) 2008-2015, Google, International Business Machines Corporation and
      7  * others. All Rights Reserved.
      8  *******************************************************************************
      9  */
     10 package android.icu.text;
     11 
     12 import java.util.Arrays;
     13 import java.util.EnumSet;
     14 
     15 import android.icu.impl.StandardPlural;
     16 import android.icu.util.Freezable;
     17 import android.icu.util.Output;
     18 
     19 /**
     20  * Utility class for returning the plural category for a range of numbers, such as 15, so that appropriate messages can
     21  * be chosen. The rules for determining this value vary widely across locales.
     22  *
     23  * @author markdavis
     24  * @deprecated This API is ICU internal only.
     25  * @hide Only a subset of ICU is exposed in Android
     26  * @hide draft / provisional / internal are hidden on Android
     27  */
     28 @Deprecated
     29 public final class PluralRanges implements Freezable<PluralRanges>, Comparable<PluralRanges> {
     30 
     31     private volatile boolean isFrozen;
     32     private Matrix matrix = new Matrix();
     33     private boolean[] explicit = new boolean[StandardPlural.COUNT];
     34 
     35     /**
     36      * Constructor
     37      *
     38      * @deprecated This API is ICU internal only.
     39      * @hide draft / provisional / internal are hidden on Android
     40      */
     41     @Deprecated
     42     public PluralRanges() {
     43     }
     44 
     45     /**
     46      * Internal class for mapping from two StandardPluralCategories values to another.
     47      */
     48     private static final class Matrix implements Comparable<Matrix>, Cloneable {
     49         private byte[] data = new byte[StandardPlural.COUNT * StandardPlural.COUNT];
     50         {
     51             for (int i = 0; i < data.length; ++i) {
     52                 data[i] = -1;
     53             }
     54         }
     55 
     56         Matrix() {
     57         }
     58 
     59         /**
     60          * Internal method for setting.
     61          */
     62         @SuppressWarnings("unused")
     63         void set(StandardPlural start, StandardPlural end, StandardPlural result) {
     64             data[start.ordinal() * StandardPlural.COUNT + end.ordinal()] = result == null ? (byte) -1
     65                     : (byte) result.ordinal();
     66         }
     67 
     68         /**
     69          * Internal method for setting; throws exception if already set.
     70          */
     71         void setIfNew(StandardPlural start, StandardPlural end,
     72                 StandardPlural result) {
     73             byte old = data[start.ordinal() * StandardPlural.COUNT + end.ordinal()];
     74             if (old >= 0) {
     75                 throw new IllegalArgumentException("Previously set value for <" + start + ", " + end + ", "
     76                         + StandardPlural.VALUES.get(old) + ">");
     77             }
     78             data[start.ordinal() * StandardPlural.COUNT + end.ordinal()] = result == null ? (byte) -1
     79                     : (byte) result.ordinal();
     80         }
     81 
     82         /**
     83          * Internal method for getting.
     84          */
     85         StandardPlural get(StandardPlural start, StandardPlural end) {
     86             byte result = data[start.ordinal() * StandardPlural.COUNT + end.ordinal()];
     87             return result < 0 ? null : StandardPlural.VALUES.get(result);
     88         }
     89 
     90         /**
     91          * Internal method to see if <*,end> values are all the same.
     92          */
     93         @SuppressWarnings("unused")
     94         StandardPlural endSame(StandardPlural end) {
     95             StandardPlural first = null;
     96             for (StandardPlural start : StandardPlural.VALUES) {
     97                 StandardPlural item = get(start, end);
     98                 if (item == null) {
     99                     continue;
    100                 }
    101                 if (first == null) {
    102                     first = item;
    103                     continue;
    104                 }
    105                 if (first != item) {
    106                     return null;
    107                 }
    108             }
    109             return first;
    110         }
    111 
    112         /**
    113          * Internal method to see if <start,*> values are all the same.
    114          */
    115         @SuppressWarnings("unused")
    116         StandardPlural startSame(StandardPlural start,
    117                 EnumSet<StandardPlural> endDone, Output<Boolean> emit) {
    118             emit.value = false;
    119             StandardPlural first = null;
    120             for (StandardPlural end : StandardPlural.VALUES) {
    121                 StandardPlural item = get(start, end);
    122                 if (item == null) {
    123                     continue;
    124                 }
    125                 if (first == null) {
    126                     first = item;
    127                     continue;
    128                 }
    129                 if (first != item) {
    130                     return null;
    131                 }
    132                 // only emit if we didn't cover with the 'end' values
    133                 if (!endDone.contains(end)) {
    134                     emit.value = true;
    135                 }
    136             }
    137             return first;
    138         }
    139 
    140         @Override
    141         public int hashCode() {
    142             int result = 0;
    143             for (int i = 0; i < data.length; ++i) {
    144                 result = result * 37 + data[i];
    145             }
    146             return result;
    147         }
    148 
    149         @Override
    150         public boolean equals(Object other) {
    151             if (!(other instanceof Matrix)) {
    152                 return false;
    153             }
    154             return 0 == compareTo((Matrix) other);
    155         }
    156 
    157         @Override
    158         public int compareTo(Matrix o) {
    159             for (int i = 0; i < data.length; ++i) {
    160                 int diff = data[i] - o.data[i];
    161                 if (diff != 0) {
    162                     return diff;
    163                 }
    164             }
    165             return 0;
    166         }
    167 
    168         @Override
    169         public Matrix clone() {
    170             Matrix result = new Matrix();
    171             result.data = data.clone();
    172             return result;
    173         }
    174 
    175         @Override
    176         public String toString() {
    177             StringBuilder result = new StringBuilder();
    178             for (StandardPlural i : StandardPlural.values()) {
    179                 for (StandardPlural j : StandardPlural.values()) {
    180                     StandardPlural x = get(i, j);
    181                     if (x != null) {
    182                         result.append(i + " & " + j + "  " + x + ";\n");
    183                     }
    184                 }
    185             }
    186             return result.toString();
    187         }
    188     }
    189 
    190     /**
    191      * Internal method for building. If the start or end are null, it means everything of that type.
    192      *
    193      * @param rangeStart
    194      *            plural category for the start of the range
    195      * @param rangeEnd
    196      *            plural category for the end of the range
    197      * @param result
    198      *            the resulting plural category
    199      * @deprecated This API is ICU internal only.
    200      * @hide draft / provisional / internal are hidden on Android
    201      */
    202     @Deprecated
    203     public void add(StandardPlural rangeStart, StandardPlural rangeEnd,
    204             StandardPlural result) {
    205         if (isFrozen) {
    206             throw new UnsupportedOperationException();
    207         }
    208         explicit[result.ordinal()] = true;
    209         if (rangeStart == null) {
    210             for (StandardPlural rs : StandardPlural.values()) {
    211                 if (rangeEnd == null) {
    212                     for (StandardPlural re : StandardPlural.values()) {
    213                         matrix.setIfNew(rs, re, result);
    214                     }
    215                 } else {
    216                     explicit[rangeEnd.ordinal()] = true;
    217                     matrix.setIfNew(rs, rangeEnd, result);
    218                 }
    219             }
    220         } else if (rangeEnd == null) {
    221             explicit[rangeStart.ordinal()] = true;
    222             for (StandardPlural re : StandardPlural.values()) {
    223                 matrix.setIfNew(rangeStart, re, result);
    224             }
    225         } else {
    226             explicit[rangeStart.ordinal()] = true;
    227             explicit[rangeEnd.ordinal()] = true;
    228             matrix.setIfNew(rangeStart, rangeEnd, result);
    229         }
    230     }
    231 
    232     /**
    233      * Returns the appropriate plural category for a range from start to end. If there is no available data, then
    234      * 'end' is returned as an implicit value. (Such an implicit value can be tested for with {@link #isExplicit}.)
    235      *
    236      * @param start
    237      *            plural category for the start of the range
    238      * @param end
    239      *            plural category for the end of the range
    240      * @return the resulting plural category, or 'end' if there is no data.
    241      * @deprecated This API is ICU internal only.
    242      * @hide draft / provisional / internal are hidden on Android
    243      */
    244     @Deprecated
    245     public StandardPlural get(StandardPlural start, StandardPlural end) {
    246         StandardPlural result = matrix.get(start, end);
    247         return result == null ? end : result;
    248     }
    249 
    250     /**
    251      * Returns whether the appropriate plural category for a range from start to end
    252      * is explicitly in the data (vs given an implicit value). See also {@link #get}.
    253      *
    254      * @param start
    255      *            plural category for the start of the range
    256      * @param end
    257      *            plural category for the end of the range
    258      * @return whether the value for (start,end) is explicit or not.
    259      * @deprecated This API is ICU internal only.
    260      * @hide draft / provisional / internal are hidden on Android
    261      */
    262     @Deprecated
    263     public boolean isExplicit(StandardPlural start, StandardPlural end) {
    264         return matrix.get(start, end) != null;
    265     }
    266 
    267     /**
    268      * Internal method to determines whether the StandardPluralCategories was explicitly used in any add statement.
    269      *
    270      * @param count
    271      *            plural category to test
    272      * @return true if set
    273      * @deprecated This API is ICU internal only.
    274      * @hide draft / provisional / internal are hidden on Android
    275      */
    276     @Deprecated
    277     public boolean isExplicitlySet(StandardPlural count) {
    278         return explicit[count.ordinal()];
    279     }
    280 
    281     /**
    282      * {@inheritDoc}
    283      * @deprecated This API is ICU internal only.
    284      * @hide draft / provisional / internal are hidden on Android
    285      */
    286     @Deprecated
    287     @Override
    288     public boolean equals(Object other) {
    289         if (this == other) {
    290             return true;
    291         }
    292         if (!(other instanceof PluralRanges)) {
    293             return false;
    294         }
    295         PluralRanges otherPR = (PluralRanges)other;
    296         return matrix.equals(otherPR.matrix) && Arrays.equals(explicit, otherPR.explicit);
    297     }
    298 
    299     /**
    300      * {@inheritDoc}
    301      * @deprecated This API is ICU internal only.
    302      * @hide draft / provisional / internal are hidden on Android
    303      */
    304     @Override
    305     @Deprecated
    306     public int hashCode() {
    307         return matrix.hashCode();
    308     }
    309 
    310     /**
    311      * {@inheritDoc}
    312      * @deprecated This API is ICU internal only.
    313      * @hide draft / provisional / internal are hidden on Android
    314      */
    315     @Override
    316     @Deprecated
    317     public int compareTo(PluralRanges that) {
    318         return matrix.compareTo(that.matrix);
    319     }
    320 
    321     /**
    322      * {@inheritDoc}
    323      * @deprecated This API is ICU internal only.
    324      * @hide draft / provisional / internal are hidden on Android
    325      */
    326     @Override
    327     @Deprecated
    328     public boolean isFrozen() {
    329         return isFrozen;
    330     }
    331 
    332     /**
    333      * {@inheritDoc}
    334      * @deprecated This API is ICU internal only.
    335      * @hide draft / provisional / internal are hidden on Android
    336      */
    337     @Override
    338     @Deprecated
    339     public PluralRanges freeze() {
    340         isFrozen = true;
    341         return this;
    342     }
    343 
    344     /**
    345      * {@inheritDoc}
    346      * @deprecated This API is ICU internal only.
    347      * @hide draft / provisional / internal are hidden on Android
    348      */
    349     @Override
    350     @Deprecated
    351     public PluralRanges cloneAsThawed() {
    352         PluralRanges result = new PluralRanges();
    353         result.explicit = explicit.clone();
    354         result.matrix = matrix.clone();
    355         return result;
    356     }
    357 
    358     /**
    359      * {@inheritDoc}
    360      * @deprecated This API is ICU internal only.
    361      * @hide draft / provisional / internal are hidden on Android
    362      */
    363     @Override
    364     @Deprecated
    365     public String toString() {
    366         return matrix.toString();
    367     }
    368 }