Class Timer

  • All Implemented Interfaces:
    java.io.Serializable, StringBuilderFormattable

    public class Timer
    extends java.lang.Object
    implements java.io.Serializable, StringBuilderFormattable
    Primarily used in unit tests, but can be used to track elapsed time for a request or portion of any other operation so long as all the timer methods are called on the same thread in which it was started. Calling start on multiple threads will cause the the times to be aggregated.
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Timer.Status  
    • Constructor Summary

      Constructors 
      Constructor Description
      Timer​(java.lang.String name)
      Constructor.
      Timer​(java.lang.String name, int iterations)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object o)  
      void formatTo​(java.lang.StringBuilder buffer)
      Writes a text representation of this object into the specified StringBuilder, ideally without allocating temporary objects.
      long getElapsedNanoTime()
      Access the elapsed time.
      long getElapsedTime()
      Access the elapsed time.
      java.lang.String getName()
      Accessor for the name.
      Timer.Status getStatus()
      Returns the name of the last operation performed on this timer (Start, Stop, Pause or Resume).
      int hashCode()  
      void pause()
      Pause the timer.
      void resume()
      Resume the timer.
      void start()
      Start the timer.
      void startOrResume()  
      java.lang.String stop()
      Stop the timer.
      java.lang.String toString()
      Returns the String representation of the timer based upon its current state
      • Methods inherited from class java.lang.Object

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

      • name

        private final java.lang.String name
      • elapsedTime

        private long elapsedTime
      • iterations

        private final int iterations
      • NANO_PER_SECOND

        private static long NANO_PER_SECOND
      • NANO_PER_MINUTE

        private static long NANO_PER_MINUTE
      • NANO_PER_HOUR

        private static long NANO_PER_HOUR
      • startTime

        private java.lang.ThreadLocal<java.lang.Long> startTime
    • Constructor Detail

      • Timer

        public Timer​(java.lang.String name)
        Constructor.
        Parameters:
        name - the timer name.
      • Timer

        public Timer​(java.lang.String name,
                     int iterations)
        Constructor.
        Parameters:
        name - the timer name.
    • Method Detail

      • start

        public void start()
        Start the timer.
      • startOrResume

        public void startOrResume()
      • stop

        public java.lang.String stop()
        Stop the timer.
      • pause

        public void pause()
        Pause the timer.
      • resume

        public void resume()
        Resume the timer.
      • getName

        public java.lang.String getName()
        Accessor for the name.
        Returns:
        the timer's name.
      • getElapsedTime

        public long getElapsedTime()
        Access the elapsed time.
        Returns:
        the elapsed time.
      • getElapsedNanoTime

        public long getElapsedNanoTime()
        Access the elapsed time.
        Returns:
        the elapsed time.
      • getStatus

        public Timer.Status getStatus()
        Returns the name of the last operation performed on this timer (Start, Stop, Pause or Resume).
        Returns:
        the string representing the last operation performed.
      • toString

        public java.lang.String toString()
        Returns the String representation of the timer based upon its current state
        Overrides:
        toString in class java.lang.Object
      • formatTo

        public void formatTo​(java.lang.StringBuilder buffer)
        Description copied from interface: StringBuilderFormattable
        Writes a text representation of this object into the specified StringBuilder, ideally without allocating temporary objects.
        Specified by:
        formatTo in interface StringBuilderFormattable
        Parameters:
        buffer - the StringBuilder to write into
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object