Package com.atlassian.jira.util
Class Window<T>
java.lang.Object
com.atlassian.jira.util.Window<T>
- All Implemented Interfaces:
Iterable<T>
A type of collection that contains another collection - underlying collection - and limits your view over it.
The subcollection that can be seen is called the visible part. It's more flexible than
List.subList(int, int)
because it allows more operations and also can tell whether either start or end is obstructed.-
Method Summary
Modifier and TypeMethodDescriptionDrop elements that don't match the predicate.dropUntilElement(T expected) Drop all the elements before the requested element.static <T> Window<T>empty()Create a window over an empty collection.first()Returns the first element of the visible part or the only element if the visible part has only one.focusElement(T expected, int radius) Drop elements from beginning and end until there are number of elements on both sides of the requested element.get()Returns the visible partbooleanReturns true if there are elements after the end of the visible part of the underlying collectionbooleanReturns true if there are elements before the start of the visible part of the underlying collectionbooleanisEmpty()Checks whether the visible part is emptyiterator()Keep only elements that match the predicate.keepUntilElement(T expected) Drop all elements after the requested element.last()Returns the last element of the visible part or the only element if the visible part is has only one.static <T> Window<T>of(Collection<T> coll) Create a window over a whole collectionreverse()Return a copy of the window where all the elements are in reverse order.shrinkFromEnd(int size) Drop elements from the start until the new visible part is of the requested sizeshrinkFromStart(int size) Drop elements from the beginning until the new visible part is of the requested sizeintsize()Returns the size of the visible parttoString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
of
Create a window over a whole collection- Parameters:
coll- the underlying collection
-
empty
Create a window over an empty collection. -
size
public int size()Returns the size of the visible part -
hasElementsAfter
public boolean hasElementsAfter()Returns true if there are elements after the end of the visible part of the underlying collection -
hasElementsBefore
public boolean hasElementsBefore()Returns true if there are elements before the start of the visible part of the underlying collection -
first
Returns the first element of the visible part or the only element if the visible part has only one.- Throws:
IndexOutOfBoundsException- if there are no elements
-
last
Returns the last element of the visible part or the only element if the visible part is has only one.- Throws:
IndexOutOfBoundsException- if there are no elements
-
keepUntil
Keep only elements that match the predicate. The new visible part ends at the first element that doesn't match. -
dropUntil
Drop elements that don't match the predicate. The new visible part start from the first matching element. -
dropUntilElement
Drop all the elements before the requested element.- Parameters:
expected- requested element- Returns:
- a new window which includes the requested element
- Throws:
IllegalArgumentException- if the requested element is not in the current visible part
-
keepUntilElement
Drop all elements after the requested element.- Parameters:
expected- requested element- Returns:
- a new window which includes the requested element
- Throws:
IllegalArgumentException- if the requested element is not in the current visible part
-
focusElement
Drop elements from beginning and end until there are number of elements on both sides of the requested element.- Parameters:
expected- the element that should be surroundedradius- number of elements before and after- Throws:
IllegalArgumentException- if the requested element is not the visible partIllegalArgumentException- if radius is less than zero
-
shrinkFromStart
Drop elements from the beginning until the new visible part is of the requested size- Parameters:
size- requested size- Returns:
- a new window over the same collection or the same window if is larger than the visible part
-
shrinkFromEnd
Drop elements from the start until the new visible part is of the requested size- Parameters:
size- requested size- Returns:
- a new window over the same collection or the same window if is larger than the visible part
-
get
Returns the visible part- Returns:
- A fresh, mutable list
-
isEmpty
public boolean isEmpty()Checks whether the visible part is empty- Returns:
- true if the visible part is empty
-
reverse
Return a copy of the window where all the elements are in reverse order. The elements in resulting visible part are identical with the original- Returns:
- A new window over the same collection
-
toString
-
iterator
-