Class Counter

  • Direct Known Subclasses:
    UnmodifiableCounter

    public class Counter
    extends Object

    An object that stores time units for a particular counter type (in nanoseconds) for elapsed time evaluation.

    The associated counter type defines the time fetch strategy applied by the methods setUnitsBefore() and setUnitsAfter().

    Once the units are set, calling elapsedTime() will return a Duration that represents either:

    • the elapsed time between calls to setUnitsBefore() and setUnitsAfter() provided that both methods have been called before; or
    • the elapsed time between calls to setUnitsBefore() and elapsedTime() if setUnitsAfter() was not called

    Optionally, call elapsedTime(TimeUnit) to return the total elapsed time in a specific time unit.

    Author:
    oswaldo.bapvic.jr
    See Also:
    Counter.Type, Duration, ConversionMode
    • Constructor Detail

      • Counter

        public Counter​(Counter.Type type)
        Builds a Counter with a given type and default time unit.
        Parameters:
        type - the type to set; cannot be null
        Throws:
        NullPointerException - if the specified type is null
      • Counter

        public Counter​(Counter.Type type,
                       ConversionMode conversionMode)
        Builds a Counter with the given type, time unit, and conversion mode.
        Parameters:
        type - the type to set; cannot be null
        conversionMode - the ConversionMode to be applied
        Throws:
        NullPointerException - if the specified type is null
        Since:
        2.0.0
    • Method Detail

      • getUnitsBefore

        public long getUnitsBefore()
        Returns the value of the unitsBefore field.
        Returns:
        the value of the unitsBefore field
      • setUnitsBefore

        public void setUnitsBefore​(long unitsBefore)
        Populates the unitsBefore field with an arbitrary value.
        Parameters:
        unitsBefore - the units to be set
      • getUnitsAfter

        public long getUnitsAfter()
        Returns the value of the unitsAfter field.
        Returns:
        the value of the unitsAfter field
      • setUnitsAfter

        public void setUnitsAfter​(long unitsAfter)
        Populates the unitsAfter field with an arbitrary value.
        Parameters:
        unitsAfter - the units to be set
      • elapsedTime

        public Duration elapsedTime()
        Returns the elapsed time.
        Returns:
        the difference between unitsBefore and unitsAfter, if both units are set; or the difference between unitsBefore and the current value retrieved by the counter's time source, if unitsAfter is not set.
      • elapsedTime

        public double elapsedTime​(TimeUnit timeUnit)
        Returns the elapsed time in the specified TimeUnit.
        Parameters:
        timeUnit - the time unit to which the elapsed time will be converted
        Returns:
        the difference between unitsBefore and unitsAfter, if both units are set; or the difference between unitsBefore and the current value retrieved by the counter's time source, if unitsAfter is not set. The value is converted into the specified time unit applying the default conversion mode.
      • elapsedTime

        public double elapsedTime​(TimeUnit timeUnit,
                                  ConversionMode conversionMode)
        Returns the elapsed time, in a given TimeUnit, with a custom ConversionMode.
        Parameters:
        timeUnit - the time unit to which the elapsed time will be converted
        conversionMode - the ConversionMode to be used
        Returns:
        the difference between unitsBefore and unitsAfter, if both units are set; or the difference between unitsBefore and the current value retrieved by the counter's time source, if unitsAfter is not set. The value is converted into the specified time unit applying the given conversion mode.
        Since:
        2.0.0