Skip to content

Commit

Permalink
Format improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
keilw committed Nov 23, 2024
1 parent 1487697 commit 467a85e
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 89 deletions.
16 changes: 10 additions & 6 deletions src/main/java/tech/units/indriya/format/EBNFHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
*
* @author otaviojava
* @author keilw
* @version 1.3
* @version 1.4
*/
class EBNFHelper {

Expand Down Expand Up @@ -100,17 +100,21 @@ static int formatInternal(Unit<?> unit, Appendable buffer, SymbolMap symbolMap)
}
} else if (CUBIC_METRE.equals(parentUnit)) {
if (converter != null) {
parentUnit = LITRE;

parentUnit = LITRE;
}
} else if (METRE.equals(parentUnit)) {
final String s = "m";
final String s = "m";
}

if (unit instanceof TransformedUnit) {
TransformedUnit<?> transUnit = (TransformedUnit<?>) unit;
if (parentUnit == null)
parentUnit = transUnit.getSystemUnit();
if (parentUnit == null) {
parentUnit = transUnit.getParentUnit();
} else {
if (transUnit.getParentUnit() != null) {
parentUnit = transUnit.getParentUnit();
}
}
converter = transUnit.getConverter();
}

Expand Down
144 changes: 93 additions & 51 deletions src/main/java/tech/units/indriya/format/SimpleUnitFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
* @author <a href="mailto:[email protected]">Werner Keil</a>
* @author Eric Russell
* @author Andi Huber
* @version 2.14, Oct 2, 2024
* @version 2.15, Nov 24, 2024
* @since 1.0
*/
public abstract class SimpleUnitFormat extends AbstractUnitFormat {
Expand Down Expand Up @@ -432,20 +432,12 @@ private DefaultFormat init() {

label(MICRO(Units.GRAM), MetricPrefix.MICRO.getSymbol() + "g");

// Alias and ASCIIFormat for Ohm
alias(Units.OHM, "Ohm");
for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
alias(Units.OHM.prefix(MetricPrefix.values()[i]), METRIC_PREFIX_SYMBOLS[i] + "Ohm");
}
// Alias in ASCIIFormat for Ohm
aliasWithPrefixes(Units.OHM, "Ohm");

// Special case for DEGREE_CELSIUS.
label(Units.CELSIUS, "℃");
alias(Units.CELSIUS, "°C");
for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
label(Units.CELSIUS.prefix(MetricPrefix.values()[i]), METRIC_PREFIX_SYMBOLS[i] + "℃");
alias(Units.CELSIUS.prefix(MetricPrefix.values()[i]), METRIC_PREFIX_SYMBOLS[i] + "°C");
}

labelWithPrefixes(Units.CELSIUS, "℃");
aliasWithPrefixes(Units.CELSIUS, "°C");
// Additional cases and aliases
label(Units.PERCENT, "%");
label(Units.METRE, "m");
Expand All @@ -465,9 +457,9 @@ private DefaultFormat init() {
alias(Units.MONTH, "mon");
alias(Units.MONTH, "month");
label(Units.KILOMETRE_PER_HOUR, "km/h");
label(Units.CUBIC_METRE, "\u33A5");
alias(Units.SQUARE_METRE, "m2");
alias(Units.CUBIC_METRE, "m3");
aliasWithPrefixes(Units.SQUARE_METRE, "m2");
labelWithPrefixes(Units.CUBIC_METRE, "\u33A5");
aliasWithPrefixes(Units.CUBIC_METRE, "m3");
label(Units.NEWTON, "N");
label(Units.RADIAN, "rad");

Expand Down Expand Up @@ -506,6 +498,12 @@ public void label(Unit<?> unit, String label) {
unitToName.put(unit, label);
}
}

@Override
public void removeLabel(Unit<?> unit) {
unitToName.remove(unit);
nameToUnit.entrySet().removeIf(e -> e.getValue().equals(unit));
}

@Override
public void alias(Unit<?> unit, String alias) {
Expand All @@ -527,18 +525,38 @@ public void removeAliases(Unit<?> unit) {
nameToUnit.entrySet().removeIf(e -> e.getValue().equals(unit) && !e.getKey().equals(alias));
}

@Override
public void removeLabel(Unit<?> unit) {
unitToName.remove(unit);
nameToUnit.entrySet().removeIf(e -> e.getValue().equals(unit));
}

@Override
protected boolean isValidIdentifier(String name) {
if ((name == null) || (name.length() == 0))
return false;
return isUnitIdentifierPart(name.charAt(0));
}

/**
* Applies {@link #label(Unit, String)} for this unit and all standard prefixes.
*
* @param unit a unit
* @param label a label
*/
private void labelWithPrefixes(Unit<?> unit, String label) {
label(unit, label);
for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
label(unit.prefix(MetricPrefix.values()[i]), METRIC_PREFIX_SYMBOLS[i] + label);
}
}

