/** * Returns the exponent of this {@link Unit}. This method is provided for * compatibility to libSBML only. * * @return * @deprecated use {@link #getExponent()} */ @Deprecated public double getExponentAsDouble() { return getExponent(); }
/** * Predicate for testing whether this Unit is of the {@link Kind} * {@link Kind#DIMENSIONLESS}. A unit is also dimensionless if it does not * declare an offset and at the same time its exponent is zero. In this case * the unit represents a dimensionless quantity. * * @return True if this unit represents a dimensionless quantity, i.e., its * {@link Kind} is {@link Kind#DIMENSIONLESS} or offset = exponent = 0 */ public boolean isDimensionless() { return (kind == Kind.DIMENSIONLESS) || ((getOffset() == 0d) && (getExponent() == 0d)); }
/** * <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
/** * * @return {@code true} if this Unit is a variant of volume. */ public boolean isVariantOfVolume() { Kind kind = getKind(); if ((kind == Kind.LITER) || (kind == Kind.LITRE)) { return (getOffset() == 0d) && (getExponent() == 1d); } if ((kind == Kind.METER) || (kind == Kind.METRE)) { return (getOffset() == 0d) && (getExponent() == 3d); } return false; }
/** * Returns {@code true} if this Unit is a variant of area, meaning of {@link Kind#METRE} * with an exponent of 2. * * @return {@code true} if this Unit is a variant of area. */ public boolean isVariantOfArea() { Kind kind = getKind(); return (kind == Kind.METER) || (kind == Kind.METRE) && (getOffset() == 0d) && (getExponent() == 2d); }
/** * * @return */ public boolean isVariantOfTime() { return (getKind() == Kind.SECOND) && (getOffset() == 0d) && (getExponent() == 1d); }
@Override public int hashCode() { final int prime = 823; int hashCode = super.hashCode(); hashCode += prime * Double.valueOf(getMultiplier()).hashCode(); hashCode += prime * getScale(); hashCode += prime * Double.valueOf(getExponent()).hashCode(); hashCode += prime * Double.valueOf(getOffset()).hashCode(); if (isSetKind()) { hashCode += prime * getKind().hashCode(); } return hashCode; }
/** * * @return {@code true} if this Unit is a variant of length. */ public boolean isVariantOfLength() { Kind kind = getKind(); // Meter only allowed in Level 1 or -1 (level undefined) return (getLevel() < 2 && (kind == Kind.METER)) || (kind == Kind.METRE) && (getOffset() == 0d) && (getExponent() == 1d); }
/** * Creates a Unit instance from a given Unit. * * @param unit */ public Unit(Unit unit) { super(unit); exponent = unit.exponent != null ? Double.valueOf(unit.getExponent()) : null; multiplier = unit.multiplier != null ? new Double(unit.getMultiplier()) : null; offset = unit.offset != null ? new Double(unit.getOffset()) : null; scale = unit.scale != null ? Integer.valueOf(unit.getScale()) : null; kind = unit.isSetKind() ? unit.getKind() : Kind.INVALID; isSetExponent = unit.isSetExponent; isSetMultiplier = unit.isSetMultiplier; isSetOffset = unit.isSetOffset; isSetScale = unit.isSetScale; }
/** * Raises this unit definition by the power of the given exponent, i.e., the * exponents of every unit contained by this unit definition are multiplied * with the given exponent. * * @param exponent the exponent * @return a pointer to this {@link UnitDefinition}. */ public UnitDefinition raiseByThePowerOf(double exponent) { if (isSetListOfUnits()) { Unit u; for (int i = listOfUnits.size() - 1; i >= 0; i--) { u = listOfUnits.get(i); u.setExponent(u.getExponent() * exponent); if (u.getExponent() == 0d) { listOfUnits.remove(i); } } } return this; }
@Override public boolean check(ValidationContext ctx, UnitDefinition ud) { if (ctx.isLevelAndVersionLessThan(2, 4) && ud.getId().equals("volume") && ud.getNumUnits() == 1) { Unit u = ud.getUnit(0); if (u.isMetre()) { return u.getExponent() == 3; } } return true; } };
@Override public boolean check(ValidationContext ctx, UnitDefinition ud) { if (ctx.isLevelAndVersionLessThan(2, 4) && ud.getId().equals("volume") && ud.getNumUnits() == 1) { Unit u = ud.getUnit(0); if (u.isLitre()) { return u.getExponent() == 1; } } return true; } };
/** * * @param rootExponent * @param radiant * @return * @throws SBMLException */ private <T> ASTNode2Value<Double> root(double rootExponent, ASTNode2Value radiant) throws SBMLException { UnitDefinition ud = radiant.getUnits().clone(); for (Unit u : ud.getListOfUnits()) { if ((((u.getExponent() / rootExponent) % 1d) != 0d) && !u.isDimensionless() && !u.isInvalid()) { new UnitException(MessageFormat.format( "Cannot perform power or root operation due to incompatibility with a unit exponent. Given are {0,number} as the exponent of the unit and {1,number} as the root exponent for the current computation.", u.getExponent(), rootExponent)); } if (!(u.isDimensionless() || u.isInvalid())) { u.setExponent(u.getExponent() / rootExponent); } } ASTNode2Value<Double> value = new ASTNode2Value<Double>(ud, this); value.setValue(Double.valueOf(Math.pow(radiant.toDouble(), 1d / rootExponent))); return value; }
/** * * @param rootExponent * @param radiant * @return * @throws SBMLException */ protected ASTNodeValue root(double rootExponent, ASTNodeValue radiant) throws SBMLException { UnitDefinition ud = radiant.getUnits().clone(); for (Unit u : ud.getListOfUnits()) { if ((((u.getExponent() / rootExponent) % 1d) != 0d) && !u.isDimensionless() && !u.isInvalid()) { new UnitException(MessageFormat.format( "Cannot perform power or root operation due to incompatibility with a unit exponent. Given are {0,number} as the exponent of the unit and {1,number} as the root exponent for the current computation.", u.getExponent(), rootExponent)); // TODO - this exception is never thrown } if (!(u.isDimensionless() || u.isInvalid())) { u.setExponent(u.getExponent() / rootExponent); } } ASTNodeValue value = new ASTNodeValue(ud, this); value.setValue(Double.valueOf(Math.pow(radiant.toDouble(), 1d / rootExponent))); return value; }
@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); }
/** * 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(); }
@Override public boolean equals(Object object) { boolean equals = super.equals(object); if (equals) { Unit u = (Unit) object; equals &= getMultiplier() == u.getMultiplier(); equals &= getScale() == u.getScale(); equals &= getExponent() == u.getExponent(); equals &= getOffset() == u.getOffset(); equals &= getKind() == u.getKind(); } return equals; }
private boolean isVariantOfArea(ValidationContext ctx, UnitDefinition ud) { // TODO - to really use the level and version of the context, we need to clone the UnitDefinition // and change the level and version of the UnitDefinition and Unit instances so that all the methods // on UnitDefinition or Unit use this level and version as well. if (ctx.getLevel() == 1) { return ud.isVariantOfArea(); } ud = ud.clone().simplify(); if (ud.getUnitCount() == 1) { Unit u = ud.getUnit(0); return u.getKind() == Kind.METRE && u.getExponent() == 2; } return false; } };
@Override @SuppressWarnings("deprecation") public String getDerivedUnits() { UnitDefinition ud = getDerivedUnitDefinition(); Model m = getModel(); if (m != null) { if (m.getUnitDefinition(ud.getId()) != null) { return ud.getId(); } } if (ud.getUnitCount() == 1) { Unit u = ud.getUnit(0); if ((u.getOffset() == 0) && (u.getMultiplier() == 1) && (u.getScale() == 0) && (u.getExponent() == 1)) { return u.getKind().toString().toLowerCase(); } } return null; }
/** * * @return the UnitDefinition instance which has the timeUnistID of this * KineticLaw as id. Return null if it doesn't exist. */ @Deprecated public UnitDefinition getTimeUnitsInstance() { Model m = getModel(); if ((m != null) && isSetTimeUnits()) { return m.getUnitDefinition(timeUnitsID); } if (unitsID != null) { UnitDefinition def = new UnitDefinition(UnitDefinition.TIME, getLevel(), getVersion()); Unit time; for (Unit unit : getUnitsInstance().getListOfUnits()) { time = unit.clone(); time.setExponent(-unit.getExponent()); if (unit.isVariantOfTime()) { def.addUnit(time); } } return def; } return null; }