Class Speed

java.lang.Object
rgu.transport.geospatial.Speed
All Implemented Interfaces:
Comparable<Speed>

public final class Speed extends Object implements Comparable<Speed>
A (non-negative) scalar travel speed.
Author:
Lee A. Christie
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Speed
    A speed of zero (stationary).
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    Returns the speed in kilometers per hour.
    double
    Returns the speed in meters per second.
    double
    Returns the speed in miles per hour.
    int
    Compares this speed to another speed.
    boolean
    equals(Object other)
    Checks if the other object represents the same speed.
    int
    Returns a hash code for this object.
    boolean
    Returns true if the speed is positive, false otherwise.
    boolean
    Returns true if the speed is zero, false otherwise.
    static Speed
    ofKilometersPerHour(double kilometersPerHour)
    Returns an instance of Speed for the specified kilometers per hour.
    static Speed
    ofMetersPerSecond(double metersPerSecond)
    Returns an instance of Speed for the specified meters per second.
    static Speed
    ofMilesPerHour(double milesPerHour)
    Returns an instance of Speed for the specified miles peer hour.
    static Speed
    Reads the speed value from the specified input stream.
    time(Distance distance)
    Returns the total time taken to travel a given distance as this constant velocity, using double precision rounded to the nearest nanosecond.
    Returns the total time taken to travel a given distance as this constant velocity, rounded to the nearest whole number of seconds.
    Returns the total time taken to travel a given distance as this constant velocity, rounded down to a whole number of seconds.
    Returns a string representation of the speed (shown in meters per second).
    void
    Writes the speed value to the specified output stream.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • ZERO

      public static final Speed ZERO
      A speed of zero (stationary).
  • Method Details

    • ofMetersPerSecond

      public static Speed ofMetersPerSecond(double metersPerSecond)
      Returns an instance of Speed for the specified meters per second.
      Parameters:
      metersPerSecond - the speed, in the range [0, inf)
      Returns:
      an instance of Speed
    • ofKilometersPerHour

      public static Speed ofKilometersPerHour(double kilometersPerHour)
      Returns an instance of Speed for the specified kilometers per hour.
      Parameters:
      kilometersPerHour - the speed, in the range [0, inf)
      Returns:
      an instance of Speed
    • ofMilesPerHour

      public static Speed ofMilesPerHour(double milesPerHour)
      Returns an instance of Speed for the specified miles peer hour.
      Parameters:
      milesPerHour - the speed, in the range [0, inf)
      Returns:
      an instance of Speed
    • asMetersPerSecond

      public double asMetersPerSecond()
      Returns the speed in meters per second.
      Returns:
      the speed
    • asKilometersPerHour

      public double asKilometersPerHour()
      Returns the speed in kilometers per hour.
      Returns:
      the speed
    • asMilesPerHour

      public double asMilesPerHour()
      Returns the speed in miles per hour.
      Returns:
      the speed
    • toString

      public String toString()
      Returns a string representation of the speed (shown in meters per second).
      Overrides:
      toString in class Object
      Returns:
      a string representation
    • compareTo

      public int compareTo(Speed other)
      Compares this speed to another speed.
      Specified by:
      compareTo in interface Comparable<Speed>
      Parameters:
      other - the other speed, not null
      Returns:
      0, -1, or 1 based on Comparable definition
    • hashCode

      public int hashCode()
      Returns a hash code for this object.
      Overrides:
      hashCode in class Object
      Returns:
      a hash code
    • equals

      public boolean equals(Object other)
      Checks if the other object represents the same speed.
      Overrides:
      equals in class Object
      Parameters:
      other - another object
      Returns:
      true if equal, false otherwise
    • write

      public void write(ObjectOutputStream out) throws IOException
      Writes the speed value to the specified output stream.
      Parameters:
      out - the output stream, not null
      Throws:
      IOException - if an exception occurred in writing
    • read

      public static Speed read(ObjectInputStream in) throws IOException
      Reads the speed value from the specified input stream. There is a special case where if the read value is the NaN, the value null will be returned.
      Parameters:
      in - the input stream, not null
      Returns:
      the speed or null
      Throws:
      IOException - if an exception occurred in reading
    • timeTruncatedToSeconds

      public Duration timeTruncatedToSeconds(Distance distance)
      Returns the total time taken to travel a given distance as this constant velocity, rounded down to a whole number of seconds.
      Parameters:
      distance - the distance, not null
      Returns:
      the time
    • timeRoundedToSeconds

      public Duration timeRoundedToSeconds(Distance distance)
      Returns the total time taken to travel a given distance as this constant velocity, rounded to the nearest whole number of seconds.
      Parameters:
      distance - the distance, not null
      Returns:
      the time
    • time

      public Duration time(Distance distance)
      Returns the total time taken to travel a given distance as this constant velocity, using double precision rounded to the nearest nanosecond. Precision loss is likely due to floating-point error.
      Parameters:
      distance - the distance, not null
      Returns:
      the time
    • isPositive

      public boolean isPositive()
      Returns true if the speed is positive, false otherwise. If the speed is not positive, then it must be zero.
      Returns:
      true if positive, false if zero
    • isZero

      public boolean isZero()
      Returns true if the speed is zero, false otherwise. If the speed is not zero, then it must be positive.
      Returns:
      true if zero, false if positive