public final class Split extends Object implements HasAttributes, AutoCloseable
Stopwatch.start()
and the measurement is ended using stop()
method on this object. Split will return 0 as the result
if the related Stopwatch was disabled when the Split was obtained. The Split can be stopped in any other thread.
Split measures real time (based on SimonClock.nanoTime()
), it does not measure CPU time. Split can be garbage collected
and no resource leak occurs if it is not stopped, however Stopwatch's active counter (Stopwatch.getActive()
)
will be stay incremented.
Split can never be running (isRunning()
) if it is disabled. Enabled split is running until it is stopped.
Stopped split (not running) will never again be running. Split never changes enabled flag after creation.
Split implements AutoCloseable
hence it can be used in try-with-resource construction.Stopwatch
Modifier and Type | Field and Description |
---|---|
static String |
ATTR_EFFECTIVE_STOPWATCH
Attribute name under which effectively used stopwatch is stored if the split was stopped with
stop(String) . |
static Split |
DISABLED
Disabled split (implies not running) for cases where monitoring is disabled and
null value is not an option. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Allows to use Split as a resource in try-with-resource construct.
|
static Split |
create(long nanos)
Creates simulated non-running Split that took specific time in nanos.
|
static Split |
create(long nanos,
SimonClock clock)
Creates simulated non-running Split that took specific time in nanos.
|
Object |
getAttribute(String name)
Returns the value of the named attribute as an Object, or
null if no attribute of
the given name exists. |
<T> T |
getAttribute(String name,
Class<T> clazz)
Returns the value of the named attribute typed to the specified class, or
null if no attribute of
the given name exists. |
Iterator<String> |
getAttributeNames()
Returns an Iterator containing the names of the attributes available to this Split.
|
Map<String,Object> |
getCopyAsSortedMap()
Returns copy of attributes as a sorted map, this can be used further for operations like
toString . |
long |
getStart()
Returns start nano timer value or 0 if the Split is not enabled (started for disabled Stopwatch).
|
long |
getStartMillis()
Returns millis timestamp when this Split was started - or 0 if the Split is not enabled (started for disabled Stopwatch).
|
Stopwatch |
getStopwatch()
Returns the stopwatch that this split is running for.
|
boolean |
isEnabled()
Returns true if this split was created from enabled Simon or via
start() . |
boolean |
isRunning()
Returns true if this split is still running (
stop() has not been called yet). |
String |
presentRunningFor()
Returns printable form of how long this split was running for.
|
void |
removeAttribute(String name)
Removes an attribute from this Split.
|
long |
runningFor()
Returns the current running nano-time from the start to the method call or the total split time
if the Split has been stopped already.
|
void |
setAttribute(String name,
Object value)
Stores an attribute in this Split.
|
static Split |
start()
Creates a new Split for direct use without
Stopwatch ("anonymous split") based on system time. |
static Split |
start(SimonClock clock)
Creates a new Split for direct use without
Stopwatch ("anonymous split") based on specified SimonClock . |
Split |
stop()
Stops the split, updates the stopwatch and returns this.
|
Split |
stop(String subSimon)
Stops the split, updates the sub-stopwatch specified by parameter and returns this.
|
String |
toString()
Returns information about this Split, if it's running, name of the related Stopwatch and split's time.
|
public static final Split DISABLED
null
value is not an option.public static final String ATTR_EFFECTIVE_STOPWATCH
stop(String)
.public static Split start(SimonClock clock)
Stopwatch
("anonymous split") based on specified SimonClock
.
Stop will not update any Stopwatch, value can be added to any chosen Stopwatch using
Stopwatch.addSplit(Split)
- even in conjunction with stop()
like this:
Split split = Split.start(); ... stopwatch.addSplit(split.stop());If the split is not needed afterwards (subject to garbage collection) calling
stop()
is not necessary:
Split split = Split.start(); ... stopwatch.addSplit(split);No callbacks are called for this Split.
clock
- Clock for this Splitpublic static Split start()
Stopwatch
("anonymous split") based on system time.
Equivalent of Split.start(Clock.SYSTEM)
.start(SimonClock)
public static Split create(long nanos, SimonClock clock)
clock.nanoTime()
.nanos
- Split's total time in nanosclock
- Clock for this Splitpublic static Split create(long nanos)
SimonClock.SYSTEM
.nanos
- Split's total time in nanoscreate(long, SimonClock)
public Stopwatch getStopwatch()
null
for anonymous splits (directly created).null
public Split stop()
runningFor()
for the same resultpublic Split stop(String subSimon)
stop()
(or this stop with null
argument)
results in Callback.onStopwatchStop(Split, StopwatchSample)
callback method being invoked,
if sub-simon is affected then Callback.onStopwatchAdd(Stopwatch, Split, StopwatchSample)
is called instead.
If the split was obtained from disabled Stopwatch, this method does not update sub-simon even if it is enabled, because
split itself is disabled as well. If split is enabled, but sub-simon is disabled, the latter is not updated.subSimon
- name of the sub-stopwatch (hierarchy delimiter is added automatically) - if null
it behaves exactly like stop()
public long runningFor()
public String presentRunningFor()
public boolean isEnabled()
start()
.public boolean isRunning()
stop()
has not been called yet).
Returns false for disabled Split.public long getStart()
public long getStartMillis()
public void setAttribute(String name, Object value)
setAttribute
in interface HasAttributes
name
- a String specifying the name of the attributevalue
- the Object to be storedpublic Object getAttribute(String name)
null
if no attribute of
the given name exists.getAttribute
in interface HasAttributes
name
- a String specifying the name of the attributenull
if the attribute does not existpublic <T> T getAttribute(String name, Class<T> clazz)
null
if no attribute of
the given name exists.getAttribute
in interface HasAttributes
name
- a String specifying the name of the attributenull
if the attribute does not existpublic void removeAttribute(String name)
removeAttribute
in interface HasAttributes
name
- a String specifying the name of the attribute to removepublic Iterator<String> getAttributeNames()
getAttributeNames
in interface HasAttributes
public Map<String,Object> getCopyAsSortedMap()
HasAttributes
toString
.getCopyAsSortedMap
in interface HasAttributes
public void close()
stop()
internally.close
in interface AutoCloseable
Copyright © 2019. All rights reserved.