private Canonical normalise(String indent, Term term) throws UcumException { Canonical result = new Canonical(new Decimal(1)); Canonical temp = normalise(indent+" ", (Term) t.getComp()); if (div) { result.divideValue(temp.getValue()); for (CanonicalUnit c : temp.getUnits()) c.setExponent(0-c.getExponent()); } else { result.multiplyValue(temp.getValue()); result.getUnits().addAll(temp.getUnits()); } else if (t.getComp() instanceof Factor) { if (div) result.divideValue(((Factor) t.getComp()).getValue()); else result.multiplyValue(((Factor) t.getComp()).getValue()); } else if (t.getComp() instanceof Symbol) { Symbol o = (Symbol) t.getComp(); Canonical temp = normalise(indent, o); if (div) { result.divideValue(temp.getValue()); for (CanonicalUnit c : temp.getUnits()) c.setExponent(0-c.getExponent()); } else { result.multiplyValue(temp.getValue()); result.getUnits().addAll(temp.getUnits()); for (int i = result.getUnits().size()-1; i >= 0; i--) {
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 Decimal convert(Decimal value, String sourceUnit, String destUnit) throws UcumException { assert value != null : paramError("convert", "value", "must not be null"); assert checkStringParam(sourceUnit) : paramError("convert", "sourceUnit", "must not be null or empty"); assert checkStringParam(destUnit) : paramError("convert", "destUnit", "must not be null or empty"); if (sourceUnit.equals(destUnit)) return value; Canonical src = new Converter(model, handlers).convert(new ExpressionParser(model).parse(sourceUnit)); Canonical dst = new Converter(model, handlers).convert(new ExpressionParser(model).parse(destUnit)); String s = new ExpressionComposer().compose(src, false); String d = new ExpressionComposer().compose(dst, false); if (!s.equals(d)) throw new UcumException("Unable to convert between units "+sourceUnit+" and "+destUnit+" as they do not have matching canonical forms ("+s+" and "+d+" respectively)"); Decimal canValue = value.multiply(src.getValue()); // System.out.println(value.toPlainString()+sourceUnit+" =("+src.getValue().toPlainString()+")= "+ // canValue.toPlainString()+s+" =("+dst.getValue().toPlainString()+")= "+ // canValue.divide(dst.getValue())+destUnit); return canValue.divide(dst.getValue()); }
private Canonical expandDefinedUnit(String indent, DefinedUnit unit) throws UcumException { String u = unit.getValue().getUnit(); if (unit.isSpecial()) { if (!handlers.exists(unit.getCode())) throw new UcumException("Not handled yet (special unit)"); else u = handlers.get(unit.getCode()).getUnits(); } Term t = new ExpressionParser(model).parse(u); debug(indent, "now handle", t); Canonical result = normalise(indent+" ", t); result.multiplyValue(unit.getValue().getValue()); return result; }
@Override public String validateInProperty(String unit, String property) { assert checkStringParam(unit) : paramError("validate", "unit", "must not be null or empty"); assert checkStringParam(property) : paramError("validateInProperty", "property", "must not be null or empty"); try { Term term = new ExpressionParser(model).parse(unit); Canonical can = new Converter(model, handlers).convert(term); String cu = new ExpressionComposer().compose(can, false); if (can.getUnits().size() == 1) { if (property.equals(can.getUnits().get(0).getBase().getProperty())) return null; else return "unit "+unit+" is of the property type "+can.getUnits().get(0).getBase().getProperty()+" ("+cu+"), not "+property+" as required."; } // defined special case if ("concentration".equals(property) && ("g/L".equals(cu) || "mol/L".equals(cu))) return null; return "unit "+unit+" has the base units "+cu+", and are not from the property "+property+" as required."; } catch (Exception e) { return e.getMessage(); } }
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 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 Canonical expandDefinedUnit(String indent, DefinedUnit unit) throws UcumException { String u = unit.getValue().getUnit(); if (unit.isSpecial()) { if (!handlers.exists(unit.getCode())) throw new UcumException("Not handled yet (special unit)"); else u = handlers.get(unit.getCode()).getUnits(); } Term t = new ExpressionParser(model).parse(u); debug(indent, "now handle", t); Canonical result = normalise(indent+" ", t); result.multiplyValue(unit.getValue().getValue()); return result; }
@Override public String validateInProperty(String unit, String property) { assert checkStringParam(unit) : paramError("validate", "unit", "must not be null or empty"); assert checkStringParam(property) : paramError("validateInProperty", "property", "must not be null or empty"); try { Term term = new ExpressionParser(model).parse(unit); Canonical can = new Converter(model, handlers).convert(term); String cu = new ExpressionComposer().compose(can, false); if (can.getUnits().size() == 1) { if (property.equals(can.getUnits().get(0).getBase().getProperty())) return null; else return "unit "+unit+" is of the property type "+can.getUnits().get(0).getBase().getProperty()+" ("+cu+"), not "+property+" as required."; } // defined special case if ("concentration".equals(property) && ("g/L".equals(cu) || "mol/L".equals(cu))) return null; return "unit "+unit+" has the base units "+cu+", and are not from the property "+property+" as required."; } catch (Exception e) { return e.getMessage(); } }
private Canonical normalise(String indent, Symbol sym) throws UcumException { Canonical result = new Canonical(new Decimal(1)); if (sym.getUnit() instanceof BaseUnit) { result.getUnits().add(new CanonicalUnit((BaseUnit) sym.getUnit(), sym.getExponent())); } else { Canonical can = expandDefinedUnit(indent, (DefinedUnit) sym.getUnit()); for (CanonicalUnit c : can.getUnits()) { c.setExponent(c.getExponent() * sym.getExponent()); } result.getUnits().addAll(can.getUnits()); if (sym.getExponent() > 0) for (int i = 0; i < sym.getExponent(); i++) result.multiplyValue(can.getValue()); else for (int i = 0; i > sym.getExponent(); i--) result.divideValue(can.getValue()); } if (sym.getPrefix() != null) { if (sym.getExponent() > 0) for (int i = 0; i < sym.getExponent(); i++) result.multiplyValue(sym.getPrefix().getValue()); else for (int i = 0; i > sym.getExponent(); i--) result.divideValue(sym.getPrefix().getValue()); } return result; }
@Override public Decimal convert(Decimal value, String sourceUnit, String destUnit) throws UcumException { assert value != null : paramError("convert", "value", "must not be null"); assert checkStringParam(sourceUnit) : paramError("convert", "sourceUnit", "must not be null or empty"); assert checkStringParam(destUnit) : paramError("convert", "destUnit", "must not be null or empty"); if (sourceUnit.equals(destUnit)) return value; Canonical src = new Converter(model, handlers).convert(new ExpressionParser(model).parse(sourceUnit)); Canonical dst = new Converter(model, handlers).convert(new ExpressionParser(model).parse(destUnit)); String s = new ExpressionComposer().compose(src, false); String d = new ExpressionComposer().compose(dst, false); if (!s.equals(d)) throw new UcumException("Unable to convert between units "+sourceUnit+" and "+destUnit+" as they do not have matching canonical forms ("+s+" and "+d+" respectively)"); Decimal canValue = value.multiply(src.getValue()); // System.out.println(value.toPlainString()+sourceUnit+" =("+src.getValue().toPlainString()+")= "+ // canValue.toPlainString()+s+" =("+dst.getValue().toPlainString()+")= "+ // canValue.divide(dst.getValue())+destUnit); return canValue.divide(dst.getValue()); }
private Canonical normalise(String indent, Symbol sym) throws UcumException { Canonical result = new Canonical(new Decimal(1)); if (sym.getUnit() instanceof BaseUnit) { result.getUnits().add(new CanonicalUnit((BaseUnit) sym.getUnit(), sym.getExponent())); } else { Canonical can = expandDefinedUnit(indent, (DefinedUnit) sym.getUnit()); for (CanonicalUnit c : can.getUnits()) { c.setExponent(c.getExponent() * sym.getExponent()); } result.getUnits().addAll(can.getUnits()); if (sym.getExponent() > 0) for (int i = 0; i < sym.getExponent(); i++) result.multiplyValue(can.getValue()); else for (int i = 0; i > sym.getExponent(); i--) result.divideValue(can.getValue()); } if (sym.getPrefix() != null) { if (sym.getExponent() > 0) for (int i = 0; i < sym.getExponent(); i++) result.multiplyValue(sym.getPrefix().getValue()); else for (int i = 0; i > sym.getExponent(); i--) result.divideValue(sym.getPrefix().getValue()); } return result; }
@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 Canonical normalise(String indent, Term term) throws UcumException { Canonical result = new Canonical(new Decimal(1)); Canonical temp = normalise(indent+" ", (Term) t.getComp()); if (div) { result.divideValue(temp.getValue()); for (CanonicalUnit c : temp.getUnits()) c.setExponent(0-c.getExponent()); } else { result.multiplyValue(temp.getValue()); result.getUnits().addAll(temp.getUnits()); } else if (t.getComp() instanceof Factor) { if (div) result.divideValue(((Factor) t.getComp()).getValue()); else result.multiplyValue(((Factor) t.getComp()).getValue()); } else if (t.getComp() instanceof Symbol) { Symbol o = (Symbol) t.getComp(); Canonical temp = normalise(indent, o); if (div) { result.divideValue(temp.getValue()); for (CanonicalUnit c : temp.getUnits()) c.setExponent(0-c.getExponent()); } else { result.multiplyValue(temp.getValue()); result.getUnits().addAll(temp.getUnits()); for (int i = result.getUnits().size()-1; i >= 0; i--) {