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 a given
TimeUnit. |
double |
elapsedTime(TimeUnit timeUnit,
ConversionMode conversionMode)
Returns the elapsed time, in a given
TimeUnit, with a custom
ConversionMode. |
ConversionMode |
getConversionMode() |
TimeUnit |
getTimeUnit() |
Counter.Type |
getType() |
long |
getUnitsAfter() |
long |
getUnitsBefore() |
void |
setUnitsAfter()
Populates the
unitsAfter field with the value retrieved by the time fetching
mode defined by this counter's type. |
void |
setUnitsAfter(long unitsAfter) |
void |
setUnitsBefore()
Populates the
unitsBefore field with the value retrieved by the time fetching
mode defined by this counter's type. |
void |
setUnitsBefore(long unitsBefore) |
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()
public void setUnitsBefore(long unitsBefore)
unitsBefore - the units to be setpublic long getUnitsAfter()
public void setUnitsAfter(long unitsAfter)
unitsAfter - the units to be setpublic Counter.Type getType()
public TimeUnit getTimeUnit()
public ConversionMode getConversionMode()
ConversionMode used by this counterpublic void setUnitsBefore()
unitsBefore field with the value retrieved by the time fetching
mode defined by this counter's type.public void setUnitsAfter()
unitsAfter field with the value retrieved by the time fetching
mode defined by this counter's type.public Duration elapsedTime()
unitsBefore and unitsAfter, if both
units are set; or the difference between unitsBefore and the current
value retrieved by the counter's default data fetch mode, 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 default data fetch mode, if unitsAfter
is not set. The value is converted to the given time unit using 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 default data fetch mode, if unitsAfter
is not set. The value converted to the given time unit using the given
conversion mode.public String toString()
toString in class ObjectObject.toString()Copyright © 2021. All rights reserved.