/**
* Applies {@link #alias(Unit, String)} for this unit and all standard prefixes.
*
* @param unit a unit
* @param alias an alias
*/
private void aliasWithPrefixes(Unit<?> unit, String alias) {
alias(unit, alias);
for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
alias(unit.prefix(MetricPrefix.values()[i]), METRIC_PREFIX_SYMBOLS[i] + alias);
}
}

protected static boolean isUnitIdentifierPart(char ch) {
return Character.isLetter(ch)
Expand Down Expand Up @@ -1021,7 +1039,7 @@ private ASCIIFormat init() {
for (int i = 0; i < METRIC_UNITS.length; i++) {
Unit<?> si = METRIC_UNITS[i];
String symbol = (si instanceof BaseUnit) ? ((BaseUnit<?>) si).getSymbol() : ((AlternateUnit<?>) si).getSymbol();
if (isAllASCII(symbol))
if (isAllAscii(symbol))
label(si, symbol);
for (int j = 0; j < METRIC_PREFIX_SYMBOLS.length; j++) {
Unit<?> u = si.prefix(MetricPrefix.values()[j]);
Expand All @@ -1048,18 +1066,12 @@ private ASCIIFormat init() {
}
}

// Alias and ASCIIFormat for Ohm
label(Units.OHM, "Ohm");
for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
label(Units.OHM.prefix(MetricPrefix.values()[i]), asciiPrefix(METRIC_PREFIX_SYMBOLS[i]) + "Ohm");
}
// ASCIIFormat for Ohm
labelWithAsciiPrefixes(Units.OHM, "Ohm");

// Special case for DEGREE_CELSIUS.
label(Units.CELSIUS, "Celsius");
for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
label(Units.CELSIUS.prefix(MetricPrefix.values()[i]), asciiPrefix(METRIC_PREFIX_SYMBOLS[i]) + "Celsius");
}
alias(Units.CELSIUS, "Cel");
labelWithAsciiPrefixes(Units.CELSIUS, "Celsius");
aliasWithAsciiPrefixes(Units.CELSIUS, "Cel");

label(Units.METRE, "m");
label(Units.SECOND, "s");
Expand Down Expand Up @@ -1146,32 +1158,62 @@ protected boolean isValidIdentifier(String name) {
if ((name == null) || (name.length() == 0))
return false;
// label must not begin with a digit or mathematical operator
return isUnitIdentifierPart(name.charAt(0)) && isAllASCII(name);
return isUnitIdentifierPart(name.charAt(0)) && isAllAscii(name);
/*
* for (int i = 0; i < name.length(); i++) { if
* (!isAsciiCharacter(name.charAt(i))) return false; } return true;
*/
}
}

private static String asciiPrefix(String prefix) {
return "µ".equals(prefix) ? "micro" : prefix;
}

/**
* Applies {@link #alias(Unit, String)} for this unit and all standard prefixes, if the alias contains only ASCII characters.
*
* @param unit a unit
* @param alias an alias
*/
private void aliasWithAsciiPrefixes(Unit<?> unit, String alias) {
if (isValidIdentifier(alias)) {
alias(unit, alias);
for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
alias(unit.prefix(MetricPrefix.values()[i]), asciiPrefix(METRIC_PREFIX_SYMBOLS[i]) + alias);
}
}
}

/**
* Applies {@link #label(Unit, String)} for this unit and all standard prefixes, if the label contains only ASCII characters.
*
* @param unit a unit
* @param alias an label
*/
private void labelWithAsciiPrefixes(Unit<?> unit, String label) {
if (isValidIdentifier(label)) {
label(unit, label);
for (int i = 0; i < METRIC_PREFIX_SYMBOLS.length; i++) {
label(unit.prefix(MetricPrefix.values()[i]), asciiPrefix(METRIC_PREFIX_SYMBOLS[i]) + label);
}
}
}

private static String asciiPrefix(String prefix) {
return "µ".equals(prefix) ? "micro" : prefix;
}

private static String asciiSymbol(String s) {
return "Ω".equals(s) ? "Ohm" : s;
}
private static String asciiSymbol(String s) {
return "Ω".equals(s) ? "Ohm" : s;
}

