Package com.pnfsoftware.jeb.util.collect
Class Lists
java.lang.Object
com.pnfsoftware.jeb.util.collect.Lists
Collection of utility functions to create or manipulate concrete or abstract
lists.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidAdd (insert) the element of a collection.static <T> voidAdd (insert) the element of a collection.static <T> List<T> Add all elements of a source list to a destination list.static <T> List<T> addNonNulls(List<T> dst, List<? extends T> src) Add all non-null source elements to a destination list.static <T> List<T> addNonNulls(List<T> dst, T... elts) Add all non-null elements to a destination list.static <T extends Comparable<T>>
intInsert an element into a sorted list at its natural position.static <T> ArrayList<T> Create an emptyArrayList.static <T> ArrayList<T> createArrayList(Collection<? extends T>... colls) Create anArrayListcontaining the elements of the provided collections.static <T> ArrayList<T> createArrayList(T elt) Create anArrayListcontaining one element.static <T> List<T> fromIterator(Iterable<T> iterable) Generate a list from an iterable.static <T> TGet the value of a list, safely returning null if the list is null or the index out-of-range.static <T> TGet the value of a list, safely returning a provided default value if the list is null or the index out-of-range.static <T> TGet the first value of a list, safely returning null if the list is null or the index out-of-range.static <T extends Comparable<T>>
voidinsertIntoSortedList(List<T> list, T elt) Insert an additional element into a pre-sorted list and keep the list sorted.static <T> voidinsertIntoSortedList(List<T> list, T elt, Comparator<T> cmp, boolean reverse) Insert an additional element into a pre-sorted list and keep the list sorted.static <T> TRetrieve the last item of aList.static <T,R> List <? extends R> map(Collection<T> input, Function<? super T, ? extends R> mapper) Apply a 1-to-1 map operation to the elements of a collection and return the list of resulting new elements.static <T> ArrayList<T> newArrayList(T... elts) Create anArrayListcontaining the provided elements.static <T> List<T> Reverse the provided input list.static <T> ListIterator<T> reverseIterator(List<T> list) Get a reverse iterator on the list.static <T> voidRotate the elements of a list.static <T> voidRotate the elements of a list or a portion of the elements of a list.static <T> List<T> Return a non-null list.static <T> TSet the element of a collection.static <T> TSet the element of a collection.static <T> List<T> Get a tail sub-list of a list.
-
Constructor Details
-
Lists
public Lists()
-
-
Method Details
-
addSorted
Insert an element into a sorted list at its natural position.- Type Parameters:
T- element type- Parameters:
sortedList- a sorted (natural sort) list of comparable elementselt- the element to be inserted- Returns:
- the insertion index
-
createArrayList
Create an emptyArrayList.- Type Parameters:
T- element type- Returns:
- a new empty list
-
createArrayList
Create anArrayListcontaining one element.- Type Parameters:
T- element type- Parameters:
elt- element to add- Returns:
- a new list
-
createArrayList
Create anArrayListcontaining the elements of the provided collections.- Type Parameters:
T- element type- Parameters:
colls- source collections- Returns:
- a new list
-
newArrayList
Create anArrayListcontaining the provided elements.- Type Parameters:
T- element type- Parameters:
elts- elements to add- Returns:
- a new list
-
add
Add all elements of a source list to a destination list.- Type Parameters:
T- element type- Parameters:
dst- destination listsrc- optional source list- Returns:
- the destination list
-
addNonNulls
Add all non-null source elements to a destination list.- Type Parameters:
T- element type- Parameters:
dst- destination listsrc- optional source list- Returns:
- the destination list
-
addNonNulls
Add all non-null elements to a destination list.- Type Parameters:
T- element type- Parameters:
dst- destination listelts- elements to add- Returns:
- the destination list
-
subList
Get a tail sub-list of a list.- Parameters:
list- input listfromIndex- start index, inclusive- Returns:
- a view of the list tail
-
get
Get the value of a list, safely returning a provided default value if the list is null or the index out-of-range.- Parameters:
list- optional listindex- requested indexsafeValue- default value- Returns:
- the value at the index, or the default value
-
get
Get the value of a list, safely returning null if the list is null or the index out-of-range.- Parameters:
list- optional listindex- requested index- Returns:
- the value at the index, or null
-
getFirst
Get the first value of a list, safely returning null if the list is null or the index out-of-range.- Parameters:
list- optional list- Returns:
- the first value, or null
-
reverseIterator
Get a reverse iterator on the list. The pointer is set to the last element of the list (if any).- Parameters:
list- input list- Returns:
- a list iterator positioned after the last element
-
reverse
Reverse the provided input list.- Parameters:
list- a list- Returns:
- the input list (same object), reversed
-
map
public static <T,R> List<? extends R> map(Collection<T> input, Function<? super T, ? extends R> mapper) Apply a 1-to-1 map operation to the elements of a collection and return the list of resulting new elements.- Type Parameters:
T- type of input elementR- type of output element- Parameters:
input- input collectionmapper- amap()function- Returns:
- output list
-
set
Set the element of a collection. If the index is negative, the method throws OOB. If it is greater or equal than the current number of elements in the list, additional elements are inserted using the provided default value.- Type Parameters:
T- element type- Parameters:
list- list to updateindex- target indexelt- element to setdef- default value used to grow the list- Returns:
- the element previously at the target index
-
set
Set the element of a collection. If the index is negative, the method throws OOB. If it is greater or equal than the current number of elements in the list, additional elements are inserted using the null default value.- Type Parameters:
T- element type- Parameters:
list- list to updateindex- target indexelt- element to set- Returns:
- the element previously at the target index
-
add
Add (insert) the element of a collection. If the index is negative, the method throws OOB. If it is greater that the current number of elements in the list, additional elements are inserted using the provided default value.- Type Parameters:
T- element type- Parameters:
list- list to updateindex- insertion indexelt- element to insertdef- default value used to grow the list
-
add
Add (insert) the element of a collection. If the index is negative, the method throws OOB. If it is greater that the current number of elements in the list, additional elements are inserted using the null default value.- Type Parameters:
T- element type- Parameters:
list- list to updateindex- insertion indexelt- element to insert
-
safe
Return a non-null list.- Type Parameters:
T- element type- Parameters:
list- input list, possibly null- Returns:
- the input list or an empty list if the input was null
-
fromIterator
Generate a list from an iterable.- Type Parameters:
T- element type- Parameters:
iterable- input iterable- Returns:
- generated list
-
insertIntoSortedList
Insert an additional element into a pre-sorted list and keep the list sorted.- Type Parameters:
T- element type- Parameters:
list- a sorted listelt- the element to insert into the sorted list
-
insertIntoSortedList
public static <T> void insertIntoSortedList(List<T> list, T elt, Comparator<T> cmp, boolean reverse) Insert an additional element into a pre-sorted list and keep the list sorted.- Type Parameters:
T- element type- Parameters:
list- a sorted listelt- the element to insert into the sorted listcmp- optional comparator to be used; if null, the elements must becomparablereverse- false if the list is sorted in natural (ascending) order, true if the list is sorted in reverse (descending) order
-
rotate
Rotate the elements of a list.- Type Parameters:
T- element type- Parameters:
list- a listrotateForward- if true, the elements at i are moved to i+1 (and the last element will replace the first one); else, the elements at i are moved to i-1 (and the first element will replace the last one)
-
rotate
Rotate the elements of a list or a portion of the elements of a list.- Type Parameters:
T- element type- Parameters:
list- a listrotateForward- if true, the elements at i are moved to i+1 (and the last element will replace the first one); else, the elements at i are moved to i-1 (and the first element will replace the last one)indexFirst- first element to rotate (included)indexLast- last element to rotate (included), must be greater than indexFirst
-
last
Retrieve the last item of aList. This method is safe and returns null if the list is null or empty.- Type Parameters:
T- element type- Parameters:
list- a list- Returns:
- the last item at index
list.size() - 1or null if list is null or empty
-