org.omnaest.utils.structure.collection.list
Class ListUtils

java.lang.Object
  extended by org.omnaest.utils.structure.collection.list.ListUtils

public class ListUtils
extends Object

Helper class for modifying List instances.

Author:
Omnaest

Constructor Summary
ListUtils()
           
 
Method Summary
static
<FROM,TO> List<TO>
adapter(List<FROM> list, ElementBidirectionalConverter<FROM,TO> elementBidirectionalConverter)
          Returns a new ListToListAdapter instance
static
<E> List<E>
add(List<? extends E> list, E... elements)
          Returns the given List instance or a new List instance if the given one is null.
static
<E> List<E>
add(List<? extends E> list, E element)
          Returns the given List instance or a new List instance if the given one is null.
static
<E> List<E>
add(List<? extends E> list, int index, E... elements)
          Similar to add(List, Object...) allowing to specify an index position
static
<E> List<E>
add(List<? extends E> list, int index, E element)
          Similar to add(List, Object) allowing to specify an index position
static
<E> List<E>
addAll(List<? extends E> list, E... elements)
          Similar to add(List, Object...)
static
<E> List<E>
addAll(List<? extends E> list, int index, E... elements)
          Similar to add(List, int, Object...)
static
<E> List<E>
addAll(List<? extends E> list, Iterable<? extends E> iterable)
          Adds all the elements from the Iterable to the given List instance.
static
<E> List<E>
addToNewList(List<? extends E> list, E... elements)
          Returns a new List instance which contains all elements of the given List and additionally all further given elements.
static
<E> List<E>
addToNewList(List<? extends E> list, E element)
          Returns a new List instance which contains all elements of the given List and additionally the further given element.
static
<E> E[]
asArray(List<? extends E> list, Class<E> type)
          Similar to List.toArray(Object[]), returns null if a null reference is given.
static
<FROM,TO> List<TO>
convert(Collection<FROM> collection, ElementConverter<FROM,TO> elementConverter)
          Transforms a given Collection instance from one generic type into the other using a given ElementConverter.
static
<FROM,TO> List<TO>
convert(Collection<FROM> collection, ElementConverter<FROM,TO> elementConverter, boolean eliminateNullValues)
          Transforms a given Collection instance from one generic type into the other using a given ElementConverter.
static
<FROM,TO> List<TO>
convert(Collection<FROM> collection, ElementConverterOneToMany<FROM,TO> multiElementConverter)
          Transforms a given Collection instance from one generic type into the other using a given ElementConverterOneToMany.
static
<FROM,TO> List<TO>
convert(Collection<FROM> collection, ElementConverterOneToMany<FROM,TO> multiElementConverter, boolean eliminateNullValues)
          Transforms a given Collection instance from one generic type into the other using a given ElementConverter.
static
<FROM,TO> List<TO>
convert(ElementConverter<FROM,TO> elementConverter, FROM... elements)
           
static
<FROM,TO> List<TO>
convert(Iterable<FROM> iterable, ElementConverter<FROM,TO> elementConverter)
          Transforms a given Iterable instance from one generic type into the other using a given ElementConverter
static
<FROM,TO> List<TO>
convertExcludingNullElements(Collection<FROM> collection, ElementConverter<FROM,TO> elementConverter)
          Transforms a given Collection instance from one generic type into the other using a given ElementConverter.
static
<FROM,TO> List<TO>
convertListExcludingNullElements(Collection<FROM> collection, ElementConverterOneToMany<FROM,TO> multiElementConverter)
          Transforms a given Collection instance from one generic type into the other using a given ElementConverterOneToMany.
static
<E> E
elementAt(List<E> list, int index)
          Returns the element at the given index position of the given List.
static
<E> E
elementAtInverseIndex(List<E> list, int inverseIndex)
          Returns the element at the given inverse index position of the given List.
static
<E> E
elementWithReverseIndexPosition(List<E> list, int reverseIndexPosition)
          Returns the element of the given List at the given reverse index position which is counted beginning from the last element.
static
<E> List<E>
emptyList()
          Returns an empty ArrayList
static
<E> List<E>
filter(Collection<E> collection, ElementFilter<E> elementFilter)
          Returns a filtered List using a ElementFilter on the given Collection
