/** * Divides this unit into another unit. * * @param that * The other unit. * @return The quotient of this unit divided into the other unit. * @throws OperationException * Can't divide these units. */ @Override protected Unit myDivideInto(final Unit that) throws OperationException { return that instanceof ScaledUnit ? new ScaledUnit(((ScaledUnit) that).getScale() / getScale(), getUnit().divideInto(((ScaledUnit) that).getUnit())) : new ScaledUnit(1 / getScale(), getUnit().divideInto(that)); }
/** * Converts a numeric value from the underlying derived unit to this unit. * * @param amount * The numeric value in the underlying derived unit. * @return The equivalent value in this unit. * @throws ConversionException * Can't convert value. */ public float fromDerivedUnit(final float amount) throws ConversionException { return (float) fromDerivedUnit((double) amount); }
@Override public Unit multiplyBy(final double scale) throws MultiplyException { return getInstance(scale * _scale, _unit); }
/** * Clones this unit, changing the identifier. * * @param id * The new identifier. * @return A ScaledUnit with the new identifier. */ public Unit clone(final UnitName id) { return new ScaledUnit(_scale, getUnit(), id); }
/** * Converts a numeric value from the underlying derived unit to this unit. * * @param amount * The numeric value in the underlying derived unit. * @return The equivalent value in this unit. * @throws ConversionException * Can't convert value. */ public double fromDerivedUnit(final double amount) throws ConversionException { if (!(_unit instanceof DerivableUnit)) { throw new ConversionException(getDerivedUnit(), this); } return ((DerivableUnit) getUnit()).fromDerivedUnit(amount) / getScale(); }
final BaseUnit meter = BaseUnit.getOrCreate(UnitName.newUnitName( "meter", null, "m"), BaseQuantity.LENGTH); final ScaledUnit nauticalMile = new ScaledUnit(1852f, meter); System.out.println("nauticalMile.getUnit().equals(meter)=" + nauticalMile.getUnit().equals(meter)); final ScaledUnit nauticalMileMeter = (ScaledUnit) nauticalMile .multiplyBy(meter); System.out.println("nauticalMileMeter.divideBy(nauticalMile)=" + nauticalMileMeter.divideBy(nauticalMile)); System.out.println("meter.divideBy(nauticalMile)=" + meter.divideBy(nauticalMile)); System.out .println("nauticalMile.raiseTo(2)=" + nauticalMile.raiseTo(2)); System.out.println("nauticalMile.toDerivedUnit(1.)=" + nauticalMile.toDerivedUnit(1.)); System.out .println("nauticalMile.toDerivedUnit(new float[]{1,2,3}, new float[3])[1]=" + nauticalMile.toDerivedUnit(new float[] { 1, 2, 3 }, new float[3])[1]); System.out.println("nauticalMile.fromDerivedUnit(1852.)=" + nauticalMile.fromDerivedUnit(1852.)); System.out .println("nauticalMile.fromDerivedUnit(new float[]{1852},new float[1])[0]=" + nauticalMile.fromDerivedUnit(new float[] { 1852 }, new float[1])[0]); System.out.println("nauticalMile.equals(nauticalMile)=" + nauticalMile.equals(nauticalMile)); final ScaledUnit nautical2Mile = new ScaledUnit(2, nauticalMile); System.out.println("nauticalMile.equals(nautical2Mile)="
/** * Multiply this unit by another unit. * * @param that * The unit to multiply this unit by. Must be dimensionless. * @return The product of this unit and <code>that</code>. * @throws MultiplyException * Can't multiply these units together. */ @Override protected Unit myMultiplyBy(final Unit that) throws MultiplyException { if (!that.isDimensionless()) { throw new MultiplyException(that); } return that instanceof ScaledUnit ? new ScaledUnit(((ScaledUnit) that).getScale(), this) : this; }
/** * Gets the derived unit underlying this unit. * * @return The derived unit which underlies this unit. */ public DerivedUnit getDerivedUnit() { return getUnit().getDerivedUnit(); }
/** * Extract the value, can only be called for ScaledUnit. * @return value of this unit if ScaledUnit, else NaN */ public double getValue() { if (!(uu instanceof ScaledUnit)) return Double.NaN; ScaledUnit offset = (ScaledUnit) uu; return offset.getScale(); }
/** * Converts a numeric value from this unit to the underlying derived unit. * * @param amount * The numeric value in this unit. * @return The equivalent value in the underlying derived unit. * @throws ConversionException * Can't convert value to the underlying derived unit. */ public double toDerivedUnit(final double amount) throws ConversionException { if (!(_unit instanceof DerivableUnit)) { throw new ConversionException(this, getDerivedUnit()); } return ((DerivableUnit) _unit).toDerivedUnit(amount * getScale()); }
static Unit getInstance(final double scale, final Unit unit) throws MultiplyException { if (scale == 0) { throw new MultiplyException(scale, unit); } return scale == 1 ? unit : new ScaledUnit(scale, unit); }
/** * Returns the string representation of this unit. * * @return The string representation of this unit. */ @Override public String toString() { final String string = super.toString(); // get symbol or name return string != null ? string : getCanonicalString(); }
/** * Converts a numeric value from the underlying derived unit to this unit. * * @param amount * The numeric value in the underlying derived unit. * @return The equivalent value in this unit. * @throws ConversionException * Can't convert value. */ public double fromDerivedUnit(final double amount) throws ConversionException { if (!(_unit instanceof DerivableUnit)) { throw new ConversionException(getDerivedUnit(), this); } return ((DerivableUnit) getUnit()).fromDerivedUnit(amount) / getScale(); }
final BaseUnit meter = BaseUnit.getOrCreate(UnitName.newUnitName( "meter", null, "m"), BaseQuantity.LENGTH); final ScaledUnit nauticalMile = new ScaledUnit(1852f, meter); System.out.println("nauticalMile.getUnit().equals(meter)=" + nauticalMile.getUnit().equals(meter)); final ScaledUnit nauticalMileMeter = (ScaledUnit) nauticalMile .multiplyBy(meter); System.out.println("nauticalMileMeter.divideBy(nauticalMile)=" + nauticalMileMeter.divideBy(nauticalMile)); + meter.divideBy(nauticalMile)); System.out .println("nauticalMile.raiseTo(2)=" + nauticalMile.raiseTo(2)); System.out.println("nauticalMile.toDerivedUnit(1.)=" + nauticalMile.toDerivedUnit(1.)); System.out .println("nauticalMile.toDerivedUnit(new float[]{1,2,3}, new float[3])[1]=" + nauticalMile.toDerivedUnit(new float[] { 1, 2, 3 }, new float[3])[1]); System.out.println("nauticalMile.fromDerivedUnit(1852.)=" + nauticalMile.fromDerivedUnit(1852.)); System.out .println("nauticalMile.fromDerivedUnit(new float[]{1852},new float[1])[0]=" + nauticalMile.fromDerivedUnit(new float[] { 1852 }, new float[1])[0]); System.out.println("nauticalMile.equals(nauticalMile)=" + nauticalMile.equals(nauticalMile)); final ScaledUnit nautical2Mile = new ScaledUnit(2, nauticalMile); System.out.println("nauticalMile.equals(nautical2Mile)="
/** * Divide this unit by another unit. * * @param that * The unit to divide this unit by. * @return The quotient of this unit and <code>that</code>. * @throws DivideException * Can't divide these units. */ @Override protected Unit myDivideBy(final Unit that) throws DivideException { if (!that.isDimensionless()) { throw new DivideException(that); } return that instanceof ScaledUnit ? new ScaledUnit(1.0 / ((ScaledUnit) that).getScale(), this) : this; }
/** * Gets the derived unit underlying this unit. * * @return The derived unit which underlies this unit. */ public DerivedUnit getDerivedUnit() { return getUnit().getDerivedUnit(); }
/** * Extract the value, can only be called for ScaledUnit. * @return value of this unit if ScaledUnit, else NaN */ public double getValue() { if (!(uu instanceof ScaledUnit)) return Double.NaN; ScaledUnit offset = (ScaledUnit) uu; return offset.getScale(); }
/** * Clones this unit, changing the identifier. * * @param id * The new identifier. * @return A ScaledUnit with the new identifier. */ public Unit clone(final UnitName id) { return new ScaledUnit(_scale, getUnit(), id); }