|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
java.lang.Objecttime.Interval
public final class Interval
Interval is the implementation of an immutable time interval. A time interval represents a period of time between two instants.
An Interval contains all Instant that are greater than the start instant (inclusive) and lower than the end instant (exclusive), using the natural order of Instant (that is the chronological order). So the end instant is always greater than or equal to the start instant.
As an Interval can be seen as a sorted set of instant, it implements the java.util.SortedSet<Instant> interface. And as an Interval is immutable, the SortedSet is also immutable: every mutative method of the SortedSet interface throws an UnsupportedOperationException as the Collection interface allows it.
The Interval has not been designed to be iterated through Iterator, because it can contain a lot of Instant objects. For example, if an interval has a duration of one simple day, since the current implementation of Instant is millisecond accurate, it means that it contains 86 400 000 instants. Iterating such an interval will be likely unappropriate (but possible if really needed). And as Instant may be in the future event more accurate (nanoseconds), Interval may contain event more Instant, so you should also not use the size() method. The getDuration() is the appropriate method to know the duration of an Interval.
Interval have an natural order, which is consistent with equals: they are sorted by their starting intant first. If two intervals have the same starting instant, they are sorted by their ending instant. Other Comparator are also provided:
Instant| Field Summary | |
|---|---|
static java.util.Comparator<Interval> |
COMPARATOR_BY_ENDING_ONLY
Comparator that compares Intervals by their ending instant. |
static java.util.Comparator<Interval> |
COMPARATOR_BY_ENDING_THEN_STARTING
Comparator that compares Intervals by their ending instant first, then their starting instant. |
static java.util.Comparator<Interval> |
COMPARATOR_BY_STARTING_ONLY
Comparator that compares Intervals by their starting instant. |
static java.util.Comparator<Interval> |
COMPARATOR_BY_STARTING_THEN_ENDING
Comparator that compares Intervals by their starting instant first, then their ending instant (natural order of Interval). |
static Interval |
WHOLE_TIME
An interval that contains the whole representation of time. |
| Constructor Summary | |
|---|---|
Interval(Instant start,
Instant end)
Create a new Interval. |
|
| Method Summary | ||
|---|---|---|
boolean |
add(Instant o)
As Interval are immutable, throws a UnsupportedOperationException. |
|
boolean |
addAll(java.util.Collection<? extends Instant> c)
As Interval are immutable, throws a UnsupportedOperationException. |
|
void |
clear()
As Interval are immutable, throws a UnsupportedOperationException. |
|
java.util.Comparator<? super Instant> |
comparator()
Returns null, as Interval uses the natural order of Instant. |
|
int |
compareTo(Interval otherInterval)
Compare two interval chronogically. |
|
boolean |
contains(java.lang.Object obj)
Returns true if this set contains the specified Instant. |
|
boolean |
containsAll(java.util.Collection<?> c)
Returns true if this set contains all of the elements of the specified collection. |
|
boolean |
equals(java.lang.Object obj)
Compare two interval for equality. |
|
Instant |
first()
Returns the first (lowest) Instant currently in this Interval. |
|
long |
getDuration()
Compute the duration in milliseconds of the Interval. |
|
Instant |
getEnd()
Return the ending instant of this interval. |
|
Interval |
getIntersection(Interval otherInterval)
Return the intersection between this interval and another interval. |
|
Instant |
getStart()
Return the starting instant of this interval. |
|
int |
hashCode()
Returns a hash code value for this object. |
|
Interval |
headSet(Instant toInstant)
Return a new interval that contains all instants of this interval strictly less than toInstant. |
|
boolean |
isEmpty()
Test if the Interval is empty (has a duration of 0L). |
|
java.util.Iterator<Instant> |
iterator()
Returns an iterator over the instants in this interval. |
|
Instant |
last()
Returns the last (highest) Instant currently in this Interval. |
|
boolean |
remove(java.lang.Object o)
As Interval are immutable, throws a UnsupportedOperationException. |
|
boolean |
removeAll(java.util.Collection<?> c)
As Interval are immutable, throws a UnsupportedOperationException. |
|
boolean |
retainAll(java.util.Collection<?> c)
As Interval are immutable, throws a UnsupportedOperationException. |
|
int |
size()
Return the number of Instant in this Interval. |
|
Interval |
subSet(Instant fromInstant,
Instant toInstant)
Return a new interval that contains all instants of this interval greater than or equals to fromInstant and strictly less than toInstant. |
|
Interval |
tailSet(Instant fromInstant)
Return a new interval that contains all instants of this interval greater than or equals to fromInstant. |
|
java.lang.Object[] |
toArray()
Returns an array containing all of the instants in this interval. |
|
|
toArray(T[] a)
Returns an array containing all of the instants in this interval. |
|
java.lang.String |
toString()
Convert the interval in a String. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final java.util.Comparator<Interval> COMPARATOR_BY_STARTING_ONLY
public static final java.util.Comparator<Interval> COMPARATOR_BY_ENDING_ONLY
public static final java.util.Comparator<Interval> COMPARATOR_BY_STARTING_THEN_ENDING
public static final java.util.Comparator<Interval> COMPARATOR_BY_ENDING_THEN_STARTING
public static final Interval WHOLE_TIME
This interval contains all known Instant objects except the Apocalypse, as the end instant is not included in an interval. (The Apocalypse cannot belong to any interval).
It can be used with headSet() or tailSet(). For example:
Interval allBefore = WHOLE_TIME.headSet(limit);Represents
an Interval which contains all Instants strictly before limit.
And another example:
Interval allAfter = WHOLE_TIME.tailSet(limit);Represents
an Interval which contains all Instants after limit, including limit.
Instant.BIG_BANG,
Instant.APOCALYPSE| Constructor Detail |
|---|
public Interval(Instant start,
Instant end)
start - the starting instant of the new intervalend - the ending instant of the new interval
java.lang.NullPointerException - if end or start are null
java.lang.IllegalArgumentException - if the ending instant is before the starting instant.| Method Detail |
|---|
public long getDuration()
WHOLE_TIMEpublic java.lang.String toString()
toString in class java.lang.Objectpublic boolean equals(java.lang.Object obj)
equals in interface java.util.Collection<Instant>equals in interface java.util.Set<Instant>equals in class java.lang.Objectobj - the object to compare with.
public int hashCode()
hashCode in interface java.util.Collection<Instant>hashCode in interface java.util.Set<Instant>hashCode in class java.lang.Objectpublic int compareTo(Interval otherInterval)
The order is the same as the one used by COMPARATOR_BY_STARTING_THEN_ENDING. The interval are sorted by their starting instant first, then by their ending instant.
compareTo in interface java.lang.Comparable<Interval>otherInterval - the instant to be compared.
java.lang.NullPointerException - if otherInterval is null.COMPARATOR_BY_STARTING_THEN_ENDINGpublic Instant last()
If the interval is not empty, it differs from 1 milliseconds from the instant returned by getEnd(), as the Instant returned by last() belongs to this interval and the Instant returned by getEnd() does not.
last in interface java.util.SortedSet<Instant>java.util.NoSuchElementException - if the interval is empty.getEnd()public Instant first()
If the interval is not empty, it is the same instant as the one returned by the getStart() method.
first in interface java.util.SortedSet<Instant>java.util.NoSuchElementException - if the interval is empty.getStart()public int size()
As Instant are millisecond accurate, an Interval can contain a lot of Instant. If the Interval is larger than about 24 days, it will contains more than Integer.MAX_VALUE elements, so this method will return Integer.MAX_VALUE, as specified in Collection interface.
Since future implementations of Instant may be more accurate than millisecond, the value returned by this method may be even larger in the future. Please considere the usage of getDuration() instead.
size in interface java.util.Collection<Instant>size in interface java.util.Set<Instant>getDuration()public java.util.Iterator<Instant> iterator()
iterator in interface java.lang.Iterable<Instant>iterator in interface java.util.Collection<Instant>iterator in interface java.util.Set<Instant>public Interval tailSet(Instant fromInstant)
tailSet in interface java.util.SortedSet<Instant>fromInstant - starting instant (inclusive) of the new interval.
java.lang.NullPointerException - if fromInstant is null.
java.lang.IllegalArgumentException - if fromInstant is not in the interval.public Interval headSet(Instant toInstant)
headSet in interface java.util.SortedSet<Instant>toInstant - ending instant (exclusive) of the new interval.
java.lang.NullPointerException - if toInstant is null.
java.lang.IllegalArgumentException - if toInstant is not in the interval.
public Interval subSet(Instant fromInstant,
Instant toInstant)
subSet in interface java.util.SortedSet<Instant>fromInstant - starting instant (inclusive) of the new interval.toInstant - ending instant (exclusive) of the new interval.
java.lang.NullPointerException - if fromInstant or toInstant are null.
java.lang.IllegalArgumentException - if fromInstant or toInstant are not in the interval.
java.lang.IllegalArgumentException - if toInstant is greater than fromInstant.public java.util.Comparator<? super Instant> comparator()
comparator in interface java.util.SortedSet<Instant>public Instant getStart()
first()public Instant getEnd()
getEnding().millisBetween(last())==1Lis always true,
last()public void clear()
clear in interface java.util.Collection<Instant>clear in interface java.util.Set<Instant>public boolean removeAll(java.util.Collection<?> c)
removeAll in interface java.util.Collection<Instant>removeAll in interface java.util.Set<Instant>public boolean retainAll(java.util.Collection<?> c)
retainAll in interface java.util.Collection<Instant>retainAll in interface java.util.Set<Instant>public boolean addAll(java.util.Collection<? extends Instant> c)
addAll in interface java.util.Collection<Instant>addAll in interface java.util.Set<Instant>public boolean remove(java.lang.Object o)
remove in interface java.util.Collection<Instant>remove in interface java.util.Set<Instant>public boolean add(Instant o)
add in interface java.util.Collection<Instant>add in interface java.util.Set<Instant>public <T> T[] toArray(T[] a)
As the interval may contain a lot of instants, the returned array may be very big, so this method should be used very carefully.
toArray in interface java.util.Collection<Instant>toArray in interface java.util.Set<Instant>java.lang.OutOfMemoryError - if the interval is too big.public java.lang.Object[] toArray()
As the interval may contain a lot of instants, the returned array may be very big, so this method should be used very carefully.
toArray in interface java.util.Collection<Instant>toArray in interface java.util.Set<Instant>java.lang.OutOfMemoryError - if the interval is too big.public boolean containsAll(java.util.Collection<?> c)
containsAll in interface java.util.Collection<Instant>containsAll in interface java.util.Set<Instant>c - collection to be checked for containment in this interval.
java.lang.NullPointerException - if c is null.
java.lang.NullPointerException - if c contains one or more null elements.
java.lang.ClassCastException - if c contains one or more elements that are
not Instant.public boolean contains(java.lang.Object obj)
contains in interface java.util.Collection<Instant>contains in interface java.util.Set<Instant>java.lang.NullPointerException - if obj is null.
java.lang.ClassCastException - if obj is not an Instant.public boolean isEmpty()
isEmpty in interface java.util.Collection<Instant>isEmpty in interface java.util.Set<Instant>public Interval getIntersection(Interval otherInterval)
java.lang.NullPointerException - if otherInterval is null.
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||