@Override public Pair getCanonicalForm(Pair value) throws UcumException { assert value != null : paramError("getCanonicalForm", "value", "must not be null"); assert checkStringParam(value.getCode()) : paramError("getCanonicalForm", "value.code", "must not be null or empty"); Term term = new ExpressionParser(model).parse(value.getCode()); Canonical c = new Converter(model, handlers).convert(term); if (value.getValue() == null) return new Pair(null, new ExpressionComposer().compose(c, false)); else return new Pair(value.getValue().multiply(c.getValue()), new ExpressionComposer().compose(c, false)); }
private void checkCodes() { for (Unit unit : model.getBaseUnits()) { checkUnitCode(unit.getCode(), true); } for (Unit unit : model.getDefinedUnits()) { checkUnitCode(unit.getCode(), true); } }
public String compose(Canonical can, boolean value) { StringBuilder b = new StringBuilder(); if (value) b.append(can.getValue().asDecimal()); boolean first = true; for (CanonicalUnit c : can.getUnits()) { if (first) first = false; else b.append("."); b.append(c.getBase().getCode()); if (c.getExponent() != 1) b.append(c.getExponent()); } return b.toString(); }
@Override public String getCanonicalUnits(String unit) throws UcumException { assert checkStringParam(unit) : paramError("getCanonicalUnits", "unit", "must not be null or empty"); try { Term term = new ExpressionParser(model).parse(unit); return new ExpressionComposer().compose(new Converter(model, handlers).convert(term), false); } catch (Exception e) { throw new UcumException("Error processing "+unit+": "+e.getMessage(), e); } }
private void composeTerm(StringBuilder bldr, Term term) { if (term.getComp() != null) composeComp(bldr, term.getComp()); if (term.getOp() != null) composeOp(bldr, term.getOp()); if (term.getTerm() != null) { composeTerm(bldr, term.getTerm()); } }
private void composeTerm(StringBuilder bldr, Term term) { if (term.getComp() != null) composeComp(bldr, term.getComp()); if (term.getOp() != null) composeOp(bldr, term.getOp()); if (term.getTerm() != null) { // bldr.append('('); composeTerm(bldr, term.getTerm()); // bldr.append('}'); } }
private boolean matchesConcept(Concept concept, String text, boolean isRegex) { for (String name : concept.getNames()) { if (matches(name, text, isRegex)) return true; } if (matches(concept.getCode(), text, isRegex)) return true; if (matches(concept.getCodeUC(), text, isRegex)) return true; if (matches(concept.getPrintSymbol(), text, isRegex)) return true; return false; }
public static Decimal one() { try { return new Decimal("1"); } catch (Exception e) { return null; // won't happen } }
private void composeSymbol(StringBuilder bldr, Symbol symbol) { if (symbol.getPrefix() != null) { bldr.append(symbol.getPrefix().getCode()); } bldr.append(symbol.getUnit().getCode()); if (symbol.getExponent() != 1) { bldr.append(symbol.getExponent()); } }
@Override public List<Concept> search(ConceptKind kind, String text, boolean isRegex) { assert checkStringParam(text) : paramError("search", "text", "must not be null or empty"); return new Search().doSearch(model, kind, text, isRegex); }
private boolean matchesUnit(Unit unit, String text, boolean isRegex) { return matches(unit.getProperty(), text, isRegex) || matchesConcept(unit, text, isRegex); }
@Override public UcumVersionDetails ucumIdentification() { return new UcumVersionDetails(model.getRevisionDate(), model.getVersion()); }
@Override public String getDescription() { return super.getDescription()+" = "+value.toString(); }
@Override public String getDescription() { return super.getDescription()+" = "+value.getDescription(); }
public Decimal add(Decimal other) { if (other == null) return null; if (negative == other.negative) { Decimal result = doAdd(other); result.negative = negative; return result; } else if (negative) return other.doSubtract(this); else return doSubtract(other); }
public String getDescription() { if (value == null) return unit; return value.toString()+unit; }
private Decimal absolute() { Decimal d = copy(); d.negative = false; return d; }