Package com.globalmentor.collections
Class Lists
java.lang.Object
com.globalmentor.collections.Lists
Utilities to be used with lists.
- Author:
- Garret Wilson
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <E> List<E>
immutableListOf
(E... elements) Creates a read-only list containing the given elements.static <E> List<E>
immutableListOf
(E[] elements, int start, int end) Creates a read-only list containing the given elements in the given range.static <E> List<E>
immutableListOf
(Collection<? extends E> collection, E... elements) Creates a read-only list containing the elements of the provided collection along with the given elements.static <E> List<E>
immutableListOf
(Collection<? extends E> collection, E[] elements, int start, int end) Creates a read-only list containing the elements of the provided collection along with the given elements.static <E> List<E>
Creates and returns a mutable list containing the contents of the given iterable with the additional elements, if any.static <E> List<E>
Creates and returns a mutable list containing the contents of the given iterator with the additional elements, if any.static <T> List<T>
longestCommonSuffix
(List<List<T>> lists) Determines the longest common suffix sublist from a list of lists.static <E> void
Replaces the first occurrence of the given object with a new object.static <T> List<T>
Returns a list to represent the given iterable.
-
Constructor Details
-
Lists
public Lists()
-
-
Method Details
-
replace
Replaces the first occurrence of the given object with a new object. If the object does not appear in the list, no action is taken.- Type Parameters:
E
- The type of element contained in the list.- Parameters:
list
- The list in which the object is included.oldObject
- The object to replace.newObject
- The object to take the place of the old object.
-
immutableListOf
Creates a read-only list containing the given elements.- Type Parameters:
E
- The type of element contained in the list.- Parameters:
elements
- The elements to be contained in the list.- Returns:
- The immutable version of the list.
- Throws:
NullPointerException
- if the given array of elements isnull
.
-
immutableListOf
Creates a read-only list containing the given elements in the given range.- Type Parameters:
E
- The type of element contained in the list.- Parameters:
elements
- The elements to be contained in the list.start
- the initial index of the range to be included, inclusiveend
- the final index of the range to be included, exclusive.- Returns:
- The immutable version of the list.
- Throws:
NullPointerException
- if the given array of elements isnull
.IllegalArgumentException
- if the start index is greater than the end index.ArrayIndexOutOfBoundsException
- if the start index is less than zero or the end index is greater than the length.
-
immutableListOf
Creates a read-only list containing the elements of the provided collection along with the given elements.- Type Parameters:
E
- The type of element contained in the list.- Parameters:
collection
- The existing collection to augment.elements
- The elements to be contained in the list.- Returns:
- The immutable version of the list.
- Throws:
NullPointerException
- if the given collection and/or array of elements isnull
.
-
immutableListOf
public static <E> List<E> immutableListOf(Collection<? extends E> collection, E[] elements, int start, int end) Creates a read-only list containing the elements of the provided collection along with the given elements.- Type Parameters:
E
- The type of element contained in the list.- Parameters:
collection
- The existing collection to augment.elements
- The elements to be contained in the list.start
- the initial index of the range to be included, inclusiveend
- the final index of the range to be included, exclusive.- Returns:
- The immutable version of the list.
- Throws:
NullPointerException
- if the given collection and/or array of elements isnull
.IllegalArgumentException
- if the start index is greater than the end index.ArrayIndexOutOfBoundsException
- if the start index is less than zero or the end index is greater than the length.
-
listOf
Creates and returns a mutable list containing the contents of the given iterator with the additional elements, if any.- Type Parameters:
E
- The type of the elements of the list.- Parameters:
iterator
- The iterator a list of the contents of which should be returned.elements
- The additional elements, if any, to add to the list.- Returns:
- A mutable list containing the indicated elements.
-
listOf
Creates and returns a mutable list containing the contents of the given iterable with the additional elements, if any.- Type Parameters:
E
- The type of the list- Parameters:
iterable
- The iterable a list of the contents of which should be returned.elements
- The additional elements, if any, to add to the list.- Returns:
- A mutable list containing the indicated elements.
-
toList
Returns a list to represent the given iterable. If the given iterable is aList
, it will be returned. If the given iterable is not aList
, a temporary list will be created and filled with the contents of the given iterable.In most cases the returned list should not be modified, as there are no guarantees of whether the list will be backed by existing data or whether the list will even be mutable.
- Type Parameters:
T
- The type of elements contained in the iterable.- Parameters:
iterable
- The iterable of elements.- Returns:
- A list containing the elements of the given iterable.
-
longestCommonSuffix
Determines the longest common suffix sublist from a list of lists. No element element is allowed to benull
.- Type Parameters:
T
- The common type of element in each of the lists.- Parameters:
lists
- The lists of lists among which to find a common suffix sublist.- Returns:
- A list representing the common suffix sublist of elements, compared using
Object.equals(Object)
. - Throws:
NullPointerException
- if one of the lists isnull
or contains anull
value.
-