Class QueryResults<T,R>

java.lang.Object
net.storm.api.query.results.QueryResults<T,R>
Type Parameters:
T - the type of elements in the results
R - the concrete result type (for method chaining)
All Implemented Interfaces:
Iterable<T>, Collection<T>
Direct Known Subclasses:
ItemQueryResults, SceneEntityQueryResults, WidgetQueryResults

public abstract class QueryResults<T,R> extends Object implements Collection<T>
Abstract base class for query result containers.

This class wraps a list of query results and provides various utility methods for accessing, manipulating, and iterating over the results. It implements the Collection interface to allow standard collection operations.

The class supports method chaining for fluent result manipulation, including sorting, limiting, reversing, and shuffling results.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final List<T>
    The underlying list of query results.
  • Constructor Summary

    Constructors
    Constructor
    Description
    QueryResults(List<T> results)
    Constructs a new QueryResults wrapper around the specified list.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    accept(Consumer<T> consumer, Function<R,T> target)
    Applies a consumer to a targeted element from the results if it exists.
    boolean
    add(T t)
    Adds an element to the results.
    boolean
    addAll(Collection<? extends T> c)
    Adds all elements from the specified collection to the results.
    void
    Removes all elements from the results.
    boolean
    Returns true if the results contain the specified element.
    boolean
    Returns true if the results contain all elements in the specified collection.
    Returns the first element in the results, or null if empty.
    get(int index)
    Returns the element at the specified position in the results.
    int
    Returns the index of the first occurrence of the specified element.
    boolean
    Returns true if this result set contains no elements.
    boolean
    Returns true if this result set contains at least one element.
    Returns an iterator over the elements in the results.
    final T
    Returns the last element in the results, or null if empty.
    int
    Returns the index of the last occurrence of the specified element.
    final R
    limit(int entries)
    Limits the results to the specified number of elements from the beginning.
    final R
    limit(int startIndex, int amount)
    Limits the results to a specified number of elements starting from a given index.
    Returns the underlying list of results.
    final T
    Returns a random element from the results, or null if empty.
    boolean
    Removes a single instance of the specified element from the results.
    boolean
    Removes all elements from the results that are contained in the specified collection.
    boolean
    Retains only the elements in the results that are contained in the specified collection.
    final R
    Reverses the order of elements in the results.
    final R
    Randomly shuffles the order of elements in the results.
    int
    Returns the number of elements in the results.
    final R
    sorted(Comparator<? super T> comparator)
    Sorts the results using the specified comparator.
    Returns an array containing all elements in the results.
    T[]
    Returns an array containing all elements in the results.

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface java.util.Collection

    equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray

    Methods inherited from interface java.lang.Iterable

    forEach
  • Field Details

    • results

      protected final List<T> results
      The underlying list of query results.
  • Constructor Details

    • QueryResults

      public QueryResults(List<T> results)
      Constructs a new QueryResults wrapper around the specified list.
      Parameters:
      results - the list of query results to wrap
  • Method Details

    • get

      public T get(int index)
      Returns the element at the specified position in the results.
      Parameters:
      index - the index of the element to return
      Returns:
      the element at the specified position
      Throws:
      IndexOutOfBoundsException - if the index is out of range
    • isEmpty

      public boolean isEmpty()
      Returns true if this result set contains no elements.
      Specified by:
      isEmpty in interface Collection<T>
      Returns:
      true if the results are empty, false otherwise
    • isNotEmpty

      public boolean isNotEmpty()
      Returns true if this result set contains at least one element.
      Returns:
      true if the results are not empty, false otherwise
    • sorted

      public final R sorted(Comparator<? super T> comparator)
      Sorts the results using the specified comparator.

      This method modifies the underlying list in place.

      Parameters:
      comparator - the comparator to determine the order of results
      Returns:
      this result instance for method chaining
    • lastIndexOf

      public int lastIndexOf(T o)
      Returns the index of the last occurrence of the specified element.
      Parameters:
      o - the element to search for
      Returns:
      the index of the last occurrence, or -1 if not found
    • addAll

      public boolean addAll(Collection<? extends T> c)
      Adds all elements from the specified collection to the results.
      Specified by:
      addAll in interface Collection<T>
      Parameters:
      c - the collection containing elements to add
      Returns:
      true if the results changed as a result of the call
    • limit

      public final R limit(int startIndex, int amount)
      Limits the results to a specified number of elements starting from a given index.

      Elements outside the specified range are removed from the results.

      Parameters:
      startIndex - the index to start from (inclusive)
      amount - the maximum number of elements to retain
      Returns:
      this result instance for method chaining
    • list

      public List<T> list()
      Returns the underlying list of results.
      Returns:
      the list containing all results
    • clear

      public void clear()
      Removes all elements from the results.
      Specified by:
      clear in interface Collection<T>
    • size

      public int size()
      Returns the number of elements in the results.
      Specified by:
      size in interface Collection<T>
      Returns:
      the number of elements
    • removeAll

      public boolean removeAll(Collection<?> c)
      Removes all elements from the results that are contained in the specified collection.
      Specified by:
      removeAll in interface Collection<T>
      Parameters:
      c - the collection containing elements to remove
      Returns:
      true if the results changed as a result of the call
    • remove

      public boolean remove(Object o)
      Removes a single instance of the specified element from the results.
      Specified by:
      remove in interface Collection<T>
      Parameters:
      o - the element to remove
      Returns:
      true if an element was removed
    • add

      public boolean add(T t)
      Adds an element to the results.
      Specified by:
      add in interface Collection<T>
      Parameters:
      t - the element to add
      Returns:
      true if the element was added successfully
    • reversed

      public final R reversed()
      Reverses the order of elements in the results.
      Returns:
      this result instance for method chaining
    • first

      public T first()
      Returns the first element in the results, or null if empty.
      Returns:
      the first element, or null if no elements exist
    • indexOf

      public int indexOf(T o)
      Returns the index of the first occurrence of the specified element.
      Parameters:
      o - the element to search for
      Returns:
      the index of the first occurrence, or -1 if not found
    • accept

      public final boolean accept(Consumer<T> consumer, Function<R,T> target)
      Applies a consumer to a targeted element from the results if it exists.

      This method allows for conditional execution of an action on a specific element extracted from the results using a target function.

      Parameters:
      consumer - the consumer to apply to the targeted element
      target - the function to extract the target element from results
      Returns:
      true if the target element was found and the consumer was applied
    • random

      public final T random()
      Returns a random element from the results, or null if empty.
      Returns:
      a randomly selected element, or null if no elements exist
    • contains

      public boolean contains(Object o)
      Returns true if the results contain the specified element.
      Specified by:
      contains in interface Collection<T>
      Parameters:
      o - the element to check for
      Returns:
      true if the element is present
    • shuffled

      public final R shuffled()
      Randomly shuffles the order of elements in the results.
      Returns:
      this result instance for method chaining
    • containsAll

      public boolean containsAll(Collection<?> c)
      Returns true if the results contain all elements in the specified collection.
      Specified by:
      containsAll in interface Collection<T>
      Parameters:
      c - the collection to check
      Returns:
      true if all elements are present
    • iterator

      public Iterator<T> iterator()
      Returns an iterator over the elements in the results.
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
      Returns:
      an iterator over the results
    • limit

      public final R limit(int entries)
      Limits the results to the specified number of elements from the beginning.

      Elements beyond the specified limit are removed from the results.

      Parameters:
      entries - the maximum number of elements to retain
      Returns:
      this result instance for method chaining
    • last

      public final T last()
      Returns the last element in the results, or null if empty.
      Returns:
      the last element, or null if no elements exist
    • toArray

      public Object[] toArray()
      Returns an array containing all elements in the results.
      Specified by:
      toArray in interface Collection<T>
      Returns:
      an array containing all elements
    • toArray

      public T[] toArray(Object[] o)
      Returns an array containing all elements in the results.
      Specified by:
      toArray in interface Collection<T>
      Parameters:
      o - the array into which elements are to be stored
      Returns:
      an array containing all elements
    • retainAll

      public boolean retainAll(Collection<?> c)
      Retains only the elements in the results that are contained in the specified collection.
      Specified by:
      retainAll in interface Collection<T>
      Parameters:
      c - the collection containing elements to retain
      Returns:
      true if the results changed as a result of the call