/** * Returns a new {@code Measurement} object that is the sum of this object * added to the specified value. * * @param d The value that will be added with this object. * @return A new {@code 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, {@code Unit} and time of this * object. */ public Measurement add(double d) { return new Measurement(value + d, error, unit, time); }
/** * Returns a new {@code Measurement} object that is the sum of this object * added to the specified value. * * @param d The value that will be added with this object. * @param u The {@code Unit} object of the specified value. * @return A new {@code 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 ArithmeticException If the {@code Unit} objects of this object * and the specified value cannot be added. * @see Unit */ public Measurement add(double d, Unit u) { return new Measurement(value + d, error, unit.add(u), time); }
/** * Creates a new {@code Unit} instance. * * @param name the name of the {@code Unit} * @param type the type of the {@code Unit} */ private Unit(String name, long type) { if (name == null) { name = computeName(type); } this.name = name; this.type = type; // System.out.println( name + " " + Long.toHexString( type ) ); }
/** * Returns a new {@code Measurement} object that is the subtraction of the * specified value from this object. * * @param d The value that will be subtracted from this object. * @param u The {@code Unit} object of the specified value. * @return A new {@code 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 ArithmeticException If the {@code Unit} objects of this object * and the specified object cannot be subtracted. * @see Unit */ public Measurement sub(double d, Unit u) { return new Measurement(value - d, error, unit.sub(u), time); }
/** * Returns a new {@code Measurement} object that is the product of this * object multiplied by the specified value. * * @param d The value that will be multiplied with this object. * @param u The {@code Unit} of the specified value. * @return A new {@code 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 ArithmeticException If the units of this object and the specified * value cannot be multiplied. * @see Unit */ public Measurement mul(double d, Unit u) { return new Measurement(value * d, error * Math.abs(d), unit.mul(u), time); }
/** * Returns a new {@code Measurement} object that is the quotient of this * object divided by the specified value. * * @param d The value that will be the divisor of this object. * @param u The {@code Unit} object of the specified value. * @return A new {@code 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 ArithmeticException If the {@code Unit} objects of this object * and the specified object cannot be divided. * @see Unit */ public Measurement div(double d, Unit u) { return new Measurement(value / d, error / Math.abs(d), unit.div(u), time); }
/** * Returns a new {@code Unit} that is the multiplication of this * {@code Unit} and the {@code Unit} specified * * @param that the {@code Unit} that will be multiplied with this * {@code Unit} * * @return a new {@code Unit} that is the multiplication of this * {@code Unit} and the {@code Unit} specified * * @throws RuntimeException if both {@code Unit} s are special * * @see Unit#isSpecial() */ Unit mul(Unit that) { if (this.isSpecial() && that.isSpecial()) { throw new ArithmeticException("Cannot multiply " + this + " with " + that); } return find(this.type - UNITY + that.type); }
/** * Returns a new {@code Unit} that is the addition of this {@code Unit} and * the {@code Unit} specified. * * @param that the {@code Unit} that should be added to this {@code Unit} * * @return a new {@code Unit} that is the addition of this {@code Unit} and * the {@code Unit} specified. * * @throws RuntimeException if the two {@code Unit} s are not the same */ Unit add(Unit that) { if (!this.equals(that)) { throw new ArithmeticException("Cannot add " + this + " to " + that); } return this; }
/** * Finds a {@code Unit} based on a type. If the {@code Unit} is not found, * it will be created and added to the list of all units under a null name. * * @param type the type of the {@code Unit} to find * * @return the {@code Unit} */ static synchronized Unit find(long type) { if (base == null) { int size = allUnits.length; base = new Hashtable(size << 1); for (int i = 0; i < size; i++) { base.put(allUnits[i], allUnits[i]); } } Unit unit = new Unit(null, type); Unit out = (Unit) base.get(unit); if (out == null) { base.put(unit, unit); out = unit; } return out; }
/** * Returns a hash code value for this object. * * @return A hash code value for this object. */ public int hashCode() { int h = hashCode; if (h == 0) { long bits = Double.doubleToLongBits(value); h = 31 * 17 + ((int) (bits ^ (bits >>> 32))); bits = Double.doubleToLongBits(error); h = 31 * h + ((int) (bits ^ (bits >>> 32))); h = 31 * h + unit.hashCode(); hashCode = h; } return h; }
/** * Returns a new {@code 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. * * @param m The {@code Measurement} object that will be added with this * object. * @return A new {@code Measurement} object that is the sum of this and m. * @see Unit * @throws ArithmeticException If the {@code Unit} objects of this object * and the specified object cannot be added. */ public Measurement add(Measurement m) { return new Measurement(value + m.value, error + m.error, unit.add(m.unit), time); }
/** * Returns a new {@code Measurement} object that is the subtraction of the * specified object from this object. * * @param m The {@code Measurement} object that will be subtracted from this * object. * @return A new {@code 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 ArithmeticException If the {@code Unit} objects of this object * and the specified object cannot be subtracted. * @see Unit */ public Measurement sub(Measurement m) { return new Measurement(value - m.value, error + m.error, unit.sub(m.unit), time); }
/** * Returns a new {@code Measurement} object that is the product of this * object multiplied by the specified object. * * @param m The {@code Measurement} object that will be multiplied with this * object. * @return A new {@code 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 ArithmeticException If the {@code Unit} objects of this object * and the specified object cannot be multiplied. * @see Unit */ public Measurement mul(Measurement m) { double mvalue = m.value; return new Measurement(value * mvalue, Math.abs(value) * m.error + error * Math.abs(mvalue), unit.mul(m.unit), time); }
/** * Returns a new {@code Measurement} object that is the quotient of this * object divided by the specified object. * * @param m The {@code Measurement} object that will be the divisor of this * object. * @return A new {@code 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 ArithmeticException If the {@code Unit} objects of this object * and the specified object cannot be divided. * @see Unit */ public Measurement div(Measurement m) { double mvalue = m.value; return new Measurement(value / mvalue, (Math.abs(value) * m.error + error * Math.abs(mvalue)) / (mvalue * mvalue), unit.div(m.unit), time); }
/** * Returns a new {@code Unit} that is the division of this {@code Unit} and * the {@code Unit} specified * * @param that the {@code Unit} that this {@code Unit} will be divided with * @return a new {@code Unit} that is the division of this {@code Unit} and * the {@code Unit} specified * * @throws RuntimeException if both {@code Unit} s are special * * @see Unit#isSpecial() */ Unit div(Unit that) { if (this.isSpecial() && that.isSpecial()) { if (this.type == that.type) { return Unit.unity; } throw new ArithmeticException("Cannot divide " + this + " by " + that); } return find(this.type - that.type + UNITY); }
/** * Returns a new {@code Measurement} object that is the subtraction of the * specified value from this object. * * @param d The value that will be subtracted from this object. * @return A new {@code 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, {@code Unit} object and time of * this object. */ public Measurement sub(double d) { return new Measurement(value - d, error, unit, time); }
/** * Returns a new {@code Unit} that is the subtraction between this * {@code Unit} and the {@code Unit} specified. * * @param that the {@code Unit} that will be subtracted from this * {@code Unit} * @return a new {@code Unit} that is the subtraction between this * {@code Unit} and the {@code Unit} specified. * * @throws RuntimeException if the {@code Unit} specified is not the same as * this {@code Unit} */ Unit sub(Unit that) { if (!this.equals(that)) { throw new ArithmeticException("Cannot subtract " + that + " from " + this); } return this; }
/** * Returns a new {@code Measurement} object that is the quotient of this * object divided by the specified value. * * @param d The value that will be the divisor of this object. * @return A new {@code 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 {@code Unit} and time of this object. */ public Measurement div(double d) { return new Measurement(value / d, error / Math.abs(d), unit, time); }
/** * Returns whether the specified object is equal to this object. Two * {@code Measurement} objects are equal if they have same value, error and * {@code Unit}. * * <p> * Note: This class has a natural ordering that is inconsistent with equals. * See {@link #compareTo(Object)}. * * @param obj The object to compare with this object. * @return {@code true} if this object is equal to the specified object; * {@code false} otherwise. */ public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Measurement)) { return false; } Measurement that = (Measurement) obj; return (Double.compare(value, that.value) == 0) && (Double.compare(error, that.error) == 0) && unit.equals(that.unit); } }
/** * Returns a new {@code Measurement} object that is the product of this * object multiplied by the specified value. * * @param d The value that will be multiplied with this object. * @return A new {@code 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(value * d, error * Math.abs(d), unit, time); }