@Contract(threading=IMMUTABLE) public class TimeValue extends Object implements Comparable<TimeValue>
long
time and a TimeUnit
.Modifier and Type | Field and Description |
---|---|
static TimeValue |
MAX_VALUE
A constant holding the maximum value a
TimeValue can have: Long.MAX_VALUE days. |
static TimeValue |
NEG_ONE_MILLISECOND
A negative one millisecond
TimeValue . |
static TimeValue |
NEG_ONE_SECOND
A negative one second
TimeValue . |
static TimeValue |
ZERO_MILLISECONDS
A zero milliseconds
TimeValue . |
Modifier and Type | Method and Description |
---|---|
static int |
asBoundInt(long value)
Returns the given
long value as an int where long values out of int range are returned as
Integer.MIN_VALUE and Integer.MAX_VALUE . |
int |
compareTo(TimeValue other)
Compares this object with the specified object for order.
|
long |
convert(TimeUnit targetTimeUnit) |
static <T extends TimeValue> |
defaultsTo(T timeValue,
T defaultValue)
Returns the given
timeValue if it is not null , if null then returns the given
defaultValue . |
static TimeValue |
defaultsToNegativeOneMillisecond(TimeValue timeValue)
|
static TimeValue |
defaultsToNegativeOneSecond(TimeValue timeValue)
|
static TimeValue |
defaultsToZeroMilliseconds(TimeValue timeValue)
|
TimeValue |
divide(long divisor)
Returns a TimeValue whose value is
(this / divisor) . |
TimeValue |
divide(long divisor,
TimeUnit targetTimeUnit)
Returns a TimeValue whose value is
(this / divisor) . |
boolean |
equals(Object obj)
Indicates whether some other object is "equal to" this one.
|
long |
getDuration() |
TimeUnit |
getTimeUnit() |
int |
hashCode()
Returns a hash code value for the object.
|
static boolean |
isNonNegative(TimeValue timeValue) |
static boolean |
isPositive(TimeValue timeValue) |
TimeValue |
min(TimeValue other) |
static TimeValue |
of(long duration,
TimeUnit timeUnit)
Creates a TimeValue.
|
static TimeValue |
ofDays(long days) |
static TimeValue |
ofHours(long hours) |
static TimeValue |
ofMicroseconds(long microseconds) |
static TimeValue |
ofMilliseconds(long millis) |
static TimeValue |
ofMinutes(long minutes) |
static TimeValue |
ofNanoseconds(long nanoseconds) |
static TimeValue |
ofSeconds(long seconds) |
static TimeValue |
parse(String value)
Parses a TimeValue in the format
<Long><SPACE><TimeUnit> , for example "1200 MILLISECONDS" . |
void |
sleep() |
void |
timedJoin(Thread thread) |
void |
timedWait(Object obj) |
long |
toDays() |
long |
toHours() |
long |
toMicroseconds() |
long |
toMilliseconds() |
int |
toMillisecondsIntBound() |
long |
toMinutes() |
long |
toNanoseconds() |
long |
toSeconds() |
int |
toSecondsIntBound() |
String |
toString()
Returns a string representation of the object.
|
Timeout |
toTimeout() |
public static final TimeValue MAX_VALUE
TimeValue
can have: Long.MAX_VALUE
days.public static final TimeValue NEG_ONE_MILLISECOND
TimeValue
.public static int asBoundInt(long value)
long
value as an int
where long values out of int range are returned as
Integer.MIN_VALUE
and Integer.MAX_VALUE
.
For example: TimeValue.asBoundInt(Long.MAX_VALUE)
returns Integer.MAX_VALUE
.
value
- a long value to convertInteger.MIN_VALUE
and Integer.MAX_VALUE
.public int compareTo(TimeValue other)
Comparable
The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception.)
The implementor must also ensure that the relation is transitive: (x.compareTo(y)>0 && y.compareTo(z)>0) implies x.compareTo(z)>0.
Finally, the implementor must ensure that x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.
It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."
In the foregoing description, the notation sgn(expression) designates the mathematical signum function, which is defined to return one of -1, 0, or 1 according to whether the value of expression is negative, zero or positive.
compareTo
in interface Comparable<TimeValue>
other
- the object to be compared.public long convert(TimeUnit targetTimeUnit)
public static <T extends TimeValue> T defaultsTo(T timeValue, T defaultValue)
timeValue
if it is not null
, if null
then returns the given
defaultValue
.timeValue
- may be null
defaultValue
- may be null
timeValue
or defaultValue
public static TimeValue defaultsToNegativeOneMillisecond(TimeValue timeValue)
timeValue
- may be null
timeValue
or NEG_ONE_SECOND
public static TimeValue defaultsToNegativeOneSecond(TimeValue timeValue)
timeValue
- may be null
timeValue
or NEG_ONE_SECOND
public static TimeValue defaultsToZeroMilliseconds(TimeValue timeValue)
timeValue
- may be null
timeValue
or ZERO_MILLISECONDS
public TimeValue divide(long divisor)
(this / divisor)
.divisor
- value by which this TimeValue is to be divided.this / divisor
ArithmeticException
- if divisor
is zero.public TimeValue divide(long divisor, TimeUnit targetTimeUnit)
(this / divisor)
.divisor
- value by which this TimeValue is to be divided.targetTimeUnit
- the target TimeUnitthis / divisor
ArithmeticException
- if divisor
is zero.public boolean equals(Object obj)
Object
The equals
method implements an equivalence relation on non-null object references:
x
, x.equals(x)
should return true
.
x
and y
,
x.equals(y)
should return true
if and only if y.equals(x)
returns
true
.
x
, y
, and
z
, if x.equals(y)
returns true
and y.equals(z)
returns
true
, then x.equals(z)
should return true
.
x
and y
, multiple
invocations of x.equals(y)
consistently return true
or consistently return
false
, provided no information used in equals
comparisons on the objects is
modified.
x
, x.equals(null)
should return
false
.
The equals
method for class Object
implements the most discriminating possible
equivalence relation on objects; that is, for any non-null reference values x
and
y
, this method returns true
if and only if x
and y
refer to the
same object (x == y
has the value true
).
Note that it is generally necessary to override the hashCode
method whenever this method
is overridden, so as to maintain the general contract for the hashCode
method, which
states that equal objects must have equal hash codes.
equals
in class Object
obj
- the reference object with which to compare.true
if this object is the same as the obj argument; false
otherwise.Object.hashCode()
,
HashMap
public long getDuration()
public TimeUnit getTimeUnit()
public int hashCode()
Object
HashMap
.
The general contract of hashCode
is:
hashCode
method must consistently return the same integer, provided no
information used in equals
comparisons on the object is modified. This integer need not
remain consistent from one execution of an application to another execution of the same
application.
equals(Object)
method, then calling the
hashCode
method on each of the two objects must produce the same integer result.
Object.equals(java.lang.Object)
method, then calling the hashCode
method on each of the two objects must produce distinct integer results. However, the programmer
should be aware that producing distinct integer results for unequal objects may improve the
performance of hash tables.
As much as is reasonably practical, the hashCode method defined by class Object
does
return distinct integers for distinct objects. (This is typically implemented by converting the
internal address of the object into an integer, but this implementation technique is not required
by the JavaTM programming language.)
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public static boolean isNonNegative(TimeValue timeValue)
public static boolean isPositive(TimeValue timeValue)
public static TimeValue of(long duration, TimeUnit timeUnit)
duration
- the time duration in the given timeUnit
.timeUnit
- the time unit for the given durarion.public static TimeValue ofDays(long days)
public static TimeValue ofHours(long hours)
public static TimeValue ofMicroseconds(long microseconds)
public static TimeValue ofMilliseconds(long millis)
public static TimeValue ofMinutes(long minutes)
public static TimeValue ofNanoseconds(long nanoseconds)
public static TimeValue ofSeconds(long seconds)
public static TimeValue parse(String value) throws java.text.ParseException
<Long><SPACE><TimeUnit>
, for example "1200 MILLISECONDS"
.
Parses:
"1200 MILLISECONDS"
." 1200 MILLISECONDS "
, spaces are ignored."1 MINUTE"
, singular units.value
- the TimeValue to parsejava.text.ParseException
- if the number cannot be parsedpublic void sleep() throws InterruptedException
InterruptedException
public void timedJoin(Thread thread) throws InterruptedException
InterruptedException
public void timedWait(Object obj) throws InterruptedException
InterruptedException
public long toDays()
public long toHours()
public long toMicroseconds()
public long toMilliseconds()
public int toMillisecondsIntBound()
public long toMinutes()
public long toNanoseconds()
public long toSeconds()
public int toSecondsIntBound()
public String toString()
Object
toString
method returns a
string that "textually represents" this object. The result should be a concise but informative
representation that is easy for a person to read. It is recommended that all subclasses override
this method.
The toString
method for class Object
returns a string consisting of the name of
the class of which the object is an instance, the at-sign character `@
', and the unsigned
hexadecimal representation of the hash code of the object. In other words, this method returns a
string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
public Timeout toTimeout()