@Override public String validate(String unit) { assert unit != null : paramError("validate", "unit", "must not be null"); try { new ExpressionParser(model).parse(unit); return null; } catch (Exception e) { return e.getMessage(); } }
private void checkUnitCode(String code, boolean primary) { try { Term term = new ExpressionParser(model).parse(code); String c = new ExpressionComposer().compose(term); if (!c.equals(code))
@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(); } }
@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); } }
@Override public String validateCanonicalUnits(String unit, String canonical) { assert checkStringParam(unit) : paramError("validate", "unit", "must not be null or empty"); assert checkStringParam(canonical) : paramError("validateCanonicalUnits", "canonical", "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 (!canonical.equals(cu)) return "unit "+unit+" has the base units "+cu+", not "+canonical+" as required."; return null; } catch (Exception e) { return e.getMessage(); } }
/** * given a unit, return a formal description of what the units stand for using * full names * @param units the unit code * @return formal description * @throws UcumException * @ */ @Override public String analyse(String unit) throws UcumException { if (Utilities.noString(unit)) return "(unity)"; assert checkStringParam(unit) : paramError("analyse", "unit", "must not be null or empty"); Term term = new ExpressionParser(model).parse(unit); return new FormalStructureComposer().compose(term); }
@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 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)); }
@Override public String validate(String unit) { assert unit != null : paramError("validate", "unit", "must not be null"); try { new ExpressionParser(model).parse(unit); return null; } catch (Exception e) { return e.getMessage(); } }
private void checkUnitCode(String code, boolean primary) { try { Term term = new ExpressionParser(model).parse(code); String c = new ExpressionComposer().compose(term); if (!c.equals(code))
@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(); } }
@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); } }
@Override public String validateCanonicalUnits(String unit, String canonical) { assert checkStringParam(unit) : paramError("validate", "unit", "must not be null or empty"); assert checkStringParam(canonical) : paramError("validateCanonicalUnits", "canonical", "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 (!canonical.equals(cu)) return "unit "+unit+" has the base units "+cu+", not "+canonical+" as required."; return null; } catch (Exception e) { return e.getMessage(); } }
/** * given a unit, return a formal description of what the units stand for using * full names * @param units the unit code * @return formal description * @throws UcumException * @ */ @Override public String analyse(String unit) throws UcumException { if (Utilities.noString(unit)) return "(unity)"; assert checkStringParam(unit) : paramError("analyse", "unit", "must not be null or empty"); Term term = new ExpressionParser(model).parse(unit); return new FormalStructureComposer().compose(term); }
@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 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)); }