return "Wb"; default: // AVOGADRO, DIMENSIONLESS, ITEM, INVALID: return toString().toLowerCase();
sb.append(unit.printUnit()); } else { sb.append(unit.getKind().getName().toLowerCase()); sb.append(MessageFormat.format( " (exponent = {0}, multiplier = {1}, scale = {2})",
/** * * @param u * @return */ @SuppressWarnings("deprecation") public static String toHTML(Unit u) { StringBuffer times = new StringBuffer(); if (u.getMultiplier() != 0) { if (u.getMultiplier() != 1) { times.append(StringTools.toString(u.getMultiplier())); } StringBuffer pow = new StringBuffer(); pow.append(u.getKind().getSymbol()); String prefix = u.getPrefix(); if (prefix.length() > 0 && !prefix.startsWith("10")) { pow.insert(0, prefix); } else if (u.getScale() != 0) { pow.insert(0, ' '); pow = HTMLFormula.times(HTMLFormula.pow(Integer.valueOf(10), u .getScale()), pow); } times = HTMLFormula.times(times, pow); } if (u.getOffset() != 0) { times = HTMLFormula.sum(StringTools.toString(u.getOffset()), times); } return HTMLFormula.pow(times, StringTools.toString(u.getExponent())) .toString(); }
Kind k1 = unit1.getKind(); Kind k2 = unit2.getKind(); boolean equivalent = Kind.areEquivalent(k1, k2); if (equivalent || unit1.isDimensionless() || unit2.isDimensionless()) { int s1 = unit1.getScale(), s2 = unit2.getScale(); unit1.setExponent(newExponent); } else if (k1.equals(Kind.INVALID) || k2.equals(Kind.INVALID)) { if (!k2.equals(Kind.INVALID)) { if (unit2.isSetOffset) { unit1.setOffset(unit2.getOffset());
Kind kind = null; try { kind = Kind.valueOf(id.toUpperCase()); } catch (IllegalArgumentException exc) { logger.warn(MessageFormat.format( String resource = u.getKind().getUnitOntologyResource(); if (resource != null) {
if (Unit.Kind.areEquivalent(u.getKind(), s.getKind()) || u.isDimensionless() || s.isDimensionless() || u.isInvalid() || s.isInvalid()) { && (getUnit(0).getKind().equals(Kind.DIMENSIONLESS))) { u = removeUnit(0); Unit.merge(getUnit(0), u); if (!Unit.Kind.areEquivalent(u.getKind(), s.getKind()) && !u.isDimensionless() && !s.isDimensionless() && !u.isInvalid() && !s.isInvalid()) {
for (int i = getUnitCount() - 1; (i >= 0) && !contains; i--) { Unit u = getUnit(i); if (Unit.Kind.areEquivalent(u.getKind(), unit.getKind()) || u.isDimensionless() || unit.isDimensionless() || u.isInvalid() || unit.isInvalid()) {
attributes.put("kind", getKind().toString().toLowerCase());
for (int i = getUnitCount() - 1; (i >= 0) && !contains; i--) { Unit u = getUnit(i); if (Unit.Kind.areEquivalent(u.getKind(), unit.getKind()) || u.isDimensionless() || unit.isDimensionless() || u.isInvalid() || unit.isInvalid()) {
/** * * @param u * @return */ public static String toHTML(Unit u) { StringBuffer times = new StringBuffer(); if (u.getMultiplier() != 0) { if (u.getMultiplier() != 1) { times.append(StringTools.toString(u.getMultiplier())); } StringBuffer pow = new StringBuffer(); pow.append(u.getKind().getSymbol()); String prefix = u.getPrefix(); if (prefix.length() > 0 && !prefix.startsWith("10")) { pow.insert(0, prefix); } else if (u.getScale() != 0) { pow.insert(0, ' '); pow = HTMLFormula.times(HTMLFormula.pow(Integer.valueOf(10), u .getScale()), pow); } times = HTMLFormula.times(times, pow); } if (u.getOffset() != 0) { times = HTMLFormula.sum(StringTools.toString(u.getOffset()), times); } return HTMLFormula.pow(times, StringTools.toString(u.getExponent())) .toString(); }
/** * Orders alphabetically the {@link Unit} objects within the * {@link #listOfUnits} of a {@link UnitDefinition}. * * @param ud * the {@link UnitDefinition} object whose {@link Unit}s are to be * reordered. */ public static void reorder(UnitDefinition ud) { if (1 < ud.getUnitCount()) { ListOf<Unit> orig = ud.getListOfUnits(); ListOf<Unit> units = new ListOf<Unit>(ud.getLevel(), ud.getVersion()); units.setSBaseListType(Type.listOfUnits); orig.removeAllTreeNodeChangeListeners(); units.add(orig.remove(orig.size() - 1)); int i, j; for (i = orig.size() - 1; i >= 0; i--) { Unit u = orig.remove(i); j = 0; while ((j < units.size()) && (0 < u.getKind().compareTo(units.get(j).getKind()))) { j++; } units.add(j, u); } ud.setListOfUnits(units); } }
@Override @SuppressWarnings("deprecation") public void setUnits(Unit unit) { if ((unit.getExponent() != 1) || (unit.getScale() != 0) || (unit.getMultiplier() != 1d) || (unit.getOffset() != 0d)) { StringBuilder sb = new StringBuilder(); sb.append('_'); sb.append(unit.getMultiplier()); sb.append('_'); sb.append(unit.getScale()); sb.append('_'); sb.append(unit.getKind().toString()); sb.append('_'); sb.append(unit.getExponent()); UnitDefinition ud = new UnitDefinition(sb.toString().replace('.', '_'), getLevel(), getVersion()); ud.addUnit(unit); Model m = getModel(); if (m != null) { m.addUnitDefinition(ud); } setUnits(ud); } else { // must be a base unit setUnits(unit.getKind().toString().toLowerCase()); } }
@Override @Deprecated public void setUnits(Unit unit) { if (!unit.isVariantOfSubstance() && !unit.isVariantOfTime()) { throw new IllegalArgumentException(MessageFormat.format( ILLEGAL_UNIT_KIND_EXCEPTION_MSG, unit.toString())); } if ((unit.getExponent() != 1) || (unit.getScale() != 0) || (unit.getMultiplier() != 1d) || (unit.getOffset() != 0d)) { StringBuilder sb = new StringBuilder(); sb.append(unit.getMultiplier()); sb.append('_'); sb.append(unit.getScale()); sb.append('_'); sb.append(unit.getKind().toString()); sb.append('_'); sb.append(unit.getExponent()); UnitDefinition ud = new UnitDefinition(sb.toString(), getLevel(), getVersion()); ud.addUnit(unit); Model m = getModel(); if (m != null) { m.addUnitDefinition(ud); } setUnits(ud.getId()); } else { setUnits(unit.getKind().toString()); } }
/** * Throws an {@link IllegalArgumentException} if the given units do not * represent a dimensionless or invalid unit. * * @param units */ protected void checkForDimensionlessOrInvalidUnits(UnitDefinition units) { units.simplify(); String illegal = null; if (units.getUnitCount() == 1) { Kind kind = units.getUnit(0).getKind(); if ((kind != Kind.DIMENSIONLESS) && (kind != Kind.ITEM) && (kind != Kind.RADIAN) && (kind != Kind.STERADIAN) && (kind != Kind.INVALID)) { illegal = kind.toString(); } } else { illegal = units.toString(); } if (illegal != null && (!allowInvalidModel) ) { throw new IllegalArgumentException( new UnitException(MessageFormat.format( "An invalid or dimensionless unit is required but given is {0}.", illegal))); } }
/** * Creates a unit ontology identifier for this {@link Kind} if possible * and returns it. See <a href="http://obo.cvs.sourceforge.net/viewvc/obo/obo/ontology/phenotype/unit.obo">the Unit Ontology</a> * for more information. * * @return the unit ontology identifier for this {@link Kind} or null if * this {@link Kind} has no corresponding type in the unit * ontology. * * @see #getUnitOntologyNumber() */ public String getUnitOntologyIdentifier() { int id = getUnitOntologyNumber(); if (id > -1) { StringBuilder sb = new StringBuilder(); sb.append("UO:"); sb.append(id); while (sb.toString().length() < 10) { sb.insert(3, 0); } return sb.toString(); } return null; }
@Override @Deprecated public void setUnits(Unit unit) { UnitDefinition ud = new UnitDefinition(unit.getKind().toString(), getLevel(), getVersion()); ud.addUnit(unit); if ((unit.getExponent() != 1) || (unit.getScale() != 0) || (unit.getMultiplier() != 1d) || (unit.getOffset() != 0d)) { StringBuilder sb = new StringBuilder(); sb.append(unit.getMultiplier()); sb.append('_'); sb.append(unit.getScale()); sb.append('_'); sb.append(unit.getKind().toString()); sb.append('_'); sb.append(unit.getExponent()); ud.setId(sb.toString()); Model m = getModel(); if (m != null) { m.addUnitDefinition(ud); } } setUnits(ud); }
/** * Throws an {@link IllegalArgumentException} if the given units do not * represent a dimensionless or invalid unit. * * @param units */ private void checkForDimensionlessOrInvalidUnits(UnitDefinition units) { units.simplify(); String illegal = null; if (units.getUnitCount() == 1) { Kind kind = units.getUnit(0).getKind(); if ((kind != Kind.DIMENSIONLESS) && (kind != Kind.ITEM) && (kind != Kind.RADIAN) && (kind != Kind.STERADIAN) && (kind != Kind.INVALID)) { illegal = kind.toString(); } } else { illegal = units.toString(); } if (illegal != null) { throw new IllegalArgumentException( new UnitException(MessageFormat.format( "An invalid or dimensionless unit is required but given is {0}.", illegal))); } }
/** * Produces a text formula representation of this unit. */ public String printUnit() { StringBuffer times = new StringBuffer(); if (getMultiplier() != 0d) { if (getMultiplier() != 1d) { times.append(StringTools.toString(getMultiplier())); } StringBuffer pow = new StringBuffer(); pow.append(kind != null ? kind.getSymbol() : "undefined"); String prefix = getPrefix(); if ((prefix.length() > 0) && !prefix.startsWith("10")) { pow.insert(0, prefix); } else if (getScale() != 0) { pow = FormulaCompiler.times(FormulaCompiler.pow(Integer.valueOf(10), getScale()), pow); } times = FormulaCompiler.times(times, pow); } if ((offset != null) && (offset.doubleValue() != 0d)) { times = FormulaCompiler.sum(StringTools.toString(offset.doubleValue()), times); } return FormulaCompiler.pow(times, StringTools.toString(getExponent())).toString(); }
/** * <p> * Predicate returning true or false depending on whether Unit objects are * equivalent. * </p> * <p> * Two Unit objects are considered to be equivalent if their 'kind' and * 'exponent' attributes are equal. (Contrast this to the method * areIdentical(Unit unit1, Unit unit2), which compares Unit objects with * respect to all attributes, not just the kind and exponent.) * </p> * * @param unit1 * the first Unit object to compare * @param unit2 * the second Unit object to compare * @return if the 'kind' and 'exponent' attributes of unit1 are identical to * the kind and exponent attributes of unit2, {@code false} otherwise. */ public static boolean areEquivalent(Unit unit1, Unit unit2) { return Kind.areEquivalent(unit1.getKind(), unit2.getKind()) && (unit1.getExponent() == unit2.getExponent() || (Math.abs(unit1.getExponent() - unit2.getExponent()) < (unit1.getExponent() * 0.001))); } // TODO - check if we can use some of the Math or StrictMath operations when calculating exponent