static
<E> List<E>
filter(Iterable<E> iterable, ElementFilter<E> elementFilter)
          Returns a filtered List using a ElementFilter on the given Iterable
static
<E> List<E>
filter(List<E> list, BitSet bitSet)
          Returns a new List instance which contains all elements from the given List which have a related bit within the BitSet set to true
static
<E> List<E>
filter(List<E> list, ExcludingElementFilterIndexPositionBased elementFilterIndexBased)
          Returns a filtered List using a ExcludingElementFilterIndexPositionBased
static
<E> List<E>
filterAndOrderBy(Iterable<E> sourceIterable, Iterable<E>... filterAndOrderProvidingIterables)
          Creates a new List instance based on the given source Iterable.
static
<E> List<String>
filterExcludingBlankElements(Collection<String> collection)
          Filters all blank elements from the given Collection with elements of type String and returns a new List instance.
static
<E> List<E>
filterExcludingElement(Collection<E> collection, E element)
          Returns a filtered List which does not contain the given element
static
<E> List<E>
filterExcludingIndexPositions(List<E> list, Collection<Integer> indexPositionCollection)
           
static
<E> List<E>
filterExcludingIndexPositions(List<E> list, int... indexPositions)
           
static
<E> List<E>
filterExcludingNullElements(Collection<E> collection)
          Filters all null elements from the given Collection and returns a new List instance.
static
<E> List<E>
filterIncludingIndexPositions(List<E> list, Collection<Integer> indexPositionCollection)
           
static
<E> List<E>
filterIncludingIndexPositions(List<E> list, Integer... indexPositions)
           
static
<E> E
firstElement(List<E> list)
          Returns the first element of the given List.
static
<E> E
firstElementOf(List<E> list, int reverseIndexPosition)
          Returns the first element of a given List
static
<E> List<E>
generateList(int numberOfElements, Factory<E> valueFactory)
          Generates a new List using the given value Factory to create all the elements the given number of times.
static
<E> E
get(List<E> list, int index)
          Returns the element at the given index position within the given List instance.
static
<E> List<Integer>
indexListOf(List<E> list, E element)
          Returns a List of all index positions for the given element.
static
<E> List<E>
intersection(Collection<? extends Collection<E>> collectionOfCollections)
          Returns a new List with only this elements which are in all of the given Collections
static
<E> List<E>
intersection(Collection<E>... collections)
          Returns a new List with only this elements which are in all of the given Collections
static boolean isListType(Class<?> type)
          Returns true if the given type is not null and assignable to a List
static
<E> List<E>
iterableAsList(Iterable<E> iterable)
          Creates a new List from a given Iterable
static
<E> List<E>
iteratorAsList(Iterator<E> iterator)
          Creates a new List from a given Iterator
static
<E> E
lastElement(List<E> list)
          Returns the last element of the given List.
static
<E> E
lastElementOf(List<E> list)
          Returns the last element of the given List.
static
<E> List<E>
locked(List<E> list, Lock lock)
          Returns a view of the given List using the given Lock to synchronize all of its methods
static
<E> ListIterator<E>
locked(ListIterator<E> listIterator, Lock lock)
          Returns a view on the given ListIterator which uses a Lock to synchronize all its methods.
static
<E> List<E>
lockedByReentrantLock(List<E> list)
          Returns a view of the given List using a new ReentrantLock instance to synchronize all of its methods
static
<E> ListIterator<E>
lockedByReentrantLock(ListIterator<E> listIterator)
          Returns a view on the given ListIterator which uses a ReentrantLock to synchronize all its methods.
static
<E> List<E>
max(List<E> list, int maximumSize)
          Returns a sublist of the given List with the maximum given size.
static
<E> List<E>
mergeAll(Collection<? extends Collection<E>> collections)
          Merges all elements of the given Collection instances into one single List instance which keeps the order of the elements.
static
<E> List<E>
mergeAll(Collection<E>... collections)
          Merges all elements of the given Collection instances into one single List instance which keeps the order of the elements.
static
<E> E
remove(List<E> list, int index)
          Removes the element at the given index position of a given List instance.
static
<E> E
removeFirst(List<E> list)
          Removes the first element of a given List instance.
static
<E> List<E>
removeFirstToNewList(List<? extends E> list)
          Short for valueOf(Iterable) and removeFirst(List) applied to the new List

