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 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(); } }
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) 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--) { CanonicalUnit sf = result.getUnits().get(i); for (int j = i-1; j >=0; j--) { CanonicalUnit st = result.getUnits().get(j); if (st.getBase() == sf.getBase()) { st.setExponent(sf.getExponent()+st.getExponent()); result.getUnits().remove(i); break;
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; }
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 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(); } }
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) 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--) { CanonicalUnit sf = result.getUnits().get(i); for (int j = i-1; j >=0; j--) { CanonicalUnit st = result.getUnits().get(j); if (st.getBase() == sf.getBase()) { st.setExponent(sf.getExponent()+st.getExponent()); result.getUnits().remove(i); break;
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; }