How to parse inch from String

26 views
Skip to first unread message

paran...@gmail.com

unread,
Apr 29, 2018, 1:01:16 PM4/29/18
to Units Users
Hi guys,

this may seem like a trivial question, but I have been stuck with this for the last 4 days so any help is appreciated. I'm building a service which would parse String into Units. So I pass "in" to the service and I want to get back INCH unit.

Unfortunately this doesn't work:

SimpleUnitFormat.getInstance().parse(str);

nor this:


ServiceProvider.current().getUnitFormatService().getUnitFormat().parse(str);

they both throw the same error:


Caused by: javax.measure.format.ParserException: Parse Error
    at tec.uom.se.format.SimpleUnitFormat$DefaultFormat.check(SimpleUnitFormat.java:579)
    at tec.uom.se.format.SimpleUnitFormat$DefaultFormat.parseSingleUnit(SimpleUnitFormat.java:432)
    at tec.uom.se.format.SimpleUnitFormat$DefaultFormat.parseProductUnit(SimpleUnitFormat.java:443)
    at tec.uom.se.format.SimpleUnitFormat.parseObject(SimpleUnitFormat.java:269)
    at tec.uom.se.format.SimpleUnitFormat$DefaultFormat.parse(SimpleUnitFormat.java:787)
    at tec.uom.se.format.SimpleUnitFormat$DefaultFormat.parse(SimpleUnitFormat.java:782)

I have these dependencies in my classpath:

<!-- Java Units of Measurement -->
<dependency>
<groupId>javax.measure</groupId>
<artifactId>unit-api</artifactId>
</dependency>
<!--dependency>
<groupId>tech.units</groupId>
<artifactId>indriya</artifactId>
</dependency-->
<dependency>
<groupId>tec.uom</groupId>
<artifactId>uom-se</artifactId>
</dependency>
<dependency>
<groupId>si.uom</groupId>
<artifactId>si-units-java8</artifactId>
</dependency>
<dependency>
<groupId>systems.uom</groupId>
<artifactId>systems-common-java8</artifactId>
</dependency>
<dependency>
<groupId>systems.uom</groupId>
<artifactId>systems-unicode-java8</artifactId>
</dependency>
<dependency>
<groupId>systems.uom</groupId>
<artifactId>systems-quantity</artifactId>
</dependency>

I also tried defining my own

public class PlatformUomServiceProvider extends DefaultServiceProvider {

and
public class PlatformSystemOfUnitsService implements SystemOfUnitsService, IntPrioritySupplier {

which registers my custom units:
public class NemesisUnits extends AbstractSystemOfUnits implements Nameable {

where I have defined the INCH unit:

public static final Unit<Length> INCH = addUnit(new TransformedUnit<>("in", MILLIMETRE, MILLIMETRE, new MultiplyConverter(25.4)));

I have also registered my PlatformUomServiceProvider in META-INF/service/javax.measure.spi.ServiceProvider

Any help would be greatly appreaciated as the documentation is very scarce.

Reply all
Reply to author
Forward
0 new messages