The source List is kept unaffected by this operation.
static
<E> E
removeLast(List<E> list)
          Removes the last element of a given List instance.
static
<E> List<E>
reverse(Iterable<E> iterable)
          Returns a new List instance with the same elements of the given Iterable but in reversed order.
static
<E> List<E>
set(List<E> list, int index, E element)
          List.set(int, Object) the value of the given List instance at the given index position to the given element.
static
<E extends Comparable<E>>
List<E>
sorted(Collection<E> collection)
          Similar to sorted(Collection, Comparator) using the Comparable interface of the given elements
static
<E> List<E>
sorted(Collection<E> collection, Comparator<E> comparator)
          Returns a new List instance which is based on the elements of the given Collection and which is sorted using the given Comparator
If the given Comparator is null the natural order is used.
static
<K,V,E> Map<K,V>
toMap(Iterable<E> iterable, ElementConverterElementToMapEntry<E,K,V> elementToMapEntryTransformer)
           
static
<K,V,E> Map<K,V>
toMap(KeyExtractor<? extends K,V> keyExtractor, Iterable<? extends V> iterable)
          Same as MapUtils.valueOf(KeyExtractor, Iterable)
static
<E> List<E>
valueOf(E... elements)
          Same as valueOf(Iterable) for one or more elements
static
<E> List<E>
valueOf(ElementStream<E> elementStream)
          Returns a new List instance for a given ElementStream
static
<E> List<E>
valueOf(Iterable<? extends E> iterable)
          Returns a new ArrayList instance containing the elements of a given Iterable
static
<E> List<E>
valueOf(Iterator<E> iterator)
          Returns a new List instance for a given Iterator
static
<E> List<E>
valueOf(Iterator<E> iterator, int toIndexPosition)
          Returns a new List instance for all the elements up to the given end index position based on the given Iterator.
static
<E> List<E>
valueOf(Iterator<E> iterator, int fromIndexPosition, int toIndexPosition)
          Returns a new List instance for all the elements between the two index position boundaries based on the given Iterator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ListUtils

public ListUtils()
Method Detail

convert

public static <FROM,TO> List<TO> convert(Collection<FROM> collection,
                                         ElementConverter<FROM,TO> elementConverter)
Transforms a given Collection instance from one generic type into the other using a given ElementConverter.

Parameters:
collection -
elementConverter -
See Also:
convert(Collection, ElementConverter, boolean)

convert

public static <FROM,TO> List<TO> convert(ElementConverter<FROM,TO> elementConverter,
                                         FROM... elements)
Parameters:
elementConverter -
elements -
Returns:
See Also:
convert(Collection, ElementConverter), valueOf(Object...)

convert

public static <FROM,TO> List<TO> convert(Iterable<FROM> iterable,
                                         ElementConverter<FROM,TO> elementConverter)
Transforms a given Iterable instance from one generic type into the other using a given ElementConverter

Parameters:
iterable -
elementConverter -
See Also:
convert(Collection, ElementConverter)

convert

public static <FROM,TO> List<TO> convert(Collection<FROM> collection,
                                         ElementConverter<FROM,TO> elementConverter,
                                         boolean eliminateNullValues)
Transforms a given Collection instance from one generic type into the other using a given ElementConverter.

Parameters:
collection -
elementConverter -
eliminateNullValues - : true->all null results from the element transformer will be discarded and not inserted into the result list.
See Also:
convert(Collection, ElementConverter)

convert

public static <FROM,TO> List<TO> convert(Collection<FROM> collection,
                                         ElementConverterOneToMany<FROM,TO> multiElementConverter)
Transforms a given Collection instance from one generic type into the other using a given ElementConverterOneToMany.

Parameters:
collection -
multiElementConverter -
See Also:
convert(Collection, ElementConverter, boolean)

convert

public static <FROM,TO> List<TO> convert(Collection<FROM> collection,
                                         ElementConverterOneToMany<FROM,TO> multiElementConverter,
                                         boolean eliminateNullValues)
Transforms a given Collection instance from one generic type into the other using a given ElementConverter.

Parameters:
collection -
multiElementConverter -
eliminateNullValues - : true->all null results from the element transformer will be discarded and not inserted into the result list.
Returns:
always new (ordered) list instance containing transformed elements
See Also:
convert(Collection, ElementConverter)

convertExcludingNullElements

