/** * 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)); }
/** * * @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); }
@Override public boolean check(ValidationContext ctx, UnitDefinition ud) { boolean success = true; if (ud.isSetListOfUnits()) { for (Unit u : ud.getListOfUnits()) { success = success && u.getOffset() == 0; } } return success; }
/** * * @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; }
/** * 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; }
/** * * @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); }
/** * We remove the offset by expressing it within a new {@link #multiplier}, * m': * <p> * m' = {@link #offset} / 10^{@link #scale} + {@link #multiplier} * <p> * When inserting this again into the unit formula, the offset vanishes: * <p> * (({@link #offset} + {@link #multiplier} * 10^{@link #scale}) * * {@link #kind})^{@link #exponent} * <p> * then becomes * <p> * (0 + ({@link #offset} / 10^{@link #scale} + {@link #multiplier}) * 10^{@link #scale} * {@link #kind})^{@link #exponent} = * <p> * (m' * 10^{@link #scale} * {@link #kind})^{@link #exponent} * <p> * This is possible because offset and multiplier are real double numbers. * * @return this {@link Unit} whose {@link #offset} and {@link #multiplier} * might have been changed in case that there was an {@link #offset} * defined that was different to zero. */ public Unit removeOffset() { if (isSetOffset() && (getOffset() != 0d)) { setMultiplier(getOffset() / Math.pow(10, getScale()) + getMultiplier()); setOffset(0d); } return this; }
/** * <p> * Predicate returning true or false depending on whether two Unit objects * are identical. * </p> * <p> * Two Unit objects are considered to be identical if they match in all * attributes. (Contrast this to the method areEquivalent(Unit unit1, Unit * unit2), which compares Unit objects only with respect to certain * attributes.) * </p> * * @param unit1 * the first Unit object to compare * @param unit2 * the second Unit object to compare * @return {@code true} if all the attributes of unit1 are identical to the * attributes of unit2, {@code false} otherwise. * @see #areEquivalent */ public static boolean areIdentical(Unit unit1, Unit unit2) { boolean identical = areEquivalent(unit1, unit2); identical &= unit1.getOffset() == unit2.getOffset(); identical &= unit1.getMultiplier() == unit2.getMultiplier(); return identical && unit1.getScale() == unit2.getScale(); }
@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; }
/** * * @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(); }
/** * * @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(); }
@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) { 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); }
@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()); } }
attributes.put("offset", StringTools.toString(en, getOffset()));
@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; }