Class Span
- java.lang.Object
-
- org.apache.pivot.wtk.Span
-
public final class Span extends java.lang.ObjectClass representing a range of integer values. The range includes all values in the interval [start, end] (inclusive). Values may be negative, and the value ofstartmay be less than or equal to the value ofend.
-
-
Field Summary
Fields Modifier and Type Field Description intendstatic java.util.Comparator<Span>END_COMPARATORComparator that determines the index of the last intersecting range.static java.lang.StringEND_KEYstatic java.util.Comparator<Span>INTERSECTION_COMPARATORComparator that determines if two ranges intersect.intstartstatic java.util.Comparator<Span>START_COMPARATORComparator that determines the index of the first intersecting range.static java.lang.StringSTART_KEY
-
Constructor Summary
Constructors Constructor Description Span(int index)Construct a new span of length 1 at the given location.Span(int start, int end)Construct a new span with the given bounds.Span(Dictionary<java.lang.String,?> span)Span(Sequence<?> span)Construct a new span from the given sequence with two numeric values corresponding to the start and end values respectively.Span(Span span)Construct a new span from another one (a "copy constructor").
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadjacentTo(Span span)Determines whether this span is adjacent to another span.booleanafter(Span span)Determines whether this span is "after" another span.booleanbefore(Span span)Determines whether this span is "before" another span.booleancontains(Span span)Determines whether this span contains another span.static Spandecode(java.lang.String value)Convert a string into a span.booleanequals(java.lang.Object o)longgetLength()Returns the length of the span.inthashCode()Spanintersect(Span span)Calculates the intersection of this span and another span.booleanintersects(Span span)Determines whether this span intersects with another span.booleanisNormal()Spanlengthen(int offset)Returns a newSpanwith the end value offset by the given value.Spanmove(int offset)Returns a newSpanwith the start value offset by the given value.intnormalEnd()booleannormalEquals(Span span)Decides whether the normalized version of this span is equal to the normalized version of the other span.Spannormalize()static Spannormalize(int start, int end)Create a span where the start value is less than or equal to the end value.intnormalStart()Spanoffset(int offset)Returns a newSpanwith both values offset by the given value.java.lang.StringtoString()Spanunion(Span span)Calculates the union of this span and another span.
-
-
-
Field Detail
-
start
public final int start
-
end
public final int end
-
START_KEY
public static final java.lang.String START_KEY
- See Also:
- Constant Field Values
-
END_KEY
public static final java.lang.String END_KEY
- See Also:
- Constant Field Values
-
START_COMPARATOR
public static final java.util.Comparator<Span> START_COMPARATOR
Comparator that determines the index of the first intersecting range.
-
END_COMPARATOR
public static final java.util.Comparator<Span> END_COMPARATOR
Comparator that determines the index of the last intersecting range.
-
INTERSECTION_COMPARATOR
public static final java.util.Comparator<Span> INTERSECTION_COMPARATOR
Comparator that determines if two ranges intersect.
-
-
Constructor Detail
-
Span
public Span(int index)
Construct a new span of length 1 at the given location.- Parameters:
index- The start and end of this span (inclusive).
-
Span
public Span(int start, int end)Construct a new span with the given bounds.- Parameters:
start- The start of this span - inclusive.end- The end of the span - inclusive.
-
Span
public Span(Span span)
Construct a new span from another one (a "copy constructor").- Parameters:
span- An existing span (which must not benull).- Throws:
java.lang.IllegalArgumentException- if the given span isnull.
-
Span
public Span(Dictionary<java.lang.String,?> span)
- Parameters:
span- A dictionary containing start and end values.- Throws:
java.lang.IllegalArgumentException- if the given span isnullor if the dictionary does not contain the start and end keys.
-
Span
public Span(Sequence<?> span)
Construct a new span from the given sequence with two numeric values corresponding to the start and end values respectively.- Parameters:
span- A sequence containing the start and end values.- Throws:
java.lang.IllegalArgumentException- if the given span isnull.
-
-
Method Detail
-
getLength
public long getLength()
Returns the length of the span.- Returns:
- The absolute value of (
endminusstart) + 1.
-
contains
public boolean contains(Span span)
Determines whether this span contains another span.- Parameters:
span- The span to test for containment.- Returns:
trueif this span containsspan;false, otherwise.- Throws:
java.lang.IllegalArgumentException- if the given span isnull.
-
adjacentTo
public boolean adjacentTo(Span span)
Determines whether this span is adjacent to another span.Adjacency means that one end of this span is +/-1 from either end of the other span (since start and end are inclusive).
- Parameters:
span- The span to test for adjacency.- Returns:
trueif this span is adjacentspan;false, otherwise.- Throws:
java.lang.IllegalArgumentException- if the given span isnull.
-
before
public boolean before(Span span)
Determines whether this span is "before" another span."Before" means that the normalized end of this span is
<the normalized start of the other span.- Parameters:
span- The span to test.- Returns:
trueif this span is "before"span;false, otherwise.- Throws:
java.lang.IllegalArgumentException- if the given span isnull.
-
after
public boolean after(Span span)
Determines whether this span is "after" another span."After" means that the normalized start of this span is
>the normalized end of the other span.- Parameters:
span- The span to test.- Returns:
trueif this span is "after"span;false, otherwise.- Throws:
java.lang.IllegalArgumentException- if the given span isnull.
-
intersects
public boolean intersects(Span span)
Determines whether this span intersects with another span.- Parameters:
span- The span to test for intersection.- Returns:
trueif this span intersects withspan;false, otherwise.- Throws:
java.lang.IllegalArgumentException- if the given span isnull.
-
intersect
public Span intersect(Span span)
Calculates the intersection of this span and another span.- Parameters:
span- The span to intersect with this span.- Returns:
- A new Span instance representing the intersection of this span and
span, ornullif the spans do not intersect. - Throws:
java.lang.IllegalArgumentException- if the given span isnull.
-
union
public Span union(Span span)
Calculates the union of this span and another span.- Parameters:
span- The span to union with this span.- Returns:
- A new Span instance representing the union of this span and
span.
-
normalStart
public int normalStart()
- Returns:
- The normalized start of this span, which is the lesser of the current start and end.
-
normalEnd
public int normalEnd()
- Returns:
- The normalized end of this span, which is the greater of the current start and end.
-
isNormal
public boolean isNormal()
- Returns:
trueif the span is "normal", meaning the start is less or equal the end.
-
normalize
public static Span normalize(int start, int end)
Create a span where the start value is less than or equal to the end value.- Parameters:
start- The new proposed start value.end- The new proposed end.- Returns:
- A span containing the normalized range.
-
normalize
public Span normalize()
- Returns:
- A normalized equivalent of the span in which
startis guaranteed to be less or equal toend.Note: if the span is already "normal", then no new object is created.
-
offset
public Span offset(int offset)
Returns a newSpanwith both values offset by the given value.This is useful while moving through a
TextPanedocument for instance, where you have to subtract off the starting offset for child nodes.- Parameters:
offset- The positive or negative amount by which to "move" this span (both start and end).- Returns:
- A new
Spanwith updated values.
-
lengthen
public Span lengthen(int offset)
Returns a newSpanwith the end value offset by the given value.- Parameters:
offset- The positive or negative amount by which to "lengthen" this span (only the end).- Returns:
- A new
Spanwith updated value.
-
move
public Span move(int offset)
Returns a newSpanwith the start value offset by the given value.- Parameters:
offset- The positive or negative amount by which to "shift" this span (only the start).- Returns:
- A new
Spanwith updated value.
-
normalEquals
public boolean normalEquals(Span span)
Decides whether the normalized version of this span is equal to the normalized version of the other span. Saves the overhead of making a new object (withnormalize(int, int)).- Parameters:
span- The span to test against this span.- Returns:
- Whether or not the normalized values of both spans are the same.
- Throws:
java.lang.IllegalArgumentException- if the other span isnull.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
decode
public static Span decode(java.lang.String value)
Convert a string into a span.If the string value is a JSON map, then parse the map and construct using the
Span(Dictionary)method.If the string value is a JSON list, then parse the list and construct using the first two values as start and end respectively, using the
Span(int, int)constructor.Also accepted is a simple list of two integer values separated by comma or semicolon.
Otherwise the string should be a single integer value that will be used to construct the span using the
Span(int)constructor.- Parameters:
value- The string value to decode into a new span.- Returns:
- The decoded span.
- Throws:
java.lang.IllegalArgumentException- if the value isnullor if the string starts with"{"but it cannot be parsed as a JSON map, or if it starts with"["but cannot be parsed as a JSON list.- See Also:
Span(Dictionary),Span(Sequence),Span(int, int),Span(int)
-
-