Search in sources :

Example 1 with Pair

use of android.icu.impl.Pair 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 2 with Pair

use of android.icu.impl.Pair 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 3 with Pair

use of android.icu.impl.Pair in project j2objc by google.

the class MeasureUnitTest method generateConstants.

// 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 generateConstants(String thisVersion) {
    System.out.println();
    Map<String, MeasureUnit> seen = new HashMap<String, MeasureUnit>();
    TreeMap<String, List<MeasureUnit>> allUnits = getAllUnits();
    for (Map.Entry<String, List<MeasureUnit>> entry : allUnits.entrySet()) {
        String type = entry.getKey();
        if (isTypeHidden(type)) {
            continue;
        }
        for (MeasureUnit unit : entry.getValue()) {
            String name = toJAVAName(unit);
            String code = unit.getSubtype();
            checkForDup(seen, name, unit);
            System.out.println("    /**");
            System.out.println("     * Constant for unit of " + type + ": " + code);
            // Special case JAVA had old constants for time from before.
            if ("duration".equals(type) && TIME_CODES.contains(code)) {
                System.out.println("     * @stable ICU 4.0");
            } else if (isDraft(name)) {
                System.out.println("     * @draft ICU " + getVersion(name, thisVersion));
                System.out.println("     * @provisional This API might change or be removed in a future release.");
            } else {
                System.out.println("     * @stable ICU " + getVersion(name, thisVersion));
            }
            System.out.println("    */");
            if ("duration".equals(type) && TIME_CODES.contains(code)) {
                System.out.println("    public static final TimeUnit " + name + " = (TimeUnit) MeasureUnit.internalGetInstance(\"" + type + "\", \"" + code + "\");");
            } else {
                System.out.println("    public static final MeasureUnit " + name + " = MeasureUnit.internalGetInstance(\"" + type + "\", \"" + code + "\");");
            }
            System.out.println();
        }
    }
    System.out.println("    private static HashMap<Pair<MeasureUnit, MeasureUnit>, MeasureUnit>unitPerUnitToSingleUnit =");
    System.out.println("            new HashMap<Pair<MeasureUnit, MeasureUnit>, MeasureUnit>();");
    System.out.println();
    System.out.println("    static {");
    for (Map.Entry<MeasureUnit, Pair<MeasureUnit, MeasureUnit>> unitPerUnitEntry : getUnitsToPerParts().entrySet()) {
        Pair<MeasureUnit, MeasureUnit> unitPerUnit = unitPerUnitEntry.getValue();
        System.out.println("        unitPerUnitToSingleUnit.put(Pair.<MeasureUnit, MeasureUnit>of(MeasureUnit." + toJAVAName(unitPerUnit.first) + ", MeasureUnit." + toJAVAName(unitPerUnit.second) + "), MeasureUnit." + toJAVAName(unitPerUnitEntry.getKey()) + ");");
    }
    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) Pair(android.icu.impl.Pair)

Aggregations

Pair (android.icu.impl.Pair)3 MeasureUnit (android.icu.util.MeasureUnit)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3