use of javax.measure.format.MeasurementParseException in project unit-api by unitsofmeasurement.
the class DefaultTestQuantityFormat method parse.
@SuppressWarnings("unchecked")
@Override
public Quantity<?> parse(CharSequence csq, int index) throws MeasurementParseException {
// cursor.getIndex();
int startDecimal = index;
while ((startDecimal < csq.length()) && Character.isWhitespace(csq.charAt(startDecimal))) {
startDecimal++;
}
int endDecimal = startDecimal + 1;
while ((endDecimal < csq.length()) && !Character.isWhitespace(csq.charAt(endDecimal))) {
endDecimal++;
}
try {
final Double decimal = new Double(csq.subSequence(startDecimal, endDecimal).toString());
final String uStr = csq.subSequence(endDecimal + 1, csq.length()).toString();
Unit unit = SimpleTestUnitFormat.getInstance().parse(uStr);
return TestQuantities.getQuantity(decimal, unit);
} catch (NumberFormatException nfe) {
throw new MeasurementParseException(nfe);
}
}
use of javax.measure.format.MeasurementParseException in project indriya by unitsofmeasurement.
the class EBNFUnitFormat method parse.
@Override
protected Unit<? extends Quantity<?>> parse(CharSequence csq, ParsePosition cursor) throws MeasurementParseException {
// Parsing reads the whole character sequence from the parse position.
int start = cursor != null ? cursor.getIndex() : 0;
int end = csq.length();
if (end <= start) {
return AbstractUnit.ONE;
}
String source = csq.subSequence(start, end).toString().trim();
if (source.length() == 0) {
return AbstractUnit.ONE;
}
try {
UnitFormatParser parser = new UnitFormatParser(symbolMap, new StringReader(source));
Unit<?> result = parser.parseUnit();
if (cursor != null)
cursor.setIndex(end);
return result;
} catch (TokenException e) {
if (e.currentToken != null) {
cursor.setErrorIndex(start + e.currentToken.endColumn);
} else {
cursor.setErrorIndex(start);
}
throw new MeasurementParseException(e);
} catch (TokenMgrError e) {
cursor.setErrorIndex(start);
throw new IllegalArgumentException(e.getMessage());
}
}
use of javax.measure.format.MeasurementParseException in project n2a by frothga.
the class ImportJob method unit.
@SuppressWarnings({ "rawtypes", "unchecked" })
public void unit(Node node) {
String symbol = getAttribute(node, "symbol");
String dimension = getAttribute(node, "dimension");
int power = getAttribute(node, "power", 0);
double scale = getAttribute(node, "scale", 1.0);
double offset = getAttribute(node, "offset", 0.0);
Unit unit = dimensions.get(dimension);
if (unit == null)
unit = nmlDimensions.get(dimension);
// fall back, but in general something is broken about the file
if (unit == null)
unit = AbstractUnit.ONE;
if (power > 0)
unit = unit.transform(MultiplyConverter.ofRational(BigInteger.TEN.pow(power), BigInteger.ONE));
else if (power < 0)
unit = unit.transform(MultiplyConverter.ofRational(BigInteger.ONE, BigInteger.TEN.pow(-power)));
else {
if (scale == 1.0) {
unit = unit.shift(offset);
} else {
// UCUM only allows rational numbers, so convert scale
MultiplyConverter ratio = null;
if (scale < 1.0) {
// Attempt to find a simple ratio of 1/integer
double inverse = 1.0 / scale;
long integer = Math.round(inverse);
if (Math.abs(inverse - integer) < epsilon)
ratio = MultiplyConverter.ofRational(1, integer);
}
if (ratio == null) {
String s = getAttribute(node, "scale").toLowerCase();
String[] pieces = s.split("e");
int shift = 0;
if (pieces.length > 1)
shift = Integer.valueOf(pieces[1]);
pieces = pieces[0].split(".");
if (pieces.length > 1) {
shift -= pieces[1].length();
s = pieces[0] + pieces[1];
}
BigInteger numerator = new BigInteger(s);
BigInteger denominator = new BigDecimal(10).pow(shift).toBigInteger();
ratio = MultiplyConverter.ofRational(numerator, denominator);
}
unit = unit.transform(ratio).shift(offset);
}
}
// Since LEMS and NeuroML tend to follow certain naming practices, we may be able to retrieve
// a more parsimonious unit based on direct name translation.
String tempName = symbol;
tempName = tempName.replace("_per_", "/");
tempName = tempName.replace("per_", "/");
tempName = tempName.replace("_", ".");
tempName = tempName.replace("ohm", "Ohm");
tempName = tempName.replace("hour", "h");
Unit temp = null;
try {
temp = UCUM.parse(tempName);
} catch (MeasurementParseException | TokenException e) {
}
if (// found a unit with matching dimension ...
temp != null && temp.isCompatible(unit)) {
Number tempScale = temp.getConverterTo(temp.getSystemUnit()).convert(Integer.valueOf(1));
Number unitScale = temp.getConverterTo(unit.getSystemUnit()).convert(Integer.valueOf(1));
if (// ... and matching scale ...
tempScale.equals(unitScale)) {
int unitLength = UCUM.format(unit).length();
int tempLength = UCUM.format(temp).length();
if (// ... and at least as parsimonious
tempLength <= unitLength) {
unit = temp;
// Update dimension if this is directly equivalent, but strictly more parsimonious
if (power == 0 && scale == 1 && offset == 0 && tempLength < unitLength) {
dimensions.put(dimension, temp);
}
}
}
}
ExpressionParser.namedUnits.put(symbol, unit);
}
Aggregations