public static <FROM,TO> List<TO> convertExcludingNullElements(Collection<FROM> collection,
                                                              ElementConverter<FROM,TO> elementConverter)
Transforms a given Collection instance from one generic type into the other using a given ElementConverter. Every null value returned by the ElementConverter will be discarded and not put into the result list.

Parameters:
collection -
elementConverter -
See Also:
convert(Collection, ElementConverter, boolean)

convertListExcludingNullElements

public static <FROM,TO> List<TO> convertListExcludingNullElements(Collection<FROM> collection,
                                                                  ElementConverterOneToMany<FROM,TO> multiElementConverter)
Transforms a given Collection instance from one generic type into the other using a given ElementConverterOneToMany. Every null value returned by the ElementConverter will be discarded and not put into the result list.

Parameters:
collection -
multiElementConverter -
See Also:
convert(Collection, ElementConverter, boolean)

filter

public static <E> List<E> filter(Collection<E> collection,
                                 ElementFilter<E> elementFilter)
Returns a filtered List using a ElementFilter on the given Collection

Parameters:
collection -
elementFilter -
Returns:
a new List instance containing only the not filtered elements of the given List
See Also:
ElementFilter, ExcludingElementFilter

filter

public static <E> List<E> filter(Iterable<E> iterable,
                                 ElementFilter<E> elementFilter)
Returns a filtered List using a ElementFilter on the given Iterable

Parameters:
iterable - Iterable
elementFilter -
Returns:
a new List instance containing only the not filtered elements of the given List
See Also:
ElementFilter, ExcludingElementFilter

filter

public static <E> List<E> filter(List<E> list,
                                 ExcludingElementFilterIndexPositionBased elementFilterIndexBased)
Returns a filtered List using a ExcludingElementFilterIndexPositionBased

Parameters:
list -
elementFilterIndexBased -
Returns:
a new List instance containing only the not filtered elements of the given List

filterAndOrderBy

public static <E> List<E> filterAndOrderBy(Iterable<E> sourceIterable,
                                           Iterable<E>... filterAndOrderProvidingIterables)
Creates a new List instance based on the given source Iterable. The given Iterable is then filtered by the additional filter and order providing Iterables. Filtering means that the list of source elements will only retain elements, which are in the filter list, too. Additionally the order of the filter lists will be used to sort the ouput List. Naturally the last applied filter will decide the order only.

If null is given as source, null is returned. If filter are not given or null, they are ignored, but still a List instance is returned.

Parameters:
sourceIterable -
filterAndOrderProvidingIterables -
Returns:

filterExcludingElement

public static <E> List<E> filterExcludingElement(Collection<E> collection,
                                                 E element)
Returns a filtered List which does not contain the given element

Parameters:
collection -
element -
Returns:
a new List instance containing only the not filtered elements of the given List
See Also:
filter(Iterable, ElementFilter)

filterExcludingIndexPositions

public static <E> List<E> filterExcludingIndexPositions(List<E> list,
                                                        Collection<Integer> indexPositionCollection)
Parameters:
list -
indexPositionCollection -
Returns:
See Also:
filter(List, ExcludingElementFilterIndexPositionBased)

filterExcludingIndexPositions

public static <E> List<E> filterExcludingIndexPositions(List<E> list,
                                                        int... indexPositions)
Parameters:
list -
indexPositions -
Returns:
See Also:
filter(List, ExcludingElementFilterIndexPositionBased)

filterExcludingNullElements

public static <E> List<E> filterExcludingNullElements(Collection<E> collection)
Filters all null elements from the given Collection and returns a new List instance.

Parameters:
collection -
Returns:

filterExcludingBlankElements

public static <E> List<String> filterExcludingBlankElements(Collection<String> collection)
Filters all blank elements from the given Collection with elements of type String and returns a new List instance.

Parameters:
collection -
Returns:

filterIncludingIndexPositions

public static <E> List<E> filterIncludingIndexPositions(List<E> list,
                                                        Collection<Integer> indexPositionCollection)
Parameters:
list -
indexPositionCollection -
Returns:
See Also:
filter(List, ExcludingElementFilterIndexPositionBased)

filterIncludingIndexPositions

public static <E> List<E> filterIncludingIndexPositions(List<E> list,
                                                        Integer... indexPositions)
