Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8128589

Duration API does not respect units

    XMLWordPrintable

    Details

      Description

      Durations in JavaFX 2.0 support multiplication and division by other durations. However, these operations do not respect the basic rules of cancelling/converting units and therefore have surprising results.

      Here are some examples of duration multiplication:
      10ms * 10ms = 100ms // seems pretty normal - except it should be ms^2 for the unit
      5s * 5s = 25000s // this is nonsensical
      1h * 1h = 3600000h // yeah, it gets crazier as you choose larger units

      Division is similarly flawed:
      1000ms / 100ms = 10ms // seems pretty normal - except the units should cancel leaving a Double
      5s / 2.5s = .002s // another nonsensical answer

      The analogy that should have been applied is one of units (length/weight/etc.). When you multiply two lengths you get a square length (2ft * 2ft = 4sqft). When you divide two lengths you get a constant (10ft / 2ft = 5).

      The current Duration behavior is properly documented in the Duration library as millisecond multiplication/division. However, the behavior is not actually useful for any practical application as I have shown. It also exposes an awkward implementation detail about Durations being implemented with milliseconds, which would make the semantics for nanosecond time very hard to work into the API in the future.

      My recommendation is as follows:
      * remove/deprecate multiplication of two Durations - there is no concept of squared time, so this is not necessary/useful
      * change the signature of Division to return a Double (no units)

        Attachments

          Activity

            People

            Assignee:
            rbair Richard Bair (Inactive)
            Reporter:
            schin Stephen Chin (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported: