Class Window<T>

java.lang.Object
com.atlassian.jira.util.Window<T>
All Implemented Interfaces:
Iterable<T>

@Immutable public class Window<T> extends Object implements 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 Type
    Method
    Description
    dropUntil(Predicate<T> predicate)
    Drop elements that don't match the predicate.
    dropUntilElement(T expected)
    Drop all the elements before the requested element.
    static <T> Window<T>
    Create a window over an empty collection.
    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 part
    boolean
    Returns true if there are elements after the end of the visible part of the underlying collection
    boolean
    Returns true if there are elements before the start of the visible part of the underlying collection
    boolean
    Checks whether the visible part is empty
     
    keepUntil(Predicate<T> predicate)
    Keep only elements that match the predicate.
    keepUntilElement(T expected)
    Drop all elements after the requested element.
    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 collection
    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 size
    shrinkFromStart(int size)
    Drop elements from the beginning until the new visible part is of the requested size
    int
    Returns the size of the visible part
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Method Details

    • of

      public static <T> Window<T> of(Collection<T> coll)
      Create a window over a whole collection
      Parameters:
      coll - the underlying collection
    • empty

      public static <T> Window<T> 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

      public T 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

      public T 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

      public Window<T> keepUntil(Predicate<T> predicate)
      Keep only elements that match the predicate. The new visible part ends at the first element that doesn't match.
    • dropUntil

      public Window<T> dropUntil(Predicate<T> predicate)
      Drop elements that don't match the predicate. The new visible part start from the first matching element.
    • dropUntilElement

      public Window<T> dropUntilElement(T expected)
      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

      public Window<T> keepUntilElement(T expected)
      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

      public Window<T> focusElement(T expected, int radius)
      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 surrounded
      radius - number of elements before and after
      Throws:
      IllegalArgumentException - if the requested element is not the visible part
      IllegalArgumentException - if radius is less than zero
    • shrinkFromStart

      public Window<T> shrinkFromStart(int size)
      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

      public Window<T> shrinkFromEnd(int size)
      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

      public List<T> 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

      public Window<T> 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

      public String toString()
      Overrides:
      toString in class Object
    • iterator

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Iterable<T>