/** * Returns the value of this measure stated in the specified unit as a * <code>float</code>. If the measure has too great a magnitude to be * represented as a <code>float</code>, it will be converted to * <code>Float.NEGATIVE_INFINITY</code> or * <code>Float.POSITIVE_INFINITY</code> as appropriate. * * @param unit the unit in which the measure is stated. * @return the numeric value after conversion to type <code>float</code>. */ public float floatValue(Unit<Q> unit) { return (float) doubleValue(unit); }
/** * Converts the specified speed to {@link #INTERNAL_SPEED_UNIT}. * @param speed The speed to convert. * @return The converted speed. */ @SuppressWarnings("static-method") public double toInSpeed(Measure<Double, Velocity> speed) { return speed.doubleValue(INTERNAL_SPEED_UNIT); }
/** * Returns the value of this measure stated in the specified unit as a * <code>float</code>. If the measure has too great a magnitude to be * represented as a <code>float</code>, it will be converted to * <code>Float.NEGATIVE_INFINITY</code> or * <code>Float.POSITIVE_INFINITY</code> as appropriate. * * @param unit the unit in which the measure is stated. * @return the numeric value after conversion to type <code>float</code>. */ public float floatValue(Unit<Q> unit) { return (float) doubleValue(unit); }
/** * Returns the value of this measure stated in the specified unit as a * <code>float</code>. If the measure has too great a magnitude to be * represented as a <code>float</code>, it will be converted to * <code>Float.NEGATIVE_INFINITY</code> or * <code>Float.POSITIVE_INFINITY</code> as appropriate. * * @param unit the unit in which the measure is stated. * @return the numeric value after conversion to type <code>float</code>. */ public float floatValue(Unit<Q> unit) { return (float) doubleValue(unit); }
double doCalculateTravelTime(Measure<Double, Velocity> speed, Measure<Double, Length> distance, Unit<Duration> outputTimeUnit) { return Measure.valueOf(distance.doubleValue(SI.METER) // divided by m/s / speed.doubleValue(SI.METERS_PER_SECOND), // gives seconds SI.SECOND) // convert to desired unit .doubleValue(outputTimeUnit); }
double doCalculateTravelTime(Measure<Double, Velocity> speed, Measure<Double, Length> distance, Unit<Duration> outputTimeUnit) { return Measure.valueOf(distance.doubleValue(SI.METER) // divided by m/s / speed.doubleValue(SI.METERS_PER_SECOND), // gives seconds SI.SECOND) // convert to desired unit .doubleValue(outputTimeUnit); }
/** * Computes the duration which is required to travel the specified distance * with the given velocity. Note: although time is normally a long, we use * double here instead. Converting it to long in this method would introduce * rounding in a too early stage. * @param speed The travel speed. * @param distance The distance to travel. * @param outputTimeUnit The time unit to use for the output. * @return The time it takes to travel the specified distance with the * specified speed. */ public static double computeTravelTime(Measure<Double, Velocity> speed, Measure<Double, Length> distance, Unit<Duration> outputTimeUnit) { // meters return Measure.valueOf(distance.doubleValue(SI.METER) // divided by m/s / speed.doubleValue(SI.METERS_PER_SECOND), // gives seconds SI.SECOND) // convert to desired unit .doubleValue(outputTimeUnit); }
/** * Returns the estimated integral value of this measure stated in * the specified unit as a <code>long</code>. * * <p> Note: This method differs from the <code>Number.longValue()</code> * in the sense that the closest integer value is returned * and an ArithmeticException is raised instead * of a bit truncation in case of overflow (safety critical).</p> * * @param unit the unit in which the measurable value is stated. * @return the numeric value after conversion to type <code>long</code>. * @throws ArithmeticException if this quantity cannot be represented * as a <code>long</code> number in the specified unit. */ public long longValue(Unit<Q> unit) throws ArithmeticException { double doubleValue = doubleValue(unit); if (java.lang.Double.isNaN(doubleValue) || (doubleValue < java.lang.Long.MIN_VALUE) || (doubleValue > java.lang.Long.MAX_VALUE)) throw new ArithmeticException(doubleValue + " " + unit + " cannot be represented as long"); return Math.round(doubleValue); }
/** * Returns the estimated integral value of this measure stated in * the specified unit as a <code>long</code>. * * <p> Note: This method differs from the <code>Number.longValue()</code> * in the sense that the closest integer value is returned * and an ArithmeticException is raised instead * of a bit truncation in case of overflow (safety critical).</p> * * @param unit the unit in which the measurable value is stated. * @return the numeric value after conversion to type <code>long</code>. * @throws ArithmeticException if this quantity cannot be represented * as a <code>long</code> number in the specified unit. */ public long longValue(Unit<Q> unit) throws ArithmeticException { double doubleValue = doubleValue(unit); if (java.lang.Double.isNaN(doubleValue) || (doubleValue < java.lang.Long.MIN_VALUE) || (doubleValue > java.lang.Long.MAX_VALUE)) throw new ArithmeticException(doubleValue + " " + unit + " cannot be represented as long"); return Math.round(doubleValue); }
@Override public double calculateTravelTime(Graph<?> graph, Point from, Point to, Unit<Length> distanceUnit, Measure<Double, Velocity> speed, Unit<Duration> outputTimeUnit) { final Measure<Double, Length> distance = Measure .valueOf(graph.getConnection(from, to).getLength(), distanceUnit); return Measure.valueOf(distance.doubleValue(SI.METER) // divided by m/s / speed.doubleValue(SI.METERS_PER_SECOND), // gives seconds SI.SECOND) // convert to desired unit .doubleValue(outputTimeUnit); } }
@Override public double calculateTravelTime(Graph<?> graph, Point from, Point to, Unit<Length> distanceUnit, Measure<Double, Velocity> speed, Unit<Duration> outputTimeUnit) { final Measure<Double, Length> distance = Measure .valueOf(graph.getConnection(from, to).getLength(), distanceUnit); return Measure.valueOf(distance.doubleValue(SI.METER) // divided by m/s / speed.doubleValue(SI.METERS_PER_SECOND), // gives seconds SI.SECOND) // convert to desired unit .doubleValue(outputTimeUnit); } }
static int[] toRemainingServiceTimes(GlobalStateObject state, Unit<Duration> outputTimeUnit) { final UnmodifiableIterator<VehicleStateObject> iterator = state.vehicles .iterator(); final int[] remainingServiceTimes = new int[state.vehicles.size()]; for (int i = 0; i < state.vehicles.size(); i++) { remainingServiceTimes[i] = DoubleMath.roundToInt( Measure.valueOf(iterator.next().remainingServiceTime, state.timeUnit) .doubleValue(outputTimeUnit), RoundingMode.CEILING); } return remainingServiceTimes; }
/** * Compares this measure to the specified measurable quantity. * This method compares the {@link Measurable#doubleValue(Unit)} of * both this measure and the specified measurable stated in the * same unit (this measure's {@link #getUnit() unit}). * * @return a negative integer, zero, or a positive integer as this measure * is less than, equal to, or greater than the specified measurable * quantity. * @return <code>Double.compare(this.doubleValue(getUnit()), * that.doubleValue(getUnit()))</code> */ public int compareTo(Measurable<Q> that) { return java.lang.Double.compare(doubleValue(getUnit()), that .doubleValue(getUnit())); }
/** * Compares this measure to the specified measurable quantity. * This method compares the {@link Measurable#doubleValue(Unit)} of * both this measure and the specified measurable stated in the * same unit (this measure's {@link #getUnit() unit}). * * @return a negative integer, zero, or a positive integer as this measure * is less than, equal to, or greater than the specified measurable * quantity. * @return <code>Double.compare(this.doubleValue(getUnit()), * that.doubleValue(getUnit()))</code> */ public int compareTo(Measurable<Q> that) { return java.lang.Double.compare(doubleValue(getUnit()), that .doubleValue(getUnit())); }
/** * Compares this measure to the specified measurable quantity. * This method compares the {@link Measurable#doubleValue(Unit)} of * both this measure and the specified measurable stated in the * same unit (this measure's {@link #getUnit() unit}). * * @return a negative integer, zero, or a positive integer as this measure * is less than, equal to, or greater than the specified measurable * quantity. * @return <code>Double.compare(this.doubleValue(getUnit()), * that.doubleValue(getUnit()))</code> */ public int compareTo(Measurable<Q> that) { return java.lang.Double.compare(doubleValue(getUnit()), that .doubleValue(getUnit())); }
/** * Returns the <code>String</code> representation of this measure * The string produced for a given measure is always the same; * it is not affected by locale. This means that it can be used * as a canonical string representation for exchanging data, * or as a key for a Hashtable, etc. Locale-sensitive * measure formatting and parsing is handled by the {@link * MeasureFormat} class and its subclasses. * * @return the string representation of this measure. */ public String toString() { if (getUnit() instanceof CompoundUnit) return MeasureFormat.DEFAULT.formatCompound(doubleValue(getUnit()), getUnit(), new StringBuffer(), null).toString(); return getValue() + " " + getUnit(); }
/** * Returns the <code>String</code> representation of this measure * The string produced for a given measure is always the same; * it is not affected by locale. This means that it can be used * as a canonical string representation for exchanging data, * or as a key for a Hashtable, etc. Locale-sensitive * measure formatting and parsing is handled by the {@link * MeasureFormat} class and its subclasses. * * @return the string representation of this measure. */ public String toString() { if (getUnit() instanceof CompoundUnit) return MeasureFormat.DEFAULT.formatCompound(doubleValue(getUnit()), getUnit(), new StringBuffer(), null).toString(); return getValue() + " " + getUnit(); }
/** * Returns the <code>String</code> representation of this measure * The string produced for a given measure is always the same; * it is not affected by locale. This means that it can be used * as a canonical string representation for exchanging data, * or as a key for a Hashtable, etc. Locale-sensitive * measure formatting and parsing is handled by the {@link * MeasureFormat} class and its subclasses. * * @return the string representation of this measure. */ public String toString() { if (getUnit() instanceof CompoundUnit) return MeasureFormat.DEFAULT.formatCompound(doubleValue(getUnit()), getUnit(), new StringBuffer(), null).toString(); return getValue() + " " + getUnit(); }
static int[] toRemainingServiceTimes(GlobalStateObject state, Unit<Duration> outputTimeUnit) { final UnmodifiableIterator<VehicleStateObject> iterator = state .getVehicles() .iterator(); final int[] remainingServiceTimes = new int[state.getVehicles().size()]; for (int i = 0; i < state.getVehicles().size(); i++) { remainingServiceTimes[i] = DoubleMath.roundToInt( Measure.valueOf(iterator.next().getRemainingServiceTime(), state.getTimeUnit()) .doubleValue(outputTimeUnit), RoundingMode.CEILING); } return remainingServiceTimes; }
@Test public void testMoveToDistance() { final TestRoadUser testRoadUser = new TestRoadUser(); model.addObjectAt(testRoadUser, SW); final Point start = model.getPosition(testRoadUser); final MoveProgress mp = model.moveTo(testRoadUser, NW, timeLength(3)); final Point end = model.getPosition(testRoadUser); final double lineDist = Point.distance(start, end); assertThat(lineDist).isWithin(EPSILON) .of(mp.distance().doubleValue(model.getDistanceUnit())); }