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() and setUnitsAfter().
Once the units are set, calling elapsedTime() will return a Duration
that represents either:
setUnitsBefore() and
setUnitsAfter() provided that both methods have been called before; orsetUnitsBefore() and
elapsedTime() if setUnitsAfter() was not called
Optionally, call elapsedTime(TimeUnit) to return the total elapsed time in a
specific time unit.
Counter.Type,
Duration,
ConversionMode| Modifier and Type | Class and Description |
|---|---|
static class |
Counter.Type
Enumerates all supported counter types, defining a particular time fetch strategy for
each of them.
|
| Constructor and 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.
|
| Modifier and Type | Method and Description |
|---|---|
Duration |
elapsedTime()
Returns the elapsed time.
|
double |
elapsedTime(TimeUnit timeUnit)
Returns the elapsed time in the specified
TimeUnit. |
double |
elapsedTime(TimeUnit timeUnit,
ConversionMode conversionMode)
Returns the elapsed time, in a given
TimeUnit, with a custom
ConversionMode. |
ConversionMode |
getConversionMode()
Returns the
ConversionMode associated with this counter. |
TimeUnit |
getTimeUnit()
Returns the time unit associated with this counter.
|
Counter.Type |
getType()
Returns the
Counter.Type associated with this counter. |
long |
getUnitsAfter()
Returns the value of the
unitsAfter field. |
long |
getUnitsBefore()
Returns the value of the
unitsBefore field. |
void |
setUnitsAfter(long unitsAfter)
Populates the
unitsAfter field with an arbitrary value. |
void |
setUnitsBefore(long unitsBefore)
Populates the
unitsBefore field with an arbitrary value. |
String |
toString()
Returns a string representation of this object.
|
public Counter(Counter.Type type)
type - the type to set; cannot be nullNullPointerException - if the specified type is nullpublic Counter(Counter.Type type, TimeUnit timeUnit)
type - the type to set; cannot be nulltimeUnit - the time unit to setNullPointerException - if the specified type is nullpublic Counter(Counter.Type type, TimeUnit timeUnit, ConversionMode conversionMode)
type - the type to set; cannot be nulltimeUnit - the time unit to setconversionMode - the ConversionMode to be appliedNullPointerException - if the specified type is nullpublic long getUnitsBefore()
unitsBefore field.unitsBefore fieldpublic void setUnitsBefore(long unitsBefore)
unitsBefore field with an arbitrary value.unitsBefore - the units to be setpublic long getUnitsAfter()
unitsAfter field.unitsAfter fieldpublic void setUnitsAfter(long unitsAfter)
unitsAfter field with an arbitrary value.unitsAfter - the units to be setpublic Counter.Type getType()
Counter.Type associated with this counter.Counter.Type associated with this counterpublic TimeUnit getTimeUnit()
public ConversionMode getConversionMode()
ConversionMode associated with this counter.ConversionMode associated with this counterpublic Duration elapsedTime()
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.public double elapsedTime(TimeUnit timeUnit)
TimeUnit.timeUnit - the time unit to which the elapsed time will be convertedunitsBefore 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.public double elapsedTime(TimeUnit timeUnit, ConversionMode conversionMode)
TimeUnit, with a custom
ConversionMode.timeUnit - the time unit to which the elapsed time will be convertedconversionMode - the ConversionMode to be usedunitsBefore 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.public String toString()
toString in class ObjectObject.toString()Copyright © 2023. All rights reserved.