Class AbstractMetric<U extends ChronoUnit,​P extends AbstractDuration<U>>

java.lang.Object
net.time4j.engine.AbstractMetric<U,​P>
Type Parameters:
U - generic type of time unit (ChronoUnit)
P - generic type of duration result
All Implemented Interfaces:
Comparator<U>, TimeMetric<U,​P>

public abstract class AbstractMetric<U extends ChronoUnit,​P extends AbstractDuration<U>> extends Object implements TimeMetric<U,​P>, Comparator<U>

Represents a metric suitable for the default algorithm of Time4J.

If the starting time point is after the end time point then a duration computed with this metric will be negative. In this case the metric defined here will first toggle the time points to be compared and then compare all elements in the order of ascending precision. Elements which differ less than a full unit will cause an amount of 0 in related duration item. Convertible units will be consolidated in one step. Finally the representation of the duration will be normalized such that small units will be converted to larger units if possible.

This metric can be changed to a reversible one by calling reversible() (third invariance in AbstractDuration.

Author:
Meno Hochschild
See Also:
AbstractDuration
  • Method Details

    • compare

      public int compare(U u1, U u2)

      Compares time units by their length in descending order.

      Specified by:
      compare in interface Comparator<U extends ChronoUnit>
      Parameters:
      u1 - first time unit
      u2 - second time unit
      Returns:
      negative, zero or positive if u1 is greater, equal to or smaller than u2
    • between

      public <T extends TimePoint<? super U,​ T>> P between(T start, T end)
      Description copied from interface: TimeMetric

      Computes the temporal distance between two time points.

      Important note: This method might not work in Java 6 under some circumstances. In case of any problem users can use the equivalent method until() defined in the class TimePoint.

      Specified by:
      between in interface TimeMetric<U extends ChronoUnit,​P extends AbstractDuration<U>>
      Type Parameters:
      T - generic type of time point
      Parameters:
      start - first time point
      end - second time point
      Returns:
      calculated time span between given time points, will be negative if start is after end
      See Also:
      TimePoint.until(TimePoint, TimeMetric)
    • reversible

      public TimeMetric<U,​P> reversible()
      Description copied from interface: TimeMetric

      Obtains a modified metric which has reversible characteristics.

      Usually metrics are not reversible by default. The default implementation throws an UnsupportedOperationException. Overriding implementations should document the details of reversal characteristics.

      Specified by:
      reversible in interface TimeMetric<U extends ChronoUnit,​P extends AbstractDuration<U>>
      Returns:
      modified reversible time metric