Package net.obvj.performetrics
Class Counter
- java.lang.Object
-
- net.obvj.performetrics.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()
andsetUnitsAfter()
.Once the units are set, calling
elapsedTime()
will return aDuration
that 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Counter.Type
Enumerates 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, 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 Duration
elapsedTime()
Returns the elapsed time.double
elapsedTime(TimeUnit timeUnit)
Returns the elapsed time in the specifiedTimeUnit
.double
elapsedTime(TimeUnit timeUnit, ConversionMode conversionMode)
Returns the elapsed time, in a givenTimeUnit
, with a customConversionMode
.ConversionMode
getConversionMode()
Returns theConversionMode
associated with this counter.Counter.Type
getType()
Returns theCounter.Type
associated with this counter.long
getUnitsAfter()
Returns the value of theunitsAfter
field.long
getUnitsBefore()
Returns the value of theunitsBefore
field.void
setUnitsAfter(long unitsAfter)
Populates theunitsAfter
field with an arbitrary value.void
setUnitsBefore(long unitsBefore)
Populates theunitsBefore
field with an arbitrary value.String
toString()
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, ConversionMode conversionMode)
Builds a Counter with the given type, time unit, and conversion mode.- Parameters:
type
- the type to set; cannot be nullconversionMode
- theConversionMode
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 theunitsBefore
field.- Returns:
- the value of the
unitsBefore
field
-
setUnitsBefore
public void setUnitsBefore(long unitsBefore)
Populates theunitsBefore
field with an arbitrary value.- Parameters:
unitsBefore
- the units to be set
-
getUnitsAfter
public long getUnitsAfter()
Returns the value of theunitsAfter
field.- Returns:
- the value of the
unitsAfter
field
-
setUnitsAfter
public void setUnitsAfter(long unitsAfter)
Populates theunitsAfter
field with an arbitrary value.- Parameters:
unitsAfter
- the units to be set
-
getType
public Counter.Type getType()
Returns theCounter.Type
associated with this counter.- Returns:
- the
Counter.Type
associated with this counter
-
getConversionMode
public ConversionMode getConversionMode()
Returns theConversionMode
associated with this counter.- Returns:
- the
ConversionMode
associated with this counter - Since:
- 2.0.0
-
elapsedTime
public Duration elapsedTime()
Returns the elapsed time.- Returns:
- the difference between
unitsBefore
andunitsAfter
, if both units are set; or the difference betweenunitsBefore
and the current value retrieved by the counter's time source, ifunitsAfter
is 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
unitsBefore
andunitsAfter
, if both units are set; or the difference betweenunitsBefore
and the current value retrieved by the counter's time source, ifunitsAfter
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 givenTimeUnit
, with a customConversionMode
.- Parameters:
timeUnit
- the time unit to which the elapsed time will be convertedconversionMode
- theConversionMode
to be used- Returns:
- the difference between
unitsBefore
andunitsAfter
, if both units are set; or the difference betweenunitsBefore
and the current value retrieved by the counter's time source, ifunitsAfter
is 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:
toString
in classObject
- See Also:
Object.toString()
-
-