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);
}
}
}
}
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();
}
}
}
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(" }");
}
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;
}
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);
}
}
Aggregations