/** to check if a string only contains US-ASCII_INSTANCE characters */
private static boolean isAllASCII(String input) {
boolean isASCII = true;
for (int i = 0; i < input.length(); i++) {
int c = input.charAt(i);
if (c > 0x7F) {
isASCII = false;
break;
/** to check if a string only contains US-ASCII_INSTANCE characters */
private static boolean isAllAscii(String input) {
boolean isASCII = true;
for (int i = 0; i < input.length(); i++) {
int c = input.charAt(i);
if (c > 0x7F) {
isASCII = false;
break;
}
}
return isASCII;
}
return isASCII;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,6 @@
#
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)

# SI Prefixes
javax.measure.MetricPrefix.ATTO = a
javax.measure.MetricPrefix.CENTI = c
javax.measure.MetricPrefix.DECI = d
javax.measure.MetricPrefix.DECA = da
javax.measure.MetricPrefix.EXA = E
javax.measure.MetricPrefix.FEMTO = f
javax.measure.MetricPrefix.GIGA = G
javax.measure.MetricPrefix.HECTO = h
javax.measure.MetricPrefix.KILO = k
javax.measure.MetricPrefix.MEGA = M
javax.measure.MetricPrefix.MICRO = \u00B5
javax.measure.MetricPrefix.MILLI = m
javax.measure.MetricPrefix.NANO = n
javax.measure.MetricPrefix.PETA = P
javax.measure.MetricPrefix.PICO = p
javax.measure.MetricPrefix.TERA = T
javax.measure.MetricPrefix.YOCTO = y
javax.measure.MetricPrefix.YOTTA = Y
javax.measure.MetricPrefix.ZEPTO = z
javax.measure.MetricPrefix.ZETTA = Z
javax.measure.MetricPrefix.QUETTA = Q
javax.measure.MetricPrefix.RONNA = R
javax.measure.MetricPrefix.QUECTO = q
javax.measure.MetricPrefix.RONTO = r

# Common Units
tech.units.indriya.unit.Units.KILOMETRE_PER_HOUR = km/h

Expand Down
7 changes: 7 additions & 0 deletions src/test/java/tech/units/indriya/format/EBNFPrefixTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static tech.units.indriya.unit.Units.KILOGRAM;
import static tech.units.indriya.unit.Units.LITRE;
import static tech.units.indriya.unit.Units.METRE;
import static tech.units.indriya.unit.Units.MINUTE;

import javax.measure.Quantity;
import javax.measure.Unit;
Expand Down Expand Up @@ -193,6 +194,12 @@ public void testParseQuecto() {
assertEquals(v, QUECTO(LITRE));
}


@Test
public void testQuettaMin() {
assertEquals("Qmin", format.format(QUETTA(MINUTE)));
}

@Test
public void testHashMapAccessingMap() {
assertThat(LITRE.toString(), is("l"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import javax.measure.format.UnitFormat;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -48,7 +47,7 @@
class UnitFormatRoundtripTest {

@Nested
@DisplayName("EBNFUnitFormat") @Disabled("yet too many errors")
@DisplayName("EBNFUnitFormat")
public class EBNFUnitFormatTest {

final UnitFormat format = EBNFUnitFormat.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static tech.units.indriya.unit.Units.CANDELA;
import static tech.units.indriya.unit.Units.CELSIUS;
import static tech.units.indriya.unit.Units.COULOMB;
import static tech.units.indriya.unit.Units.CUBIC_METRE;
import static tech.units.indriya.unit.Units.FARAD;
import static tech.units.indriya.unit.Units.GRAM;
import static tech.units.indriya.unit.Units.GRAY;
Expand All @@ -50,6 +51,7 @@
import static tech.units.indriya.unit.Units.LUMEN;
import static tech.units.indriya.unit.Units.LUX;
import static tech.units.indriya.unit.Units.METRE;
import static tech.units.indriya.unit.Units.MINUTE;
import static tech.units.indriya.unit.Units.MOLE;
import static tech.units.indriya.unit.Units.NEWTON;
import static tech.units.indriya.unit.Units.OHM;
Expand Down Expand Up @@ -117,9 +119,9 @@ static enum NonPrefixedUnits {
Sievert(SIEVERT),
Katal(KATAL),
//SQUARE_METRE
//CUBIC_METRE
CubicMetre(CUBIC_METRE),
//PERCENT
//MINUTE
//Minute(MINUTE),
//HOUR
//DAY
//WEEK
Expand Down Expand Up @@ -185,8 +187,7 @@ private void test(final UnitFormat format) {
assertEquals(unitLiteral, unitLiteralAfterRountrip,
()->String.format("testing '%s', unit literal diverted after roundtrip '%s' -> '%s'",
this.name(), unitLiteral, unitLiteralAfterRountrip));



}

/** whether the UnitFormat {@code format} can correctly handle the {@code prefix}-ed unit */
Expand Down

0 comments on commit 467a85e

Please sign in to comment.