Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) OSGi Alliance (2002, 2013). All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.osgi.util.measurement;

Represents a value with an error, a unit and a time-stamp.

A Measurement object is used for maintaining the tuple of value, error, unit and time-stamp. The value and error are represented as doubles and the time is measured in milliseconds since midnight, January 1, 1970 UTC.

Mathematic methods are provided that correctly calculate taking the error into account. A runtime error will occur when two measurements are used in an incompatible way. E.g., when a speed (m/s) is added to a distance (m). The measurement class will correctly track changes in unit during multiplication and division, always coercing the result to the most simple form. See Unit for more information on the supported units.

Errors in the measurement class are absolute errors. Measurement errors should use the P95 rule. Actual values must fall in the range value +/- error 95% or more of the time.

A Measurement object is immutable in order to be easily shared.

Note: This class has a natural ordering that is inconsistent with equals. See compareTo(java.lang.Object).

Author(s):
$Id: 5d6be0f87b34d62ad5671e13710ef52ea36abf0c $
Immutable:
 
 public class Measurement implements Comparable {
 	private final double		value;
 	private final double		error;
 	private final long			time;
 	private final Unit			unit;
 	private transient String	name;
 	private transient int		hashCode;

Create a new Measurement object.

Parameters:
value The value of the Measurement.
error The error of the Measurement.
unit The Unit object in which the value is measured. If this argument is null, then the unit will be set to Unit.unity.
time The time measured in milliseconds since midnight, January 1, 1970 UTC.
 
 	public Measurement(double valuedouble errorUnit unitlong time) {
 		this. = value;
 		this. = Math.abs(error);
 		this. = (unit != null) ? unit : .;
 		this. = time;
 		 = null;
 		 = 0;
 	}

Create a new Measurement object with a time of zero.

Parameters:
value The value of the Measurement.
error The error of the Measurement.
unit The Unit object in which the value is measured. If this argument is null, then the unit will be set to Unit.unity.
 
 	public Measurement(double valuedouble errorUnit unit) {
 		this(valueerrorunit, 0l);
 	}

Create a new Measurement object with an error of 0.0 and a time of zero.

Parameters:
value The value of the Measurement.
unit The Unit in which the value is measured. If this argument is null, then the unit will be set to Unit.unity.
 
	public Measurement(double valueUnit unit) {
		this(value, 0.0d, unit, 0l);
	}

Create a new Measurement object with an error of 0.0, a unit of Unit.unity and a time of zero.

Parameters:
value The value of the Measurement.
	public Measurement(double value) {
		this(value, 0.0d, null, 0l);
	}

Returns the value of this Measurement object.

Returns:
The value of this Measurement object as a double.
	public final double getValue() {
		return ;
	}

Returns the error of this Measurement object. The error is always a positive value.

Returns:
The error of this Measurement as a double.
	public final double getError() {
		return ;
	}

Returns the Unit object of this Measurement object.

Returns:
The Unit object of this Measurement object.
See also:
Unit
	public final Unit getUnit() {
		return ;
	}

Returns the time at which this Measurement object was taken. The time is measured in milliseconds since midnight, January 1, 1970 UTC, or zero when not defined.

Returns:
The time at which this Measurement object was taken or zero.
	public final long getTime() {
		return ;
	}

Returns a new Measurement object that is the product of this object multiplied by the specified object.

Parameters:
m The Measurement object that will be multiplied with this object.
Returns:
A new Measurement that is the product of this object multiplied by the specified object. The error and unit of the new object are computed. The time of the new object is set to the time of this object.
Throws:
java.lang.ArithmeticException If the Unit objects of this object and the specified object cannot be multiplied.
See also:
Unit
	public Measurement mul(Measurement m) {
		double mvalue = m.value;
		return new Measurement( * mvalue, Math.abs() * m.error +  * Math.abs(mvalue), .mul(m.unit), );
	}

Returns a new Measurement object that is the product of this object multiplied by the specified value.

Parameters:
d The value that will be multiplied with this object.
u The Unit of the specified value.
Returns:
A new Measurement object that is the product of this object multiplied by the specified value. The error and unit of the new object are computed. The time of the new object is set to the time of this object.
Throws:
java.lang.ArithmeticException If the units of this object and the specified value cannot be multiplied.
See also:
Unit
	public Measurement mul(double dUnit u) {
		return new Measurement( * d * Math.abs(d), .mul(u), );
	}

Returns a new Measurement object that is the product of this object multiplied by the specified value.

Parameters:
d The value that will be multiplied with this object.
Returns:
A new Measurement object that is the product of this object multiplied by the specified value. The error of the new object is computed. The unit and time of the new object is set to the unit and time of this object.
	public Measurement mul(double d) {
		return new Measurement( * d * Math.abs(d), );
	}

Returns a new Measurement object that is the quotient of this object divided by the specified object.

Parameters:
m The Measurement object that will be the divisor of this object.
Returns:
A new Measurement object that is the quotient of this object divided by the specified object. The error and unit of the new object are computed. The time of the new object is set to the time of this object.
Throws:
java.lang.ArithmeticException If the Unit objects of this object and the specified object cannot be divided.
See also:
Unit
	public Measurement div(Measurement m) {
		double mvalue = m.value;
		return new Measurement( / mvalue, (Math.abs() * m.error +  * Math.abs(mvalue)) / (mvalue * mvalue), .div(m.unit), );
	}

Returns a new Measurement object that is the quotient of this object divided by the specified value.

Parameters:
d The value that will be the divisor of this object.
u The Unit object of the specified value.
Returns:
A new Measurement that is the quotient of this object divided by the specified value. The error and unit of the new object are computed. The time of the new object is set to the time of this object.
Throws:
java.lang.ArithmeticException If the Unit objects of this object and the specified object cannot be divided.
See also:
Unit
	public Measurement div(double dUnit u) {
		return new Measurement( / d / Math.abs(d), .div(u), );
	}

Returns a new Measurement object that is the quotient of this object divided by the specified value.

Parameters:
d The value that will be the divisor of this object.
Returns:
A new Measurement object that is the quotient of this object divided by the specified value. The error of the new object is computed. The unit and time of the new object is set to the Unit and time of this object.
	public Measurement div(double d) {
		return new Measurement( / d / Math.abs(d), );
	}

Returns a new Measurement object that is the sum of this object added to the specified object. The error and unit of the new object are computed. The time of the new object is set to the time of this object.

Parameters:
m The Measurement object that will be added with this object.
Returns:
A new Measurement object that is the sum of this and m.
Throws:
java.lang.ArithmeticException If the Unit objects of this object and the specified object cannot be added.
See also:
Unit
	public Measurement add(Measurement m) {
		return new Measurement( + m.value + m.error.add(m.unit), );
	}

Returns a new Measurement object that is the sum of this object added to the specified value.

Parameters:
d The value that will be added with this object.
u The Unit object of the specified value.
Returns:
A new Measurement object that is the sum of this object added to the specified value. The unit of the new object is computed. The error and time of the new object is set to the error and time of this object.
Throws:
java.lang.ArithmeticException If the Unit objects of this object and the specified value cannot be added.
See also:
Unit
	public Measurement add(double dUnit u) {
		return new Measurement( + d.add(u), );
	}

Returns a new Measurement object that is the sum of this object added to the specified value.

Parameters:
d The value that will be added with this object.
Returns:
A new Measurement object that is the sum of this object added to the specified value. The error, unit, and time of the new object is set to the error, Unit and time of this object.
	public Measurement add(double d) {
		return new Measurement( + d);
	}

Returns a new Measurement object that is the subtraction of the specified object from this object.

Parameters:
m The Measurement object that will be subtracted from this object.
Returns:
A new Measurement object that is the subtraction of the specified object from this object. The error and unit of the new object are computed. The time of the new object is set to the time of this object.
Throws:
java.lang.ArithmeticException If the Unit objects of this object and the specified object cannot be subtracted.
See also:
Unit
	public Measurement sub(Measurement m) {
		return new Measurement( - m.value + m.error.sub(m.unit), );
	}

Returns a new Measurement object that is the subtraction of the specified value from this object.

Parameters:
d The value that will be subtracted from this object.
u The Unit object of the specified value.
Returns:
A new Measurement object that is the subtraction of the specified value from this object. The unit of the new object is computed. The error and time of the new object is set to the error and time of this object.
Throws:
java.lang.ArithmeticException If the Unit objects of this object and the specified object cannot be subtracted.
See also:
Unit
	public Measurement sub(double dUnit u) {
		return new Measurement( - d.sub(u), );
	}

Returns a new Measurement object that is the subtraction of the specified value from this object.

Parameters:
d The value that will be subtracted from this object.
Returns:
A new Measurement object that is the subtraction of the specified value from this object. The error, unit and time of the new object is set to the error, Unit object and time of this object.
	public Measurement sub(double d) {
		return new Measurement( - d);
	}

Returns a String object representing this Measurement object.

Returns:
a String object representing this Measurement object.
	public String toString() {
		String result = ;
		if (result == null) {
			StringBuffer sb = new StringBuffer();
			if ( != 0.0d) {
				sb.append(" +/- ");
			}
			if (u.length() > 0) {
				sb.append(" ");
				sb.append(u);
			}
			result = sb.toString();
			 = result;
		}
		return result;
	}

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer if this object is less than, equal to, or greater than the specified object.

Note: This class has a natural ordering that is inconsistent with equals. For this method, another Measurement object is considered equal if there is some x such that

 getValue() - getError() <= x <= getValue() + getError()
 
for both Measurement objects being compared.

Parameters:
obj The object to be compared.
Returns:
A negative integer, zero, or a positive integer if this object is less than, equal to, or greater than the specified object.
Throws:
java.lang.ClassCastException If the specified object is not of type Measurement.
java.lang.ArithmeticException If the unit of the specified Measurement object is not equal to the Unit object of this object.
	public int compareTo(Object obj) {
		if (this == obj) {
			return 0;
		}
		Measurement that = (Measurementobj;
		if (!.equals(that.unit)) {
			throw new ArithmeticException("Cannot compare " + this + " and " + that);
		}
		int result = Double.compare(that.value);
		if (result == 0) {
			return 0;
		}
		if (result < 0) {
			if (Double.compare( + that.value - that.error) >= 0) {
				return 0;
			}
			return -1;
		}
		if (Double.compare( - that.value + that.error) <= 0) {
			return 0;
		}
		return 1;
	}

Returns a hash code value for this object.

Returns:
A hash code value for this object.
	public int hashCode() {
		int h = ;
		if (h == 0) {
			long bits = Double.doubleToLongBits();
			h = 31 * 17 + ((int) (bits ^ (bits >>> 32)));
			bits = Double.doubleToLongBits();
			h = 31 * h + ((int) (bits ^ (bits >>> 32)));
			h = 31 * h + .hashCode();
			 = h;
		}
		return h;
	}

Returns whether the specified object is equal to this object. Two Measurement objects are equal if they have same value, error and Unit.

Note: This class has a natural ordering that is inconsistent with equals. See compareTo(java.lang.Object).

Parameters:
obj The object to compare with this object.
Returns:
true if this object is equal to the specified object; false otherwise.
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof Measurement)) {
			return false;
		}
		Measurement that = (Measurementobj;
		return (Double.compare(that.value) == 0) && (Double.compare(that.error) == 0) && .equals(that.unit);
	}
New to GrepCode? Check out our FAQ X