Parameters:
list -
indexPositions -
Returns:
See Also:
filter(List, ExcludingElementFilterIndexPositionBased)

filter

public static <E> List<E> filter(List<E> list,
                                 BitSet bitSet)
Returns a new List instance which contains all elements from the given List which have a related bit within the BitSet set to true

Parameters:
list - List
bitSet - BitSet
Returns:
new List instance

valueOf

public static <E> List<E> valueOf(Iterable<? extends E> iterable)
Returns a new ArrayList instance containing the elements of a given Iterable

Parameters:
iterable -
Returns:
new ArrayList

valueOf

public static <E> List<E> valueOf(Iterator<E> iterator)
Returns a new List instance for a given Iterator

Parameters:
iterator -
Returns:

valueOf

public static <E> List<E> valueOf(Iterator<E> iterator,
                                  int toIndexPosition)
Returns a new List instance for all the elements up to the given end index position based on the given Iterator. The upper boundary is exclusive.

Parameters:
iterator -
toIndexPosition -
Returns:
new List instance

valueOf

public static <E> List<E> valueOf(Iterator<E> iterator,
                                  int fromIndexPosition,
                                  int toIndexPosition)
Returns a new List instance for all the elements between the two index position boundaries based on the given Iterator. The lower boundary is inclusive, the upper boundary is exclusive.

Parameters:
iterator -
fromIndexPosition -
toIndexPosition -
Returns:
new List instance

valueOf

public static <E> List<E> valueOf(ElementStream<E> elementStream)
Returns a new List instance for a given ElementStream

Parameters:
elementStream -
Returns:

valueOf

public static <E> List<E> valueOf(E... elements)
Same as valueOf(Iterable) for one or more elements

Parameters:
elements -
Returns:

indexListOf

public static <E> List<Integer> indexListOf(List<E> list,
                                            E element)
Returns a List of all index positions for the given element. If no element can be found at all an empty List is returned.

Parameters:
list -
element -
Returns:

intersection

public static <E> List<E> intersection(Collection<E>... collections)
Returns a new List with only this elements which are in all of the given Collections

Parameters:
collections -
Returns:
new List instance

intersection

public static <E> List<E> intersection(Collection<? extends Collection<E>> collectionOfCollections)
Returns a new List with only this elements which are in all of the given Collections

Parameters:
collectionOfCollections -
Returns:
new List instance

iterableAsList

public static <E> List<E> iterableAsList(Iterable<E> iterable)
Creates a new List from a given Iterable

Parameters:
iterable -
Returns:

iteratorAsList

public static <E> List<E> iteratorAsList(Iterator<E> iterator)
Creates a new List from a given Iterator

Parameters:
iterator -
Returns:

lastElementOf

public static <E> E lastElementOf(List<E> list)
Returns the last element of the given List. Returns null if the list is null or empty.

Parameters:
list -
Returns:

elementWithReverseIndexPosition

public static <E> E elementWithReverseIndexPosition(List<E> list,
                                                    int reverseIndexPosition)
Returns the element of the given List at the given reverse index position which is counted beginning from the last element. This means the last element has the reversed index position 0. Returns null if the list is null or empty.

Parameters:
list -
reverseIndexPosition -
Returns:

firstElementOf

public static <E> E firstElementOf(List<E> list,
                                   int reverseIndexPosition)
Returns the first element of a given List

Parameters:
list -
reverseIndexPosition -
Returns:

mergeAll

public static <E> List<E> mergeAll(Collection<? extends Collection<E>> collections)
Merges all elements of the given Collection instances into one single List instance which keeps the order of the elements.

Type Parameters:
E -
Parameters:
collections -
Returns:
See Also:
mergeAll(Collection...)

mergeAll

public static <E> List<E> mergeAll(Collection<E>... collections)
Merges all elements of the given Collection instances into one single List instance which keeps the order of the elements.

Type Parameters:
E -
Parameters:
collections -
Returns:
See Also:
mergeAll(Collection)

addToNewList

public static <E> List<E> addToNewList(List<? extends E> list,
                                       E... elements)
Returns a new List instance which contains all elements of the given List and additionally all further given elements.

This function will return always a new instance, even if the given list is null.

Parameters:
list -
elements -
Returns:
new List instance

addToNewList

public static <E> List<E> addToNewList(List<? extends E> list,
                                       E element)
