edu.stanford.nlp.util
Class Interval<E extends Comparable<E>>

java.lang.Object
  extended by edu.stanford.nlp.util.Pair<E,E>
      extended by edu.stanford.nlp.util.Interval<E>
All Implemented Interfaces:
HasInterval<E>, Serializable, Comparable<Pair<E,E>>

public class Interval<E extends Comparable<E>>
extends Pair<E,E>
implements HasInterval<E>

Represents a interval of a generic type E that is comparable. An interval is an ordered pair where the first element is less than the second. Only full intervals are currently supported (i.e. both endpoints has to be specified - cannot be null) Provides functions for computing relationships between intervals. For flags that indicate relationship between two intervals, the following convention is used: SS = relationship between start of first interval and start of second interval SE = relationship between start of first interval and end of second interval ES = relationship between end of first interval and start of second interval EE = relationship between end of first interval and end of second interval

Author:
Angel Chang
See Also:
Serialized Form

Nested Class Summary
static class Interval.RelType
          RelType gives the basic types of relations between two intervals
 
Field Summary
static int INTERVAL_OPEN_BEGIN
          Flag indicating that an interval's begin point is not inclusive (by default, begin points are inclusive)
static int INTERVAL_OPEN_END
          Flag indicating that an interval's end point is not inclusive (by default, begin points are inclusive)
protected static int REL_FLAGS_AFTER
           
protected static int REL_FLAGS_BEFORE
           
static int REL_FLAGS_EE_AFTER
          The first interval ends after the second ends
static int REL_FLAGS_EE_BEFORE
          The first interval ends before the second ends
static int REL_FLAGS_EE_SAME
          Both intervals have the same end point
protected static int REL_FLAGS_EE_SHIFT
           
static int REL_FLAGS_EE_UNKNOWN
          The relationship between the end points of the two intervals is unknown (used for fuzzy intervals)
static int REL_FLAGS_ES_AFTER
          The end point of the first interval is after the start point of the second interval (the two intervals overlap)
static int REL_FLAGS_ES_BEFORE
          The end point of the first interval is before the start point of the second interval (the first interval is before the second)
static int REL_FLAGS_ES_SAME
          The end point of the first interval is the same as the start point of the second interval (the first interval is before the second)
protected static int REL_FLAGS_ES_SHIFT
           
static int REL_FLAGS_ES_UNKNOWN
          The relationship between the end point of the first interval and the start point of the second interval is unknown (used for fuzzy intervals)
static int REL_FLAGS_INTERVAL_AFTER
          The first interval is entirely after the second interval (the start of the first interval happens after the end of the second)
static int REL_FLAGS_INTERVAL_ALMOST_AFTER
           
static int REL_FLAGS_INTERVAL_ALMOST_BEFORE
           
static int REL_FLAGS_INTERVAL_ALMOST_SAME
           
static int REL_FLAGS_INTERVAL_BEFORE
          The first interval is entirely before the second interval (the end of the first interval happens before the start of the second)
static int REL_FLAGS_INTERVAL_CONTAIN
          The first interval contains the second interval.
static int REL_FLAGS_INTERVAL_FUZZY
           
static int REL_FLAGS_INTERVAL_INSIDE
          The first interval is inside the second interval.
static int REL_FLAGS_INTERVAL_OVERLAP
          The first interval overlaps with the second interval.
static int REL_FLAGS_INTERVAL_SAME
          The intervals are the same (have the same start and end points).
static int REL_FLAGS_INTERVAL_UNKNOWN
          It is uncertain what the relationship between the two intervals are...
protected static int REL_FLAGS_SAME
           
static int REL_FLAGS_SE_AFTER
          The start point of the first interval is after the end point of the second interval (the second interval is before the first)
static int REL_FLAGS_SE_BEFORE
          The start point of the first interval is before the end point of the second interval (the two intervals overlap)
static int REL_FLAGS_SE_SAME
          The start point of the first interval is the same as the end point of the second interval (the second interval is before the first)
protected static int REL_FLAGS_SE_SHIFT
           
static int REL_FLAGS_SE_UNKNOWN
          The relationship between the start point of the first interval and the end point of the second interval is unknown (used for fuzzy intervals)
static int REL_FLAGS_SS_AFTER
          The first interval starts after the second starts
static int REL_FLAGS_SS_BEFORE
          The first interval starts before the second starts
static int REL_FLAGS_SS_SAME
          Both intervals have the same start point
