Commit 1ccf1dc2 authored by mey's avatar mey
Browse files

AmountUtil.java: added SimpleAmountFormat

    to replace previous format which displays a wrong value
        when using with amounts around 1.0
parent 17e2a4cc
...@@ -3,11 +3,12 @@ package de.zmt.util; ...@@ -3,11 +3,12 @@ package de.zmt.util;
import static javax.measure.unit.NonSI.*; import static javax.measure.unit.NonSI.*;
import static javax.measure.unit.SI.*; import static javax.measure.unit.SI.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.measure.quantity.*; import javax.measure.quantity.*;
import javax.measure.unit.Unit; import javax.measure.unit.*;
import javax.xml.bind.annotation.adapters.XmlAdapter; import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.jscience.physics.amount.*; import org.jscience.physics.amount.*;
...@@ -22,8 +23,9 @@ public abstract class AmountUtil { ...@@ -22,8 +23,9 @@ public abstract class AmountUtil {
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static final Logger logger = Logger.getLogger(AmountUtil.class.getName()); private static final Logger logger = Logger.getLogger(AmountUtil.class.getName());
public static final AmountFormat FORMAT = AmountFormat.getExactDigitsInstance(); /** {@link AmountFormat} for display in MASON GUI. */
private static final AmountFormat FORMAT_IN = AmountFormat.getPlusMinusErrorInstance(2); public static final AmountFormat FORMAT = new SimpleAmountFormat();
private static final AmountFormat PARSE_FORMAT = AmountFormat.getPlusMinusErrorInstance(2);
/** /**
* *
...@@ -150,7 +152,7 @@ public abstract class AmountUtil { ...@@ -150,7 +152,7 @@ public abstract class AmountUtil {
*/ */
public static <Q extends Quantity> Amount<Q> parseAmount(CharSequence amountCsq, Unit<Q> unit) { public static <Q extends Quantity> Amount<Q> parseAmount(CharSequence amountCsq, Unit<Q> unit) {
try { try {
return FORMAT_IN.parse(amountCsq).to(unit); return FORMAT.parse(amountCsq).to(unit);
} catch (StringIndexOutOfBoundsException e) { } catch (StringIndexOutOfBoundsException e) {
logger.warning("No unit given. Using default."); logger.warning("No unit given. Using default.");
String amountString = amountCsq.toString(); String amountString = amountCsq.toString();
...@@ -194,7 +196,7 @@ public abstract class AmountUtil { ...@@ -194,7 +196,7 @@ public abstract class AmountUtil {
@Override @Override
public Amount<?> unmarshal(String v) throws Exception { public Amount<?> unmarshal(String v) throws Exception {
return FORMAT_IN.parse(v); return FORMAT.parse(v);
} }
@Override @Override
...@@ -202,4 +204,24 @@ public abstract class AmountUtil { ...@@ -202,4 +204,24 @@ public abstract class AmountUtil {
return FORMAT.format(v).toString(); return FORMAT.format(v).toString();
} }
} }
private static class SimpleAmountFormat extends AmountFormat {
@Override
public Appendable format(Amount<?> obj, Appendable dest) throws IOException {
if (obj.isExact()) {
dest.append(String.valueOf(obj.getExactValue()));
} else {
dest.append(String.valueOf(obj.getEstimatedValue()));
}
dest.append(" ");
return UnitFormat.getInstance().format(obj.getUnit(), dest);
}
@Override
public Amount<?> parse(CharSequence csq, javolution.text.TextFormat.Cursor cursor) {
return PARSE_FORMAT.parse(csq, cursor);
}
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment