visad
Class ScaledUnit

java.lang.Object
  extended by visad.Unit
      extended by visad.ScaledUnit
All Implemented Interfaces:
Serializable

public final class ScaledUnit
extends Unit
implements Serializable

A class that represents a certain amount of a derived unit. Instances are immutable.

Author:
Steven R. Emmerson This is part of Steve Emerson's Unit package that has been incorporated into VisAD.
See Also:
Serialized Form

Field Summary
 
Fields inherited from class visad.Unit
hashCode
 
Constructor Summary
ScaledUnit(double amount)
          Construct a dimensionless scaled unit.
ScaledUnit(double amount, BaseUnit unit)
           
ScaledUnit(double amount, BaseUnit unit, String id)
           
ScaledUnit(double amount, DerivedUnit unit)
           
ScaledUnit(double amount, DerivedUnit unit, String id)
           
ScaledUnit(double amount, String identifier)
          Construct a dimensionless scaled unit with an identifier.
ScaledUnit(double amount, Unit unit)
          Constructs a scaled unit from another unit.
ScaledUnit(double amount, Unit unit, String identifier)
          Constructs a scaled unit from another unit and an identifier.
 
Method Summary
static ScaledUnit create(double amount, Unit unit)
          Factory method for creating a scaled unit.
 Unit divide(Unit that)
          Divides this unit by another unit.
protected  Unit divideInto(Unit that)
          Divides this unit into another unit.
 boolean equals(Unit unit)
          Indicates if this instance is equal to a unit.
 double getAmount()
          Get the scale amount
 String getDefinition()
          Returns the definition of this unit.
 DerivedUnit getDerivedUnit()
          Returns the derived unit that underlies this unit.
 Unit getUnit()
          Get the underlying unit
 int hashCode()
          Returns the hash code of this instance.
 boolean isConvertible(Unit unit)
          Indicate whether this unit is convertible with another unit.
 boolean isDimensionless()
           Indicates if this instance is dimensionless.
 Unit log(double base)
          Returns the logarithmic unit that has this unit as its reference level.
static void main(String[] args)
          Test this class.
 Unit multiply(Unit that)
          Multiplies this unit by another unit.
 Unit pow(double power)
          Raises this unit to a power.
 Unit pow(int power)
          Raises this unit to a power.
protected  Unit protectedClone(String identifier)
          Clones this unit, changing the identifier.
 Unit root(int root)
          Returns the N-th root of this unit.
 Unit scale(double amount)
          Scale this unit by an amount.
 Unit shift(double offset)
          Shift this unit by an amount.
 double[] toThat(double[] values, Unit that)
          Convert values from this unit to another unit.
 double[] toThat(double[] values, Unit that, boolean copy)
          Convert values from this unit to another unit.
 float[] toThat(float[] values, Unit that)
          Convert values from this unit to another unit.
 float[] toThat(float[] values, Unit that, boolean copy)
          Convert values from this unit to another unit.
 double[] toThis(double[] values, Unit that)
          Convert values to this unit from another unit.
 double[] toThis(double[] values, Unit that, boolean copy)
          Convert values to this unit from another unit.
 float[] toThis(float[] values, Unit that)
          Convert values to this unit from another unit.
 float[] toThis(float[] values, Unit that, boolean copy)
          Convert values to this unit from another unit.
 
Methods inherited from class visad.Unit
adjustCheckAndCache, canConvert, canConvertArray, clone, convertTuple, convertTuple, convertTuple, convertTuple, copyUnitsArray, equals, getAbsoluteUnit, getIdentifier, sqrt, toString, toThat, toThis, transformUnits, transformUnits, transformUnits, transformUnits
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ScaledUnit

public ScaledUnit(double amount)
Construct a dimensionless scaled unit. The identifier will be empty.

Parameters:
amount - The given amount of this unit.

ScaledUnit

public ScaledUnit(double amount,
                  String identifier)
Construct a dimensionless scaled unit with an identifier.

Parameters:
amount - The given amount of this unit.
identifier - Name or abbreviation for the unit. May be null or empty.

ScaledUnit

public ScaledUnit(double amount,
                  Unit unit)
Constructs a scaled unit from another unit. The identifier will be that of the other unit if the amount is 1; otherwise, the identifier will be null.

Parameters:
amount - The given amount of the other unit (e.g. 0.9144 to create a yard unit if unit represents a meter).
unit - The other unit.

ScaledUnit

public ScaledUnit(double amount,
                  Unit unit,
                  String identifier)
Constructs a scaled unit from another unit and an identifier.

Parameters:
amount - The given amount of the base unit (e.g. 0.9144 to create a yard unit if unit represents a meter).
unit - The other unit.
identifier - Name or abbreviation for the unit. May be null or empty. If null and if amount is 1, then the identifier of unit is used.

ScaledUnit