protected static int REL_FLAGS_SS_SHIFT
           
static int REL_FLAGS_SS_UNKNOWN
          The relationship between the start points of the two intervals is unknown (used for fuzzy intervals)
protected static int REL_FLAGS_UNKNOWN
           
 
Fields inherited from class edu.stanford.nlp.util.Pair
first, second
 
Fields inherited from interface edu.stanford.nlp.util.HasInterval
OFFSET_COMPARATOR
 
Constructor Summary
protected Interval(E a, E b, int flags)
           
 
Method Summary
protected  int addIntervalRelationFlags(int flags, boolean checkFuzzy)
           
static boolean checkFlagExclusiveSet(int flags, int flag, int mask)
          Utility function to check if a particular flag is set exclusively given a particular set of flags and a mask
static boolean checkFlagSet(int flags, int flag)
          Utility function to check if a particular flag is set given a particular set of flags
static boolean checkMultipleBitSet(int flags)
          Utility function to check if multiple bits are set for flags
 int compareIntervalOrder(Interval<E> other)
          Returns order of another interval compared to this one
 boolean contains(E p)
          Checks whether the point p is contained inside this interval
 boolean equals(Object o)
           
 Interval expand(Interval<E> other)
          Returns (smallest) interval that contains both this and the other interval
static int extractRelationSubflags(int flags, int shift)
           
 E getBegin()
          Returns the start point
 E getEnd()
          Returns the end point
 Interval<E> getInterval()
          Returns this interval
 Interval.RelType getRelation(Interval<E> other)
          Returns the relationship of this interval to the other interval The most specific relationship from the following is returned.
 int getRelationFlags(Interval<E> other)
          Return set of flags indicating possible relationships between this interval and another interval.
 int hashCode()
           
 boolean includesBegin()
          Returns whether the start endpoint is included in the interval
 boolean includesEnd()
          Returns whether the end endpoint is included in the interval
 Interval intersect(Interval<E> other)
          Returns interval that is the intersection of this and the other interval Returns null if intersect is null
 boolean isIntervalComparable(Interval<E> other)
          Checks whether this interval is comparable with another interval comes before or after
protected static
<E extends Comparable<E>>
E
max(E a, E b)
           
