/** * 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; }
/** * <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; }
@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 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; }
@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()); } }
@Override public boolean check(ValidationContext ctx, UnitDefinition ud) { if (ud.getId().equals("volume")) { if (ctx.isLevelAndVersionLessThan(2, 4)) { if (ud.getUnitCount() == 1) { Unit u = ud.getUnit(0); if (ctx.getLevel() == 1) { return u.isLitre(); } else if (ctx.isLevelAndVersionEqualTo(2, 1)) { return u.getKind() == Kind.LITRE || (u.getKind() == Kind.METRE && u.getExponent() == 3); } else { return u.getKind() == Kind.LITRE || (u.getKind() == Kind.METRE && u.getExponent() == 3) || ud.isVariantOfDimensionless(); } } else { return ud.isVariantOfVolume(); } } else { return ud.isVariantOfVolume() || (ud.getNumUnits() == 1 && ud.getUnit(0).isDimensionless()); } } return true; } };
/** * 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))); } }
/** * 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))); } }
/** * Tests whether this {@link UnitDefinition} contains * exactly one {@link Unit} that itself represents a {@link Kind}, i.e., * multiplier = 1, exponent = 1, scale = 1. Note that this method requires * the level and version attributes of this {@link UnitDefinition} to be * set. * * @return {@code true} if this {@link UnitDefinition} contains * exactly one {@link Unit} that itself represents a simple {@link Kind}. */ public boolean isUnitKind() { if (getUnitCount() == 1) { return Unit.isUnitKind(getUnit(0).getKind(), getLevel(), getVersion()); } return false; }
@Override public boolean check(ValidationContext ctx, UnitDefinition ud) { boolean success = true; if (ud.isSetListOfUnits()) { for (Unit u : ud.getListOfUnits()) { if (!u.isCelsius()) { success = success && Unit.isUnitKind(u.getKind(), ctx.getLevel(), ctx.getVersion()); } } } return success; } };
@Override public boolean check(ValidationContext ctx, UnitDefinition ud) { if (ud.getId().equals("length")) { if (ctx.isLevelAndVersionLesserEqualThan(2, 1)) { // Meter not allowed on L2V1 if (ctx.isLevelAndVersionEqualTo(2, 1) && ud.getUnitCount() > 0 && ud.getUnit(0).getKind() == Kind.METER) { return false; } return ud.isVariantOfLength(); } else { return ud.isVariantOfLength() || ud.isVariantOfDimensionless(); } } return true; } };
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; }
/** * 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); } }