Returns a new List instance which contains all elements of the given List and additionally the further given element.

This function will return always a new instance, even if the given list is null.

Parameters:
list -
element -
Returns:
new List instance

add

public static <E> List<E> add(List<? extends E> list,
                              E element)
Returns the given List instance or a new List instance if the given one is null. The returned instance will contain all elements of the given List and additionally the further given element.

This function will return always an instance, even if the given list is null.

Parameters:
list -
element -
Returns:
given List instance or new List instance if given List instance is null

add

public static <E> List<E> add(List<? extends E> list,
                              int index,
                              E element)
Similar to add(List, Object) allowing to specify an index position

Parameters:
list -
index -
element -
Returns:
the given List instance or a new one

addAll

public static <E> List<E> addAll(List<? extends E> list,
                                 Iterable<? extends E> iterable)
Adds all the elements from the Iterable to the given List instance. If the given List instance is null a new ArrayList is created.
If null is given as Iterable nothing will be added to the List.

Parameters:
list -
iterable -
Returns:
given List instance or a new ArrayList if null is given

addAll

public static <E> List<E> addAll(List<? extends E> list,
                                 E... elements)
Similar to add(List, Object...)

Parameters:
list -
elements -
Returns:

add

public static <E> List<E> add(List<? extends E> list,
                              E... elements)
Returns the given List instance or a new List instance if the given one is null. The returned instance will contain all elements of the given List and additionally all further given elements.

This function will return always a instance, even if the given list is null.

Parameters:
list -
elements -
Returns:
given List instance or new List instance if given List instance is null

addAll

public static <E> List<E> addAll(List<? extends E> list,
                                 int index,
                                 E... elements)
Similar to add(List, int, Object...)

Parameters:
list -
index -
elements -
Returns:

add

public static <E> List<E> add(List<? extends E> list,
                              int index,
                              E... elements)
Similar to add(List, Object...) allowing to specify an index position

Parameters:
list -
index -
elements -
Returns:
the given List instance or a new one

set

public static <E> List<E> set(List<E> list,
                              int index,
                              E element)
List.set(int, Object) the value of the given List instance at the given index position to the given element.

If the given index position is negative, nothing will be done.
If the given index position is greater or equal to the size of the List, the List will be filled with null values up to the index position.

If null is given as List instance a new ArrayList is created

Parameters:
list - List
index -
element -
Returns:
the List instance

toMap

public static <K,V,E> Map<K,V> toMap(KeyExtractor<? extends K,V> keyExtractor,
                                     Iterable<? extends V> iterable)
Same as MapUtils.valueOf(KeyExtractor, Iterable)

Parameters:
keyExtractor - KeyExtractor
iterable - Iterable
Returns:
See Also:
MapUtils.valueOf(KeyExtractor, Iterable)

toMap

public static <K,V,E> Map<K,V> toMap(Iterable<E> iterable,
                                     ElementConverterElementToMapEntry<E,K,V> elementToMapEntryTransformer)
Parameters:
iterable -
elementToMapEntryTransformer -
Returns:
See Also:
MapUtils.valueOf(Iterable, ElementConverterElementToMapEntry)

locked

public static <E> List<E> locked(List<E> list,
                                 Lock lock)
Returns a view of the given List using the given Lock to synchronize all of its methods

Parameters:
list -
lock -
Returns:
See Also:
lockedByReentrantLock(List)

lockedByReentrantLock

public static <E> List<E> lockedByReentrantLock(List<E> list)
Returns a view of the given List using a new ReentrantLock instance to synchronize all of its methods

Parameters:
list -
Returns:
See Also:
locked(List, Lock)

locked

public static <E> ListIterator<E> locked(ListIterator<E> listIterator,
                                         Lock lock)
Returns a view on the given ListIterator which uses a Lock to synchronize all its methods.

Parameters:
listIterator -
lock -
Returns:

lockedByReentrantLock

public static <E> ListIterator<E> lockedByReentrantLock(ListIterator<E> listIterator)
Returns a view on the given ListIterator which uses a ReentrantLock to synchronize all its methods.

Parameters:
listIterator -
Returns:

removeLast

public static <E> E removeLast(List<E> list)
Removes the last element of a given List instance. If the List reference is null or the List is empty nothing will be done to the List.

