javax.measure
Interface Measurable<Q extends Quantity>

All Superinterfaces:
java.lang.Comparable<Measurable<Q>>
All Known Implementing Classes:
Measure

public interface Measurable<Q extends Quantity>
extends java.lang.Comparable<Measurable<Q>>

This interface represents the measurable, countable, or comparable property or aspect of a thing.

Measurable instances are for the most part scalar quantities.

     class Delay implements Measurable<Duration> {
          private final double seconds; // Implicit internal unit.
          public Delay(double value, Unit<Duration> unit) { 
              seconds = unit.getConverterTo(SI.SECOND).convert(value);
          }
          public double doubleValue(Unit<Duration> unit) { 
              return SI.SECOND.getConverterTo(unit).convert(seconds);
          }
          ...
     }
     Thread.wait(new Delay(24, NonSI.HOUR)); // Assuming Thread.wait(Measurable<Duration>) method.
     
Non-scalar quantities are nevertheless allowed as long as an aggregate value makes sense.
     class Velocity3D implements Measurable<Velocity> {
          private double x, y, z; // Meters per second.
          public double doubleValue(Unit<Velocity> unit) { // Returns the vector norm.
              double meterPerSecond = Math.sqrt(x * x + y * y + z * z);
              return SI.METER_PER_SECOND.getConverterTo(unit).convert(meterPerSecond);
          }
          ...
     }
     class ComplexCurrent implements extends Measurable<ElectricCurrent> {
          private Complex amperes;
          public double doubleValue(Unit<ElectricCurrent> unit) { // Returns the magnitude.
              return AMPERE.getConverterTo(unit).convert(amperes.magnitude());
          }
          ...
          public Complex complexValue(Unit<ElectricCurrent> unit) { ... }
     } 

For convenience, measurable instances of any type can be created using the Measure factory methods.

       Thread.wait(Measure.valueOf(24, NonSI.HOUR));

Version:
1.0, April 15, 2009
Author:
Jean-Marie Dautelle

Method Summary
 java.math.BigDecimal decimalValue(Unit<Q> unit, java.math.MathContext ctx)
          Returns the BigDecimal value of this measurable when stated in the specified unit.
 double doubleValue(Unit<Q> unit)
          Returns the double value of this measurable when stated in the specified unit.
 float floatValue(Unit<Q> unit)
          Returns the float value of this measurable when stated in the specified unit.
 int intValue(Unit<Q> unit)
          Returns the integral int value of this measurable when stated in the specified unit.
 long longValue(Unit<Q> unit)
          Returns the integral long value of this measurable when stated in the specified unit.
 
Methods inherited from interface java.lang.Comparable
compareTo
 

Method Detail

intValue

int intValue(Unit<Q> unit)
             throws java.lang.ArithmeticException
Returns the integral int value of this measurable when stated in the specified unit.

Note: This method differs from the Number.intValue() in the sense that an ArithmeticException is raised instead of a bit truncation in case of overflow (safety critical).

Parameters:
unit - the unit in which the returned value is stated.
Returns:
the numeric value after conversion to type int.
Throws:
java.lang.ArithmeticException - if this measurable cannot be represented by a int number in the specified unit.

longValue

long longValue(Unit<Q> unit)
               throws java.lang.ArithmeticException
Returns the integral long value of this measurable when stated in the specified unit.

Note: This method differs from the Number.longValue() in the sense that an ArithmeticException is raised instead of a bit truncation in case of overflow (safety critical).

Parameters:
unit - the unit in which the returned value is stated.
Returns:
the numeric value after conversion to type long.
Throws:
java.lang.ArithmeticException - if this measurable cannot be represented by a int number in the specified unit.

floatValue

float floatValue(Unit<Q> unit)
Returns the float value of this measurable when stated in the specified unit. If the measurable has too great of a magnitude to be represented as a float, FLOAT.NEGATIVE_INFINITY or FLOAT.POSITIVE_INFINITY is returned as appropriate.

Parameters:
unit - the unit in which this returned value is stated.
Returns:
the numeric value after conversion to type float.

doubleValue

double doubleValue(Unit<Q> unit)
Returns the double value of this measurable when stated in the specified unit. If the measurable has too great of a magnitude to be represented as a double, Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY is returned as appropriate.

Parameters:
unit - the unit in which this returned value is stated.
Returns:
the numeric value after conversion to type double.

decimalValue

java.math.BigDecimal decimalValue(Unit<Q> unit,
                                  java.math.MathContext ctx)
                                  throws java.lang.ArithmeticException
Returns the BigDecimal value of this measurable when stated in the specified unit.

Parameters:
unit - the unit in which the returned value is stated.
ctx - the math context being used for conversion.
Returns:
the decimal value after conversion.
Throws:
java.lang.ArithmeticException - if the result is inexact but the rounding mode is UNNECESSARY or mathContext.precision == 0 and the quotient has a non-terminating decimal expansion.


Copyright © 2009 JScience. All Rights Reserved.