Package net.obvj.performetrics
Class Counter
- java.lang.Object
-
- net.obvj.performetrics.Counter
-
public class Counter extends Object
An object that stores time units of a particular type for elapsed time evaluation.
The associated counter type defines the time fetch strategy applied by the methods
setUnitsBefore()andsetUnitsAfter().Once the units are set, calling
elapsedTime()will return aDurationthat represents either:- the elapsed time between calls to
setUnitsBefore()andsetUnitsAfter()provided that both methods have been called before; or - the elapsed time between calls to
setUnitsBefore()andelapsedTime()ifsetUnitsAfter()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
- the elapsed time between calls to
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCounter.TypeEnumerates all supported counter types, defining a particular time fetch strategy for each of them.
-
Constructor Summary
Constructors Constructor Description Counter(Counter.Type type)Builds a Counter with a given type and default time unit.Counter(Counter.Type type, TimeUnit timeUnit)Builds a Counter with the given type and time unit.Counter(Counter.Type type, TimeUnit timeUnit, ConversionMode conversionMode)Builds a Counter with the given type, time unit, and conversion mode.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DurationelapsedTime()Returns the elapsed time.doubleelapsedTime(TimeUnit timeUnit)Returns the elapsed time in the specifiedTimeUnit.doubleelapsedTime(TimeUnit timeUnit, ConversionMode conversionMode)Returns the elapsed time, in a givenTimeUnit, with a customConversionMode.ConversionModegetConversionMode()Returns theConversionModeassociated with this counter.TimeUnitgetTimeUnit()Returns the time unit associated with this counter.Counter.TypegetType()Returns theCounter.Typeassociated with this counter.longgetUnitsAfter()Returns the value of theunitsAfterfield.longgetUnitsBefore()Returns the value of theunitsBeforefield.voidsetUnitsAfter(long unitsAfter)Populates theunitsAfterfield with an arbitrary value.voidsetUnitsBefore(long unitsBefore)Populates theunitsBeforefield with an arbitrary value.StringtoString()Returns a string representation of this object.
-
-
-
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, TimeUnit timeUnit)
Builds a Counter with the given type and time unit.- Parameters:
type- the type to set; cannot be nulltimeUnit- the time unit to set- Throws:
NullPointerException- if the specified type is null
-
Counter
public Counter(Counter.Type type, TimeUnit timeUnit, ConversionMode conversionMode)
Builds a Counter with the given type, time unit, and conversion mode.- Parameters:
type- the type to set; cannot be nulltimeUnit- the time unit to setconversionMode- theConversionModeto be applied- Throws:
NullPointerException- if the specified type is null- Since:
- 2.0.0
-
-
Method Detail
-
getUnitsBefore
public long getUnitsBefore()
Returns the value of theunitsBeforefield.- Returns:
- the value of the
unitsBeforefield
-
setUnitsBefore
public void setUnitsBefore(long unitsBefore)
Populates theunitsBeforefield with an arbitrary value.- Parameters:
unitsBefore- the units to be set
-
getUnitsAfter
public long getUnitsAfter()
Returns the value of theunitsAfterfield.- Returns:
- the value of the
unitsAfterfield
-
setUnitsAfter
public void setUnitsAfter(long unitsAfter)
Populates theunitsAfterfield with an arbitrary value.- Parameters:
unitsAfter- the units to be set
-
getType
public Counter.Type getType()
Returns theCounter.Typeassociated with this counter.- Returns:
- the
Counter.Typeassociated with this counter
-
getTimeUnit
public TimeUnit getTimeUnit()
Returns the time unit associated with this counter.- Returns:
- the time unit associated with this counter
-
getConversionMode
public ConversionMode getConversionMode()
Returns theConversionModeassociated with this counter.- Returns:
- the
ConversionModeassociated with this counter - Since:
- 2.0.0
-
elapsedTime
public Duration elapsedTime()
Returns the elapsed time.- Returns:
- the difference between
unitsBeforeandunitsAfter, if both units are set; or the difference betweenunitsBeforeand the current value retrieved by the counter's time source, ifunitsAfteris not set.
-
elapsedTime
public double elapsedTime(TimeUnit timeUnit)
Returns the elapsed time in the specifiedTimeUnit.- Parameters:
timeUnit- the time unit to which the elapsed time will be converted- Returns:
- the difference between
unitsBeforeandunitsAfter, if both units are set; or the difference betweenunitsBeforeand the current value retrieved by the counter's time source, ifunitsAfteris 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 givenTimeUnit, with a customConversionMode.- Parameters:
timeUnit- the time unit to which the elapsed time will be convertedconversionMode- theConversionModeto be used- Returns:
- the difference between
unitsBeforeandunitsAfter, if both units are set; or the difference betweenunitsBeforeand the current value retrieved by the counter's time source, ifunitsAfteris not set. The value is converted into the specified time unit applying the given conversion mode. - Since:
- 2.0.0
-
toString
public String toString()
Returns a string representation of this object.- Overrides:
toStringin classObject- See Also:
Object.toString()
-
-