Parameters:
list -
Returns:
the removed element

removeFirst

public static <E> E removeFirst(List<E> list)
Removes the first element of a given List instance. If the List reference is null or the List is empty nothing will be done to the List.

Parameters:
list -
Returns:
the removed element

removeFirstToNewList

public static <E> List<E> removeFirstToNewList(List<? extends E> list)
Short for valueOf(Iterable) and removeFirst(List) applied to the new List

The source List is kept unaffected by this operation.

Parameters:
list -
Returns:
always a new List instance

remove

public static <E> E remove(List<E> list,
                           int index)
Removes the element at the given index position of a given List instance. If the List reference is null or the List is empty nothing will be done to the List.

Parameters:
list -
Returns:
the removed element

firstElement

public static <E> E firstElement(List<E> list)
Returns the first element of the given List. Returns null if the List reference is null or the List.isEmpty().

Parameters:
list -
See Also:
lastElement(List), elementAt(List, int)

lastElement

public static <E> E lastElement(List<E> list)
Returns the last element of the given List. Returns null if the List reference is null or the List.isEmpty().

Parameters:
list -
See Also:
firstElement(List), elementAt(List, int)

elementAtInverseIndex

public static <E> E elementAtInverseIndex(List<E> list,
                                          int inverseIndex)
Returns the element at the given inverse index position of the given List. If the List reference is null or the List.size() is to small, null is returned.

Parameters:
list -
inverseIndex -
See Also:
firstElement(List), lastElement(List), elementAt(List, int)

elementAt

public static <E> E elementAt(List<E> list,
                              int index)
Returns the element at the given index position of the given List. If the List reference is null or the List.size() is to small, null is returned.

Parameters:
list -
index -
See Also:
elementAtInverseIndex(List, int), firstElement(List), lastElement(List)

max

public static <E> List<E> max(List<E> list,
                              int maximumSize)
Returns a sublist of the given List with the maximum given size. There will always a new List instance be returned, but empty. If the given List has less elements only these are returned and the returned List will not be filled with additional null references.

Parameters:
list -
maximumSize -
Returns:

reverse

public static <E> List<E> reverse(Iterable<E> iterable)
Returns a new List instance with the same elements of the given Iterable but in reversed order.

Parameters:
iterable -
Returns:
See Also:
Collections.reverse(List)

sorted

public static <E extends Comparable<E>> List<E> sorted(Collection<E> collection)
Similar to sorted(Collection, Comparator) using the Comparable interface of the given elements

Parameters:
collection -
Returns:
new sorted List instance, null if given Collection is null

sorted

public static <E> List<E> sorted(Collection<E> collection,
                                 Comparator<E> comparator)
Returns a new List instance which is based on the elements of the given Collection and which is sorted using the given Comparator
If the given Comparator is null the natural order is used.
The given Collection will kept unmodified, only the returned List will be sorted.

Parameters:
collection -
comparator -
Returns:
a new List instance
See Also:
Collections.sort(List, Comparator)

generateList

public static <E> List<E> generateList(int numberOfElements,
                                       Factory<E> valueFactory)
Generates a new List using the given value Factory to create all the elements the given number of times.

Parameters:
numberOfElements -
valueFactory -
Returns:

asArray

public static <E> E[] asArray(List<? extends E> list,
                              Class<E> type)
Similar to List.toArray(Object[]), returns null if a null reference is given.

Parameters:
list - List
Returns:
new Array instance

adapter

public static <FROM,TO> List<TO> adapter(List<FROM> list,
                                         ElementBidirectionalConverter<FROM,TO> elementBidirectionalConverter)
Returns a new ListToListAdapter instance

Parameters:
list - List
elementBidirectionalConverter - ElementBidirectionalConverter
Returns:

get

public static <E> E get(List<E> list,
                        int index)
Returns the element at the given index position within the given List instance.

If the given List is null, null is returned.
If the index is out of bounds, null is returned.

Parameters:
list - List
index -
Returns:
element at the specific index position
See Also:
elementAt(List, int)

emptyList

public static <E> List<E> emptyList()
Returns an empty ArrayList

Returns:

isListType

public static boolean isListType(Class<?> type)
Returns true if the given type is not null and assignable to a List

Parameters:
type - Class
Returns:


Copyright © 2013. All Rights Reserved.