public ScaledUnit(double amount,
                  BaseUnit unit)

ScaledUnit

public ScaledUnit(double amount,
                  BaseUnit unit,
                  String id)

ScaledUnit

public ScaledUnit(double amount,
                  DerivedUnit unit)

ScaledUnit

public ScaledUnit(double amount,
                  DerivedUnit unit,
                  String id)
Method Detail

create

public static ScaledUnit create(double amount,
                                Unit unit)
                         throws UnitException
Factory method for creating a scaled unit. The identifier will be that of the input unit if the scaling amount is 1; otherwise, the identifier will be null.

Parameters:
amount - The given amount of the scaled unit (e.g. 3.0 to create a yard unit if unit represents a foot.
unit - The given unit.
Returns:
A corresponding scaled unit.
Throws:
UnitException - Can't create Scaled Unit from unit.

isDimensionless

public boolean isDimensionless()

Indicates if this instance is dimensionless. A unit is dimensionless if it is a measure of a dimensionless quantity like angle or concentration. Examples of dimensionless units include radian, degree, steradian, and "g/kg".

Specified by:
isDimensionless in class Unit
Returns:
True if an only if this unit is dimensionless.

protectedClone

protected Unit protectedClone(String identifier)
Clones this unit, changing the identifier.

Specified by:
protectedClone in class Unit
Parameters:
identifier - The name or abbreviation for the cloned unit. May be null or empty.
Returns:
A unit equal to this instance but with the given identifier.

scale

public Unit scale(double amount)
           throws UnitException
Description copied from class: Unit
Scale this unit by an amount.

Specified by:
scale in class Unit
Parameters:
amount - The amount by which to scale this unit. E.g. Unit yard = meter.scale(0.9144);
Returns:
A unit equal to this instance scaled by the given amount.
Throws:
UnitException - This unit cannot be scaled.

shift

public Unit shift(double offset)
           throws UnitException
Description copied from class: Unit
Shift this unit by an amount.

Specified by:
shift in class Unit
Parameters:
offset - The amount by which to shift this unit. E.g. Unit celsius = kelvin.shift(273.15);
Returns:
A unit equal to this instance with the origin shifted by the given amount.
Throws:
UnitException - The unit subclass is unknown.

log

public Unit log(double base)
Description copied from class: Unit
Returns the logarithmic unit that has this unit as its reference level. Not all units can be used as a reference level.

Specified by:
log in class Unit
Parameters:
base - The logarithmic base: one of 2, Math.E, or 10.
Returns:
The logarithmic unit that has this instance as its reference level.

pow

public Unit pow(int power)
         throws UnitException
Raises this unit to a power.

Specified by:
pow in class Unit
Parameters:
power - The power to raise this unit by.
Returns:
The unit resulting from raising this unit to power.
Throws:
UnitException - if the underlying unit can't be raised to the given power. promise: This unit has not been modified.

pow

public Unit pow(double power)
         throws IllegalArgumentException,
                UnitException
Raises this unit to a power.

Specified by:
pow in class Unit
Parameters:
power - The power to raise this unit by. If this unit is not dimensionless, then the value must be integral.
Returns:
The unit resulting from raising this unit to power.
Throws:
IllegalArgumentException - This unit is not dimensionless and power has a non-integral value.
UnitException - if the underlying unit can't be raised to the given power. promise: The unit has not been modified.

root

public Unit root(int root)
          throws IllegalArgumentException,
                 UnitException
Returns the N-th root of this unit.

Specified by:
root in class Unit
Parameters:
root - The root to take (e.g. 2 means square root). May not be zero.
Returns:
The unit corresponding to the root-th root of this unit.
Throws:
IllegalArgumentException - The root value is zero or the resulting unit would have a non-integral unit dimension.
UnitException - if the underlying unit given can't have the given root taken. promise: This unit has not been modified.

getDefinition

public String getDefinition()
Returns the definition of this unit.

Specified by:
getDefinition in class Unit
Returns:
The definition of this unit (e.g. "0.9144 m" for a yard).

getAmount

public double getAmount()
Get the scale amount

Returns:
The scale amount

getUnit

public Unit getUnit()
Get the underlying unit

Returns:
The underlying unit

multiply

public Unit multiply(Unit that)
              throws UnitException
Multiplies this unit by another unit.

Specified by:
multiply in class Unit
Parameters:
that - The unit with which to multiply this unit.
Returns:
The product of the two units. promise: Neither unit has been modified.
Throws:
UnitException - Meaningless operation.

divide

public Unit divide(Unit that)
            throws UnitException
Divides this unit by another unit.

Specified by:
divide in class Unit
Parameters:
that - The unit to divide into this unit.
Returns:
The quotient of the two units. promise: Neither unit has been modified.
Throws:
UnitException - Meaningless operation.

divideInto

protected Unit divideInto(Unit that)
                   throws UnitException
Divides this unit into another unit.

Specified by:
divideInto in class Unit
Parameters:
that - The unit to be divided by this unit.
Returns:
The quotient of the two units. promise: Neither unit has been modified.
Throws:
UnitException - Meaningless operation.

toThis

public double[] toThis(double[] values,
                       Unit that)
                throws UnitException
Convert values to this unit from another unit.

Specified by:
toThis in class Unit
Parameters:
values - The values to be converted.
that - The unit of values.
Returns:
The converted values in units of this unit. require: The units are convertible. promise: Neither unit has been modified.
Throws:
UnitException - The units are not convertible.

toThis

public float[] toThis(float[] values,
                      Unit that)
               throws UnitException
Convert values to this unit from another unit.

Specified by:
toThis in class Unit
Parameters:
values - The values to be converted.
that - The unit of values.
Returns:
The converted values in units of this unit. require: The units are convertible. promise: Neither unit has been modified.
Throws:
UnitException - The units are not convertible.

toThis

public double[] toThis(double[] values,
                       Unit that,
                       boolean copy)
                throws UnitException
Convert values to this unit from another unit.

Overrides:
toThis in class Unit
Parameters:
values - The values to be converted.
that - The unit of values.
copy - if false and that equals this, return values, else return a new array
Returns:
The converted values in units of this unit. require: The units are convertible. promise: Neither unit has been modified.
Throws:
UnitException - The units are not convertible.

toThis

public float[] toThis(float[] values,
                      Unit that,
                      boolean copy)
               throws UnitException
Convert values to this unit from another unit.

Overrides:
toThis in class Unit
Parameters:
values - The values to be converted.
that - The unit of values.
copy - if false and that equals this, return values, else return a new array
Returns:
The converted values in units of this unit. require: The units are convertible. promise: Neither unit has been modified.
Throws:
UnitException - The units are not convertible.

toThat

public double[] toThat(double[] values,
                       Unit that)
                throws UnitException
Convert values from this unit to another unit.

Specified by:
toThat in class Unit
Parameters:
values - The values to be converted in units of this unit.
that - The unit to which to convert the values.
Returns:
The converted values. require: The units are identical. promise: Neither unit has been modified.
Throws:
UnitException - The units are not convertible.

toThat

public float[] toThat(float[] values,
                      Unit that)
               throws UnitException
Convert values from this unit to another unit.

Specified by:
toThat in class Unit
Parameters:
values - The values to be converted in units of this unit.
that - The unit to which to convert the values.
Returns:
The converted values. require: The units are identical. promise: Neither unit has been modified.
Throws:
UnitException - The units are not convertible.

toThat

public double[] toThat(double[] values,
                       Unit that,
                       boolean copy)
                throws UnitException
Convert values from this unit to another unit.

Overrides:
toThat in class Unit
Parameters:
values - The values to be converted in units of this unit.
that - The unit to which to convert the values.
copy - if false and that equals this, return values, else return a new array
Returns:
The converted values. require: The units are convertible. promise: Neither unit has been modified.
Throws:
UnitException - The units are not convertible.

toThat

public float[] toThat(float[] values,
                      Unit that,
                      boolean copy)
               throws UnitException
Convert values from this unit to another unit.

Overrides:
toThat in class Unit
Parameters:
values - The values to be converted in units of this unit.
that - The unit to which to convert the values.
copy - if false and that equals this, return values, else return a new array
Returns:
The converted values. require: The units are convertible. promise: Neither unit has been modified.
Throws:
UnitException - The units are not convertible.

isConvertible

public boolean isConvertible(Unit unit)
Indicate whether this unit is convertible with another unit. If one unit is convertible with another, then the toThis(...)/ and toThat(...) methods will not throw a UnitException. Unit A is convertible with unit B if and only if unit B is convertible with unit A; hence, calling-order is irrelevant.

Specified by:
isConvertible in class Unit
Parameters:
unit - The other unit.
Returns:
True if and only if this unit is convertible with the other unit.

main

public static void main(String[] args)
                 throws UnitException
Test this class.

Parameters:
args - Arguments (ignored).
Throws:
UnitException - A problem occurred.

equals

public boolean equals(Unit unit)
Indicates if this instance is equal to a unit.

Specified by:
equals in class Unit
Parameters:
unit - The unit.
Returns:
true if and only if this instance equals the unit.

hashCode

public int hashCode()
Returns the hash code of this instance. Object.hashCode() should be overridden whenever Object.equals(Object) is.

Specified by:
hashCode in class Unit
Returns:
The hash code of this instance (includes the values).

getDerivedUnit

public DerivedUnit getDerivedUnit()
Description copied from class: Unit
Returns the derived unit that underlies this unit.

Specified by:
getDerivedUnit in class Unit
Returns:
The derived unit that underlies this unit.