Search in sources :

Example 1 with MeasureUnit

use of android.icu.util.MeasureUnit in project j2objc by google.

the class MeasureUnitTest method testCLDRUnitAvailability.

@Test
public void testCLDRUnitAvailability() {
    Set<MeasureUnit> knownUnits = new HashSet<MeasureUnit>();
    Class cMeasureUnit, cTimeUnit;
    try {
        cMeasureUnit = Class.forName("android.icu.util.MeasureUnit");
        cTimeUnit = Class.forName("android.icu.util.TimeUnit");
    } catch (ClassNotFoundException e) {
        fail("Count not load MeasureUnit or TimeUnit class: " + e.getMessage());
        return;
    }
    for (Field field : cMeasureUnit.getFields()) {
        if (field.getGenericType() == cMeasureUnit || field.getGenericType() == cTimeUnit) {
            try {
                MeasureUnit unit = (MeasureUnit) field.get(cMeasureUnit);
                knownUnits.add(unit);
            } catch (IllegalArgumentException e) {
                fail(e.getMessage());
                return;
            } catch (IllegalAccessException e) {
                fail(e.getMessage());
                return;
            }
        }
    }
    for (String type : MeasureUnit.getAvailableTypes()) {
        if (type.equals("currency") || type.equals("compound")) {
            continue;
        }
        for (MeasureUnit unit : MeasureUnit.getAvailable(type)) {
            if (!knownUnits.contains(unit)) {
                fail("Unit present in CLDR but not available via constant in MeasureUnit: " + unit);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) MeasureUnit(android.icu.util.MeasureUnit) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with MeasureUnit

use of android.icu.util.MeasureUnit in project j2objc by google.

the class MeasureUnitTest method generateCXXConstants.

// DO NOT DELETE THIS FUNCTION! It may appear as dead code, but we use this to generate code
// for MeasureFormat during the release process.
static void generateCXXConstants() {
    System.out.println("");
    TreeMap<String, List<MeasureUnit>> allUnits = getAllUnits();
    System.out.println("static const int32_t gOffsets[] = {");
    int index = 0;
    for (Map.Entry<String, List<MeasureUnit>> entry : allUnits.entrySet()) {
        System.out.printf("    %d,\n", index);
        index += entry.getValue().size();
    }
    System.out.printf("    %d\n", index);
    System.out.println("};");
    System.out.println();
    System.out.println("static const int32_t gIndexes[] = {");
    index = 0;
    for (Map.Entry<String, List<MeasureUnit>> entry : allUnits.entrySet()) {
        System.out.printf("    %d,\n", index);
        if (!entry.getKey().equals("currency")) {
            index += entry.getValue().size();
        }
    }
    System.out.printf("    %d\n", index);
    System.out.println("};");
    System.out.println();
    System.out.println("// Must be sorted alphabetically.");
    System.out.println("static const char * const gTypes[] = {");
    boolean first = true;
    for (Map.Entry<String, List<MeasureUnit>> entry : allUnits.entrySet()) {
        if (!first) {
            System.out.println(",");
        }
        System.out.print("    \"" + entry.getKey() + "\"");
        first = false;
    }
    System.out.println();
    System.out.println("};");
    System.out.println();
    System.out.println("// Must be grouped by type and sorted alphabetically within each type.");
    System.out.println("static const char * const gSubTypes[] = {");
    first = true;
    int offset = 0;
    int typeIdx = 0;
    Map<MeasureUnit, Integer> measureUnitToOffset = new HashMap<MeasureUnit, Integer>();
    Map<MeasureUnit, Pair<Integer, Integer>> measureUnitToTypeSubType = new HashMap<MeasureUnit, Pair<Integer, Integer>>();
    for (Map.Entry<String, List<MeasureUnit>> entry : allUnits.entrySet()) {
        int subTypeIdx = 0;
        for (MeasureUnit unit : entry.getValue()) {
            if (!first) {
                System.out.println(",");
            }
            System.out.print("    \"" + unit.getSubtype() + "\"");
            first = false;
            measureUnitToOffset.put(unit, offset);
            measureUnitToTypeSubType.put(unit, Pair.of(typeIdx, subTypeIdx));
            offset++;
            subTypeIdx++;
        }
        typeIdx++;
    }
    System.out.println();
    System.out.println("};");
    System.out.println();
    // Build unit per unit offsets to corresponding type sub types sorted by
    // unit first and then per unit.
    TreeMap<OrderedPair<Integer, Integer>, Pair<Integer, Integer>> unitPerUnitOffsetsToTypeSubType = new TreeMap<OrderedPair<Integer, Integer>, Pair<Integer, Integer>>();
    for (Map.Entry<MeasureUnit, Pair<MeasureUnit, MeasureUnit>> entry : getUnitsToPerParts().entrySet()) {
        Pair<MeasureUnit, MeasureUnit> unitPerUnit = entry.getValue();
        unitPerUnitOffsetsToTypeSubType.put(OrderedPair.of(measureUnitToOffset.get(unitPerUnit.first), measureUnitToOffset.get(unitPerUnit.second)), measureUnitToTypeSubType.get(entry.getKey()));
    }
    System.out.println("// Must be sorted by first value and then second value.");
    System.out.println("static int32_t unitPerUnitToSingleUnit[][4] = {");
    first = true;
    for (Map.Entry<OrderedPair<Integer, Integer>, Pair<Integer, Integer>> entry : unitPerUnitOffsetsToTypeSubType.entrySet()) {
        if (!first) {
            System.out.println(",");
        }
        first = false;
        OrderedPair<Integer, Integer> unitPerUnitOffsets = entry.getKey();
        Pair<Integer, Integer> typeSubType = entry.getValue();
        System.out.printf("        {%d, %d, %d, %d}", unitPerUnitOffsets.first, unitPerUnitOffsets.second, typeSubType.first, typeSubType.second);
    }
    System.out.println();
    System.out.println("};");
    System.out.println();
    Map<String, MeasureUnit> seen = new HashMap<String, MeasureUnit>();
    for (Map.Entry<String, List<MeasureUnit>> entry : allUnits.entrySet()) {
        String type = entry.getKey();
        if (type.equals("currency")) {
            continue;
        }
        for (MeasureUnit unit : entry.getValue()) {
            String name = toCamelCase(unit);
            Pair<Integer, Integer> typeSubType = measureUnitToTypeSubType.get(unit);
            if (typeSubType == null) {
                throw new IllegalStateException();
            }
            checkForDup(seen, name, unit);
            System.out.printf("MeasureUnit *MeasureUnit::create%s(UErrorCode &status) {\n", name);
            System.out.printf("    return MeasureUnit::create(%d, %d, status);\n", typeSubType.first, typeSubType.second);
            System.out.println("}");
            System.out.println();
        }
    }
}
Also used : HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) MeasureUnit(android.icu.util.MeasureUnit) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Pair(android.icu.impl.Pair)

Example 3 with MeasureUnit

use of android.icu.util.MeasureUnit in project j2objc by google.

the class MeasureUnitTest method generateBackwardCompatibilityTest.

// DO NOT DELETE THIS FUNCTION! It may appear as dead code, but we use this to generate code
// for MeasureFormat during the release process.
static void generateBackwardCompatibilityTest(String version) {
    Map<String, MeasureUnit> seen = new HashMap<String, MeasureUnit>();
    System.out.println();
    System.out.printf("    public void TestCompatible%s() {\n", version.replace(".", "_"));
    System.out.println("        MeasureUnit[] units = {");
    TreeMap<String, List<MeasureUnit>> allUnits = getAllUnits();
    int count = 0;
    for (Map.Entry<String, List<MeasureUnit>> entry : allUnits.entrySet()) {
        if (isTypeHidden(entry.getKey())) {
            continue;
        }
        for (MeasureUnit unit : entry.getValue()) {
            String javaName = toJAVAName(unit);
            checkForDup(seen, javaName, unit);
            System.out.printf("                MeasureUnit.%s,\n", javaName);
            count++;
        }
    }
    System.out.println("        };");
    System.out.printf("        assertEquals(\"\",  %d, units.length);\n", count);
    System.out.println("    }");
}
Also used : MeasureUnit(android.icu.util.MeasureUnit) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 4 with MeasureUnit

use of android.icu.util.MeasureUnit in project j2objc by google.

the class MeasureUnitTest method getUnitsToPerParts.

// DO NOT DELETE THIS FUNCTION! It may appear as dead code, but we use this to generate code
// for MeasureFormat during the release process.
static Map<MeasureUnit, Pair<MeasureUnit, MeasureUnit>> getUnitsToPerParts() {
    TreeMap<String, List<MeasureUnit>> allUnits = getAllUnits();
    Map<MeasureUnit, Pair<String, String>> unitsToPerStrings = new HashMap<MeasureUnit, Pair<String, String>>();
    Map<String, MeasureUnit> namesToUnits = new HashMap<String, MeasureUnit>();
    for (Map.Entry<String, List<MeasureUnit>> entry : allUnits.entrySet()) {
        String type = entry.getKey();
        // Currency types are always atomic units, so we can skip these
        if (type.equals("currency")) {
            continue;
        }
        for (MeasureUnit unit : entry.getValue()) {
            String javaName = toJAVAName(unit);
            String[] nameParts = javaName.split("_PER_");
            if (nameParts.length == 1) {
                namesToUnits.put(nameParts[0], unit);
            } else if (nameParts.length == 2) {
                unitsToPerStrings.put(unit, Pair.of(nameParts[0], nameParts[1]));
            }
        }
    }
    Map<MeasureUnit, Pair<MeasureUnit, MeasureUnit>> unitsToPerUnits = new HashMap<MeasureUnit, Pair<MeasureUnit, MeasureUnit>>();
    for (Map.Entry<MeasureUnit, Pair<String, String>> entry : unitsToPerStrings.entrySet()) {
        Pair<String, String> perStrings = entry.getValue();
        MeasureUnit unit = namesToUnits.get(perStrings.first);
        MeasureUnit perUnit = namesToUnits.get(perStrings.second);
        if (unit != null && perUnit != null) {
            unitsToPerUnits.put(entry.getKey(), Pair.of(unit, perUnit));
        }
    }
    return unitsToPerUnits;
}
Also used : HashMap(java.util.HashMap) MeasureUnit(android.icu.util.MeasureUnit) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Pair(android.icu.impl.Pair)

Example 5 with MeasureUnit

use of android.icu.util.MeasureUnit in project j2objc by google.

the class MeasureUnitTest method testAUnit.

@Test
public void testAUnit() {
    String lastType = null;
    for (MeasureUnit expected : MeasureUnit.getAvailable()) {
        String type = expected.getType();
        String code = expected.getSubtype();
        if (!type.equals(lastType)) {
            logln(type);
            lastType = type;
        }
        MeasureUnit actual = MeasureUnit.internalGetInstance(type, code);
        assertSame("Identity check", expected, actual);
    }
}
Also used : MeasureUnit(android.icu.util.MeasureUnit) Test(org.junit.Test)

Aggregations

MeasureUnit (android.icu.util.MeasureUnit)16 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 List (java.util.List)7 Map (java.util.Map)7 TreeMap (java.util.TreeMap)7 Test (org.junit.Test)6 MeasureFormat (android.icu.text.MeasureFormat)4 FormatWidth (android.icu.text.MeasureFormat.FormatWidth)4 Measure (android.icu.util.Measure)4 Pair (android.icu.impl.Pair)3 ULocale (android.icu.util.ULocale)3 StandardPlural (android.icu.impl.StandardPlural)2 Currency (android.icu.util.Currency)2 Field (java.lang.reflect.Field)2 FieldPosition (java.text.FieldPosition)2 DontCareFieldPosition (android.icu.impl.DontCareFieldPosition)1 NumberFormat (android.icu.text.NumberFormat)1 CurrencyAmount (android.icu.util.CurrencyAmount)1 HashSet (java.util.HashSet)1