Skip to content

Commit

Permalink
[GEOT-7288] upgrade javax.measure version to 2.1.3 (#4134)
Browse files Browse the repository at this point in the history
* upgrade javax.measure version to latest
  • Loading branch information
AlbanSeurat committed Dec 31, 2022
1 parent 29394a1 commit 3905824
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
*/
package org.geotools.measure;

import java.io.IOException;
import java.text.ParsePosition;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.measure.Quantity;
import javax.measure.Unit;
import org.geotools.measure.SimpleUnitFormatForwarder.DefaultFormatForwarder;
import javax.measure.format.MeasurementParseException;
import tech.units.indriya.format.SimpleUnitFormat;

/**
* This class encapsulates the required machinations to initialize the unit formatter implementation
Expand All @@ -30,7 +35,17 @@
* <p>GeoTools' unit formatters should be implemented by calling the provided constructors.
*/
// This class should be final, currently only prevented by the Wkt unit formatter subclass.
public class BaseUnitFormatter extends DefaultFormatForwarder {
public class BaseUnitFormatter extends SimpleUnitFormat implements UnitFormatter {

private static final char MIDDLE_DOT = '\u00b7';
private static final char EXPONENT_ONE = '\u00b9';
private static final char EXPONENT_TWO = '\u00b2';
private static final char EXPONENT_THREE = '\u00b3';

private SimpleUnitFormat delegateFormatter = SimpleUnitFormat.getNewInstance();

@Deprecated private Map<String, Unit<?>> nameToUnit = new HashMap<>();
@Deprecated private Map<Unit<?>, String> unitToName = new HashMap<>();

/**
* Create a new {@code BaseUnitFormatter} instance, initialized with provided the unit
Expand Down Expand Up @@ -78,6 +93,7 @@ public BaseUnitFormatter(List<UnitDefinition> unitDefinitions) {
* @return an immutable map that shows the units (associated with their symbols) that this unit
* formatter contains
*/
@Deprecated
public Map<Unit<?>, String> getUnitToSymbolMap() {
return Collections.unmodifiableMap(this.unitToName);
}
Expand All @@ -86,20 +102,17 @@ public Map<Unit<?>, String> getUnitToSymbolMap() {
* @return an immutable map that shows the symbols (associated with their units) that this unit
* formatter contains
*/
@Deprecated
public Map<String, Unit<?>> getSymbolToUnitMap() {
return Collections.unmodifiableMap(this.nameToUnit);
}

@Override
public void label(Unit<?> unit, String label) {
super.label(unit, label);
addUnit(unit);
}

/** Defaults to being a no-op, subclasses can override */
protected void addUnit(Unit<?> unit) {}

private void addUnit(Unit<?> unit, String unitSymbol, PrefixDefinition prefix) {
this.nameToUnit.put(unitSymbol, unit);
this.unitToName.put(unit, unitSymbol);
Unit<?> prefixedUnit = unit.prefix(prefix.getPrefix());
String prefixString = prefix.getPrefix().getSymbol();
String prefixedSymbol = prefixString + unitSymbol;
Expand All @@ -110,6 +123,8 @@ private void addUnit(Unit<?> unit, String unitSymbol, PrefixDefinition prefix) {
}

private void addAlias(Unit<?> unit, String unitSymbol, PrefixDefinition prefix) {
this.nameToUnit.put(unitSymbol, unit);
this.unitToName.put(unit, unitSymbol);
Unit<?> prefixedUnit = unit.prefix(prefix.getPrefix());
String prefixString = prefix.getPrefix().getSymbol();
String prefixedSymbol = prefixString + unitSymbol;
Expand All @@ -118,4 +133,76 @@ private void addAlias(Unit<?> unit, String unitSymbol, PrefixDefinition prefix)
this.alias(prefixedUnit, prefixAlias + unitSymbol);
}
}

@Override
public Appendable format(Unit<?> unit, Appendable appendable) throws IOException {
return delegateFormatter.format(unit, appendable);
}

@Override
public Unit<? extends Quantity> parseProductUnit(CharSequence csq, ParsePosition pos)
throws MeasurementParseException {
return delegateFormatter.parseProductUnit(csq, pos);
}

@Override
public Unit<? extends Quantity> parseSingleUnit(CharSequence csq, ParsePosition pos)
throws MeasurementParseException {
return delegateFormatter.parseSingleUnit(csq, pos);
}

@Override
public void label(Unit<?> unit, String label) {
nameToUnit.put(label, unit);
unitToName.put(unit, label);
delegateFormatter.label(unit, label);
addUnit(unit);
}

@Override
public Unit<?> parse(CharSequence csq, ParsePosition cursor) throws IllegalArgumentException {
return delegateFormatter.parse(csq, cursor);
}

@Override
public Unit<?> parse(CharSequence csq) throws MeasurementParseException {
return delegateFormatter.parse(csq);
}

@Override
protected Unit<?> parse(CharSequence csq, int index) throws IllegalArgumentException {
return parse(csq, new ParsePosition(index));
}

@Override
public void alias(Unit<?> unit, String alias) {
nameToUnit.put(alias, unit);
delegateFormatter.alias(unit, alias);
}

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

protected static boolean isUnitIdentifierPart(char ch) {
return Character.isLetter(ch)
|| (!Character.isWhitespace(ch)
&& !Character.isDigit(ch)
&& (ch != MIDDLE_DOT)
&& (ch != '*')
&& (ch != '/')
&& (ch != '(')
&& (ch != ')')
&& (ch != '[')
&& (ch != ']')
&& (ch != EXPONENT_ONE)
&& (ch != EXPONENT_TWO)
&& (ch != EXPONENT_THREE)
&& (ch != '^')
&& (ch != '+')
&& (ch != '-'));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
package org.geotools.measure;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static javax.measure.MetricPrefix.MICRO;
import static org.junit.Assert.assertEquals;
import static si.uom.SI.ASTRONOMICAL_UNIT;
import static systems.uom.common.USCustomary.ELECTRICAL_HORSEPOWER;
import static systems.uom.common.USCustomary.HORSEPOWER;
import static systems.uom.common.USCustomary.REVOLUTION_PER_MINUTE;
import static tech.units.indriya.unit.Units.CUBIC_METRE;
import static tech.units.indriya.unit.Units.MONTH;
import static tech.units.indriya.unit.Units.SQUARE_METRE;

import java.lang.reflect.Field;
import java.util.AbstractMap;
Expand All @@ -34,6 +40,7 @@
import javax.measure.Unit;
import javax.measure.format.UnitFormat;
import org.junit.Test;
import si.uom.SI;
import tech.units.indriya.format.SimpleUnitFormat;
import tech.units.indriya.unit.Units;

Expand All @@ -45,19 +52,27 @@ public void unitsOnlyInOld() throws Exception {

List<Map.Entry<Unit<?>, String>> unitToName =
toSortedList1(getUnitToNameMap(simpleUnitFormat));
@SuppressWarnings("deprecation")
List<Map.Entry<Unit<?>, String>> unitToSymbol =
toSortedList1(formatter.getUnitToSymbolMap());

List<Map.Entry<Unit<?>, String>> unitsOnlyInOld =
unitToName.stream()
.filter(entry -> !unitToSymbol.contains(entry))
.collect(Collectors.toList());
// only one kind of µ is added for those special-cased units in indriya:
List<Map.Entry<Unit<?>, String>> indriyaBug =
asList(
entry(ASTRONOMICAL_UNIT, "UA"),
entry(HORSEPOWER, "hp"),
entry(ELECTRICAL_HORSEPOWER, "hp(E)"),
entry(Units.MONTH, "mo"),
entry(SI.REVOLUTION, "rev"),
entry(REVOLUTION_PER_MINUTE, "rpm"),
entry(Units.GRAM.prefix(MICRO), "µg"),
entry(Units.LITRE.prefix(MICRO), "µl"),
entry(Units.CELSIUS.prefix(MICRO), "µ℃"));
entry(Units.CELSIUS.prefix(MICRO), "µ℃"),
entry(Units.OHM, "Ω"),
entry(Units.CELSIUS, "℃"));
List<Map.Entry<Unit<?>, String>> unitsOnlyInOldWithoutBugs =
new ArrayList<>(unitsOnlyInOld);
unitsOnlyInOldWithoutBugs.removeAll(indriyaBug);
Expand All @@ -76,6 +91,7 @@ public void unitsOnlyInNew() throws Exception {

List<Map.Entry<Unit<?>, String>> unitToNameMap =
toSortedList1(getUnitToNameMap(simpleUnitFormat));
@SuppressWarnings("deprecation")
List<Map.Entry<Unit<?>, String>> unitToSymbol =
toSortedList1(formatter.getUnitToSymbolMap());

Expand All @@ -86,9 +102,12 @@ public void unitsOnlyInNew() throws Exception {
// only one kind of µ is added for those special-cased units in indriya:
List<Map.Entry<? extends Unit<?>, String>> indriyaBug =
asList(
entry(HORSEPOWER, "HP"),
entry(Units.GRAM.prefix(MICRO), "μg"),
entry(Units.LITRE.prefix(MICRO), "μl"),
entry(Units.CELSIUS.prefix(MICRO), "μ℃"));
entry(Units.CELSIUS.prefix(MICRO), "μ℃"),
entry(Units.OHM, "Ohm"),
entry(Units.CELSIUS, "°C"));
List<Map.Entry<Unit<?>, String>> unitsOnlyInNewWithoutBugs =
new ArrayList<>(unitsOnlyInNew);
unitsOnlyInNewWithoutBugs.removeAll(indriyaBug);
Expand All @@ -107,16 +126,30 @@ public void namesOnlyInOld() throws Exception {

List<Map.Entry<String, Unit<?>>> nameToUnitMap =
toSortedList2(getNameToUnitMap(simpleUnitFormat));
@SuppressWarnings("deprecation")
List<Map.Entry<String, Unit<?>>> symbolToUnit =
toSortedList2(formatter.getSymbolToUnitMap());

List<Map.Entry<String, Unit<?>>> unitsOnlyInOld =
nameToUnitMap.stream()
.filter(entry -> !symbolToUnit.contains(entry))
.collect(Collectors.toList());
List<Map.Entry<String, Unit<?>>> indriyaBug =
asList(
entry("hp", HORSEPOWER),
entry("hp(E)", ELECTRICAL_HORSEPOWER),
entry("month", MONTH),
entry("mo", MONTH),
entry("mon", MONTH),
entry("m2", SQUARE_METRE),
entry("rpm", REVOLUTION_PER_MINUTE),
entry("m3", CUBIC_METRE));
List<Map.Entry<String, Unit<?>>> unitsOnlyInNewWithoutBug = new ArrayList<>(unitsOnlyInOld);
unitsOnlyInNewWithoutBug.removeAll(indriyaBug);

assertEquals(
unitsOnlyInOld.size() + " names only in old: " + unitsOnlyInOld + "\n",
emptyList(),
indriyaBug,
unitsOnlyInOld);
}

Expand All @@ -126,6 +159,7 @@ public void namesOnlyInNew() throws Exception {

List<Map.Entry<String, Unit<?>>> nameToUnitMap =
toSortedList2(getNameToUnitMap(simpleUnitFormat));
@SuppressWarnings("deprecation")
List<Map.Entry<String, Unit<?>>> symbolToUnit =
toSortedList2(formatter.getSymbolToUnitMap());

Expand All @@ -136,6 +170,7 @@ public void namesOnlyInNew() throws Exception {
// only one kind of µ is added for those special-cased units in indriya:
List<Map.Entry<String, Unit<?>>> indriyaBug =
asList(
entry("HP", HORSEPOWER),
entry("μg", Units.GRAM.prefix(MICRO)),
entry("μl", Units.LITRE.prefix(MICRO)),
entry("μ°C", Units.CELSIUS.prefix(MICRO)),
Expand Down
5 changes: 5 additions & 0 deletions modules/library/opengis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@
<artifactId>systems-common</artifactId>
</dependency>

<dependency>
<groupId>tech.units</groupId>
<artifactId>indriya</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
<!-- Required for ImageParameterDescriptor test. -->
Expand Down
4 changes: 4 additions & 0 deletions modules/ogc/net.opengis.wps/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<groupId>systems.uom</groupId>
<artifactId>systems-common</artifactId>
</dependency>
<dependency>
<groupId>tech.units</groupId>
<artifactId>indriya</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void reset() {
public void testDU() throws Exception {
// could not find a way to make this work with the above test, so doing it another way
Unit<?> unit = NetCDFUnitFormat.parse("DU");
assertEquals("(1/m²)*446.2·μmol", unit.toString());
assertEquals("μmol·(1/m²)*446.2", unit.toString());
}

@Test(expected = MeasurementParseException.class)
Expand Down
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -557,17 +557,23 @@
<dependency>
<groupId>systems.uom</groupId>
<artifactId>systems-common</artifactId>
<version>2.0.2</version>
<version>2.1</version>
<exclusions>
<exclusion>
<artifactId>indriya</artifactId>
<groupId>tech.units</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>tech.units</groupId>
<artifactId>indriya</artifactId>
<version>2.0.2</version>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>javax.measure</groupId>
<artifactId>unit-api</artifactId>
<version>2.0</version>
<version>2.1.3</version>
</dependency>

<!-- Java Advanced Imaging (JAI) -->
Expand Down

0 comments on commit 3905824

Please sign in to comment.