protected static
<E extends Comparable<E>>
E
min(E a, E b)
           
 boolean overlaps(Interval<E> other)
          Check whether this interval overlaps with the other interval (i.e.
static
<E extends Comparable<E>>
Interval<E>
toInterval(E a, E b)
          Create an interval with the specified endpoints in the specified order, Returns null if a does not come before b (invalid interval)
static
<E extends Comparable<E>>
Interval<E>
toInterval(E a, E b, int flags)
          Create an interval with the specified endpoints in the specified order, using the specified flags.
protected  int toRelFlags(int comp, int shift)
           
static
<E extends Comparable<E>>
Interval<E>
toValidInterval(E a, E b)
          Create an interval with the specified endpoints, reordering them as needed
static
<E extends Comparable<E>>
Interval<E>
toValidInterval(E a, E b, int flags)
          Create an interval with the specified endpoints, reordering them as needed, using the specified flags
 
Methods inherited from class edu.stanford.nlp.util.Pair
compareTo, first, internedStringPair, makePair, readStringPair, save, second, setFirst, setSecond, stringIntern, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

INTERVAL_OPEN_BEGIN

public static int INTERVAL_OPEN_BEGIN
Flag indicating that an interval's begin point is not inclusive (by default, begin points are inclusive)


INTERVAL_OPEN_END

public static int INTERVAL_OPEN_END
Flag indicating that an interval's end point is not inclusive (by default, begin points are inclusive)


REL_FLAGS_SAME

protected static final int REL_FLAGS_SAME
See Also:
Constant Field Values

REL_FLAGS_BEFORE

protected static final int REL_FLAGS_BEFORE
See Also:
Constant Field Values

REL_FLAGS_AFTER

protected static final int REL_FLAGS_AFTER
See Also:
Constant Field Values

REL_FLAGS_UNKNOWN

protected static final int REL_FLAGS_UNKNOWN
See Also:
Constant Field Values

REL_FLAGS_SS_SHIFT

protected static final int REL_FLAGS_SS_SHIFT
See Also:
Constant Field Values

REL_FLAGS_SE_SHIFT

protected static final int REL_FLAGS_SE_SHIFT
See Also:
Constant Field Values

REL_FLAGS_ES_SHIFT

protected static final int REL_FLAGS_ES_SHIFT
See Also:
Constant Field Values

REL_FLAGS_EE_SHIFT

protected static final int REL_FLAGS_EE_SHIFT
See Also:
Constant Field Values

REL_FLAGS_SS_SAME

public static final int REL_FLAGS_SS_SAME
Both intervals have the same start point
 |---- interval 1 ----?
 |---- interval 2 ----?
 

See Also:
Constant Field Values

REL_FLAGS_SS_BEFORE

public static final int REL_FLAGS_SS_BEFORE
The first interval starts before the second starts
 |---- interval 1 ----?
     |---- interval 2 ----?

 or

 |-- interval 1 --?
                       |---- interval 2 ----?
 

See Also:
Constant Field Values

REL_FLAGS_SS_AFTER

public static final int REL_FLAGS_SS_AFTER
The first interval starts after the second starts
     |---- interval 1 ----?
 |---- interval 2 ----?

 or

                       |---- interval 1 ----?
 |-- interval 2 --?
 

See Also:
Constant Field Values

REL_FLAGS_SS_UNKNOWN

public static final int REL_FLAGS_SS_UNKNOWN
The relationship between the start points of the two intervals is unknown (used for fuzzy intervals)

See Also:
Constant Field Values

REL_FLAGS_SE_SAME

public static final int REL_FLAGS_SE_SAME
The start point of the first interval is the same as the end point of the second interval (the second interval is before the first)
                     |---- interval 1 ----?
 ?---- interval 2 ---|
 

See Also:
Constant Field Values

REL_FLAGS_SE_BEFORE

public static final int REL_FLAGS_SE_BEFORE
The start point of the first interval is before the end point of the second interval (the two intervals overlap)
                 |---- interval 1 ----?
 ?---- interval 2 ---|
 

See Also:
Constant Field Values

REL_FLAGS_SE_AFTER

public static final int REL_FLAGS_SE_AFTER
The start point of the first interval is after the end point of the second interval (the second interval is before the first)
                      |---- interval 1 ---?
 ?-- interval 2 ---|
 

See Also:
Constant Field Values

REL_FLAGS_SE_UNKNOWN

public static final int REL_FLAGS_SE_UNKNOWN
The relationship between the start point of the first interval and the end point of the second interval is unknown (used for fuzzy intervals)

See Also:
Constant Field Values

REL_FLAGS_ES_SAME

public static final int REL_FLAGS_ES_SAME
The end point of the first interval is the same as the start point of the second interval (the first interval is before the second)
 ?---- interval 1 ---|
                     |---- interval 2 ----?
 

See Also:
Constant Field Values

REL_FLAGS_ES_BEFORE

public static final int REL_FLAGS_ES_BEFORE
The end point of the first interval is before the start point of the second interval (the first interval is before the second)
 ?-- interval 1 ---|
                      |---- interval 2 ---?
 

See Also:
Constant Field Values

REL_FLAGS_ES_AFTER

public static final int REL_FLAGS_ES_AFTER
The end point of the first interval is after the start point of the second interval (the two intervals overlap)
 ?---- interval 1 ---|
                 |---- interval 2 ----?
 

See Also:
Constant Field Values

REL_FLAGS_ES_UNKNOWN

public static final int REL_FLAGS_ES_UNKNOWN
The relationship between the end point of the first interval and the start point of the second interval is unknown (used for fuzzy intervals)

See Also:
Constant Field Values

REL_FLAGS_EE_SAME

public static final int REL_FLAGS_EE_SAME
Both intervals have the same end point
 ?---- interval 1 ----|
 ?---- interval 2 ----|
 

See Also:
Constant Field Values

REL_FLAGS_EE_BEFORE

public static final int REL_FLAGS_EE_BEFORE
The first interval ends before the second ends
 ?---- interval 1 ----|
     ?---- interval 2 ----|

 or

 ?-- interval 1 --|
                       ?---- interval 2 ----|
 

See Also:
Constant Field Values

REL_FLAGS_EE_AFTER

public static final int REL_FLAGS_EE_AFTER
The first interval ends after the second ends
     ?---- interval 1 ----|
 ?---- interval 2 ----|

 or

                       ?---- interval 1 ----|
 ?-- interval 2 --|
 

See Also:
Constant Field Values

REL_FLAGS_EE_UNKNOWN

public static final int REL_FLAGS_EE_UNKNOWN
The relationship between the end points of the two intervals is unknown (used for fuzzy intervals)

See Also:
Constant Field Values

REL_FLAGS_INTERVAL_SAME

public static final int REL_FLAGS_INTERVAL_SAME
The intervals are the same (have the same start and end points). When this flag is set, OVERLAP, INSIDE, and CONTAIN should also be set.
 |---- interval 1 ----|
 |---- interval 2 ----|
 

See Also:
Constant Field Values

REL_FLAGS_INTERVAL_BEFORE

public static final int REL_FLAGS_INTERVAL_BEFORE
The first interval is entirely before the second interval (the end of the first interval happens before the start of the second)
 ?---- interval 1 ----|
                          |---- interval 2 ----?
 

See Also:
Constant Field Values

REL_FLAGS_INTERVAL_AFTER

public static final int REL_FLAGS_INTERVAL_AFTER
The first interval is entirely after the second interval (the start of the first interval happens after the end of the second)
                          |---- interval 1 ----?
 ?---- interval 2 ----|
 

See Also:
Constant Field Values

REL_FLAGS_INTERVAL_OVERLAP

public static final int REL_FLAGS_INTERVAL_OVERLAP
The first interval overlaps with the second interval.

See Also:
Constant Field Values

REL_FLAGS_INTERVAL_INSIDE

public static final int REL_FLAGS_INTERVAL_INSIDE
The first interval is inside the second interval. When this flag is set, OVERLAP should also be set.
          |---- interval 1 ----|
       |---- interval 2 -----------|
 

See Also:
Constant Field Values

REL_FLAGS_INTERVAL_CONTAIN

public static final int REL_FLAGS_INTERVAL_CONTAIN
The first interval contains the second interval. When this flag is set, OVERLAP should also be set.
       |---- interval 1 -----------|
          |---- interval 2 ----|
 

See Also:
Constant Field Values

REL_FLAGS_INTERVAL_UNKNOWN

public static final int REL_FLAGS_INTERVAL_UNKNOWN
It is uncertain what the relationship between the two intervals are...

See Also:
Constant Field Values

REL_FLAGS_INTERVAL_ALMOST_SAME

public static final int REL_FLAGS_INTERVAL_ALMOST_SAME
See Also:
Constant Field Values

REL_FLAGS_INTERVAL_ALMOST_BEFORE

public static final int REL_FLAGS_INTERVAL_ALMOST_BEFORE
See Also:
Constant Field Values

REL_FLAGS_INTERVAL_ALMOST_AFTER

public static final int REL_FLAGS_INTERVAL_ALMOST_AFTER
See Also:
Constant Field Values

REL_FLAGS_INTERVAL_FUZZY

public static final int REL_FLAGS_INTERVAL_FUZZY
See Also:
Constant Field Values
Constructor Detail

Interval

protected Interval(E a,
                   E b,
                   int flags)
Method Detail

toInterval

public static <E extends Comparable<E>> Interval<E> toInterval(E a,
                                                               E b)
Create an interval with the specified endpoints in the specified order, Returns null if a does not come before b (invalid interval)

Type Parameters:
E - type of the interval endpoints
Parameters:
a - start endpoints
b - end endpoint
Returns:
Interval with endpoints in specified order, null if a does not come before b

toInterval

public static <E extends Comparable<E>> Interval<E> toInterval(E a,
                                                               E b,
                                                               int flags)
Create an interval with the specified endpoints in the specified order, using the specified flags. Returns null if a does not come before b (invalid interval)

Type Parameters:
E - type of the interval endpoints
Parameters:
a - start endpoints
b - end endpoint
flags - flags characterizing the interval
Returns:
Interval with endpoints in specified order, null if a does not come before b

toValidInterval

public static <E extends Comparable<E>> Interval<E> toValidInterval(E a,
                                                                    E b)
Create an interval with the specified endpoints, reordering them as needed

Type Parameters:
E - type of the interval endpoints
Parameters:
a - one of the endpoints
b - the other endpoint
Returns:
Interval with endpoints re-ordered as needed

toValidInterval

public static <E extends Comparable<E>> Interval<E> toValidInterval(E a,
                                                                    E b,
                                                                    int flags)
Create an interval with the specified endpoints, reordering them as needed, using the specified flags

Type Parameters:
E - type of the interval endpoints
Parameters:
a - one of the endpoints
b - the other endpoint
flags - flags characterizing the interval
Returns:
Interval with endpoints re-ordered as needed

getInterval

public Interval<E> getInterval()
Returns this interval

Specified by:
getInterval in interface HasInterval<E extends Comparable<E>>
Returns:
this interval

getBegin

public E getBegin()
Returns the start point

Returns:
the start point of this interval

getEnd

public E getEnd()
Returns the end point

Returns:
the end point of this interval

max

protected static <E extends Comparable<E>> E max(E a,
                                                 E b)

min

protected static <E extends Comparable<E>> E min(E a,
                                                 E b)

contains

public boolean contains(E p)
Checks whether the point p is contained inside this interval

Parameters:
p - point to check
Returns:
True if the point p is contained withing the interval, false otherwise

expand

public Interval expand(Interval<E> other)
Returns (smallest) interval that contains both this and the other interval

Parameters:
other - - Other interval to include
Returns:
Smallest interval that contains both this and the other interval

intersect

public Interval intersect(Interval<E> other)
Returns interval that is the intersection of this and the other interval Returns null if intersect is null

Parameters:
other - interval with which to intersect
Returns:
interval that is the intersection of this and the other interval

overlaps

public boolean overlaps(Interval<E> other)
Check whether this interval overlaps with the other interval (i.e. the intersect would not be null)

Parameters:
other - interval to compare with
Returns:
true if this interval overlaps the other interval

includesBegin

public boolean includesBegin()
Returns whether the start endpoint is included in the interval

Returns:
true if the start endpoint is included in the interval

includesEnd

public boolean includesEnd()
Returns whether the end endpoint is included in the interval

Returns:
true if the end endpoint is included in the interval

isIntervalComparable

public boolean isIntervalComparable(Interval<E> other)
Checks whether this interval is comparable with another interval comes before or after

Parameters:
other - interval to compare with

compareIntervalOrder

public int compareIntervalOrder(Interval<E> other)
Returns order of another interval compared to this one

Parameters:
other - Interval to compare with
Returns:
-1 if this interval is before the other interval, 1 if this interval is after 0 otherwise (may indicate the two intervals are same or not comparable)

toRelFlags

protected int toRelFlags(int comp,
                         int shift)

getRelationFlags

public int getRelationFlags(Interval<E> other)
Return set of flags indicating possible relationships between this interval and another interval.

Parameters:
other - Interval with which to compare with
Returns:
flags indicating possible relationship between this interval and the other interval

addIntervalRelationFlags

protected int addIntervalRelationFlags(int flags,
                                       boolean checkFuzzy)

extractRelationSubflags

public static int extractRelationSubflags(int flags,
                                          int shift)

checkMultipleBitSet

public static boolean checkMultipleBitSet(int flags)
Utility function to check if multiple bits are set for flags

Parameters:
flags - flags to check
Returns:
true if multiple bits are set

checkFlagSet

public static boolean checkFlagSet(int flags,
                                   int flag)
Utility function to check if a particular flag is set given a particular set of flags

Parameters:
flags - flags to check
flag - bit for flag of interest (is this flag set or not)
Returns:
true if flag is set for flags

checkFlagExclusiveSet

public static boolean checkFlagExclusiveSet(int flags,
                                            int flag,
                                            int mask)
Utility function to check if a particular flag is set exclusively given a particular set of flags and a mask

Parameters:
flags - flags to check
flag - bit for flag of interest (is this flag set or not)
mask - bitmask of bits to check
Returns:
true if flag is exclusively set for flags & mask

getRelation

public Interval.RelType getRelation(Interval<E> other)
Returns the relationship of this interval to the other interval The most specific relationship from the following is returned. NONE: the other interval is null EQUAL: this have same endpoints as other OVERLAP: this and other overlaps BEFORE: this ends before other starts AFTER: this starts after other ends BEGIN_MEET_END: this begin is the same as the others end END_MEET_BEGIN: this end is the same as the others begin CONTAIN: this contains the other INSIDE: this is inside the other UNKNOWN: this is returned if for some reason it is not possible to determine the exact relationship of the two intervals (possible for fuzzy intervals)

Parameters:
other - The other interval with which to compare with
Returns:
RelType indicating relationship between the two interval

equals

public boolean equals(Object o)
Overrides:
equals in class Pair<E extends Comparable<E>,E extends Comparable<E>>

hashCode

public int hashCode()
Overrides:
hashCode in class Pair<E extends Comparable<E>,E extends Comparable<E>>


Stanford NLP Group