jetbrains.buildServer.util
Class CollectionsUtil

java.lang.Object
  extended by jetbrains.buildServer.util.CollectionsUtil

public class CollectionsUtil
extends java.lang.Object

Author:
Sergey.Anchipolevsky Date: 06.09.2007

Field Summary
static Filter ACCEPT_ALL
           
static Converter SAME
           
 
Method Summary
static
<T> Filter<T>
acceptAllFilter()
          Type safe accept all filter
static
<T,Q> java.util.List<T>
applyOrder(Q[] sortedIds, java.util.List<T> allElements, java.util.Map<Q,T> idToElementMap)
          Accepts elements and reorders them according to provided partial order of the elements.
static
<T> java.util.Map<T,T>
asMap(T... args)
          Creates map from a given even number of parameters
static
<T> int
binarySearch(java.util.List<? extends T> list, com.intellij.openapi.util.Condition<? super T> condition)
          Returns the index of the first element for which condition is true.

It is supposed for the list that some index K exists such as:
1.
static
<SourceType>
java.util.List<SourceType>
collectUniqueObjectList(java.util.List<SourceType> list)
           
static
<Result,Source>
java.util.List<Result>
convertAndFilterNulls(java.lang.Iterable<Source> source, Converter<Result,Source> converter)
          Converts the collection (or any iterable) excluding the nulls afterwards.
static
<ResultType,SourceType>
java.util.List<ResultType>
convertCollection(java.util.Collection<? extends SourceType> source, Converter<ResultType,SourceType> converter)
          Create collection of type ResultType from collection of elements of type SourceType.
static
<ResultType,SourceType>
java.util.List<ResultType>
convertCollection(java.lang.Iterable<? extends SourceType> source, Converter<ResultType,SourceType> converter)
          Create collection of type ResultType from collection of elements of type SourceType.
static
<K,V,K2,V2>
void
convertMap(java.util.Map<? extends K,? extends V> source, java.util.Map<? super K2,? super V2> target, Converter<K2,K> keyConverter, Converter<V2,V> valueConverter)
          Converts `Map<K, V>` into `Map<K2, V2>` coercing keys and values given respective converters
static
<ResultType,SourceType>
java.util.Set<ResultType>
convertSet(java.util.Collection<? extends SourceType> source, Converter<ResultType,SourceType> converter)
          Create set of type ResultType from collection of elements of type SourceType.
static
<ResultType,SourceType>
java.util.Set<ResultType>
convertSet(java.lang.Iterable<? extends SourceType> source, Converter<ResultType,SourceType> converter)
          Create set of type ResultType from collection of elements of type SourceType.
static
<K,V> int
count(java.util.Map<K,V> map, Filter<K> keyFilter, Filter<V> valueFilter)
          counts a number of entries in map that matches given filters (by &&)
static
<ResultType,SourceType>
java.util.List<ResultType>
filterAndConvertCollection(java.util.Collection<? extends SourceType> source, Converter<ResultType,SourceType> converter, Filter<SourceType> filter)
          Create collection of type ResultType from collection of elements of type SourceType.
static
<ResultType,SourceType>
java.util.List<ResultType>
filterAndConvertCollection(java.lang.Iterable<? extends SourceType> source, Converter<ResultType,SourceType> converter, Filter<SourceType> filter)
          Create collection of type ResultType from collection of elements of type SourceType.
static
<ResultType,ResultCollection extends java.util.Collection<ResultType>,SourceType>
ResultCollection
filterAndConvertCollection(java.lang.Iterable<? extends SourceType> source, ResultCollection collection, Converter<ResultType,SourceType> converter, Filter<SourceType> filter)
           
static
<SourceType>
java.util.List<SourceType>
filterCollection(java.util.Collection<? extends SourceType> source, Filter<SourceType> filter)
          Elements are filtered basing on Filter interface.
static
<SourceType>
java.util.List<SourceType>
filterCollection(java.lang.Iterable<? extends SourceType> source, Filter<SourceType> filter)
          Elements are filtered basing on Filter interface.
static
<K,V> java.util.Map<K,V>
filterMap(java.util.Map<K,V> map, Filter<K> keyFilter, Filter<V> valueFilter)
          Creates a copy of map filtering it by values filter, order is preserved
static
<K,V> java.util.Map<K,V>
filterMapByKeys(java.util.Map<K,V> map, Filter<K> filter)
          Creates a copy of a map filtering it by keys filter, order is preserved
static
<K,V> java.util.Map<K,V>
filterMapByValues(java.util.Map<K,V> map, Filter<V> filter)
          Creates a copy of map filtering it by values filter, order is preserved
static
<T> java.util.List<T>
filterNulls(java.util.Collection<T> collection)
          Filters the collection, excluding all nulls from it.
static
<T> T
findFirst(java.lang.Iterable<T> list, Filter<T> filter)
          Searches a collection and returns first element which fulfills given filter function
static
<K,V> java.util.Map<K,java.util.List<V>>
groupBy(java.util.Collection<V> collection, Converter<K,V> converter)
          Groups values in collection according to some computed attribute.
static
<T extends java.lang.Comparable<T>>
void
insertWithSorting(T o, java.util.List<T> list)
          Insert an object into target collection into a position so collection remains sorted ascending
static
<T> void
insertWithSorting(T o, java.util.List<T> list, java.util.Comparator<T> comp)
          Insert an object into target collection into a position so collection remains sorted ascending
static
<T> java.util.Set<T>
intersect(java.util.Collection<T> set1, java.util.Collection<T> set2)
          Intersection: items present in both sets simultaneously.
static
<T> java.util.Collection<T>
join(java.util.Collection<T>... a)
          Merges two collections
static
<T> java.util.Collection<T>
join(java.util.Collection<T> a, java.util.Collection<T> b)
          Merges two collections
static
<T> java.util.Collection<T>
join(java.util.Collection<T> a, java.util.Collection<T> b, java.util.Collection<T> c)
          Merges two collections
static
<T> java.util.Set<T>
minus(java.util.Collection<T> minuend, java.util.Set<T> subtrahend)
          Differs: items from the first set excluding items from the second set.
static
<T> java.util.SortedSet<T>
minus(java.util.SortedSet<T> minuend, java.util.Set<T> subtrahend)
          Differs: items from the first (sorted) set excluding items from the second set.
static
<T> void
removeDuplicates(java.util.Collection<T> collection)
          Removes the duplicates from the provided collection of elements.
static
<T> java.util.Collection<T>
removeDuplicatesPreservingOrder(java.util.Collection<T> collection)
          The same as removeDuplicates(java.util.Collection), but preserves the order of non duplicated elements.
static
<T,Coll extends java.lang.Iterable<T>>
Coll
removeNulls(Coll collection)
           
static void retainMatched(java.util.Collection<java.lang.String> stringCollection, java.util.regex.Pattern pattern)
           
static
<T> java.util.Set<T>
setOf(T... args)
          Creates a set from given parameters
static
<T> java.util.List<java.util.List<T>>
split(java.util.List<T> toSplit, int numParts)
          Split one collection into a collection of smaller collections
static java.lang.String to_s(java.util.Collection c)
          Returns a debug-oriented representation of the collection, when all elements are joined with "\r\n"
static
<T> java.util.Iterator<T>
toTypedIterator(java.lang.Class<T> clazz, java.util.Iterator data)
          Converts not-typed iterator to a typed iterator with lazy type check
static
<T> java.util.Collection<T>
unite(java.util.Collection<? extends T>... col)
          Deprecated. use join(java.util.Collection...)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ACCEPT_ALL

public static final Filter ACCEPT_ALL

SAME

public static final Converter SAME
Method Detail

acceptAllFilter

@NotNull
public static <T> Filter<T> acceptAllFilter()
Type safe accept all filter

Type Parameters:
T - type
Returns:
filter accepting all values
Since:
7.1

filterAndConvertCollection

public static <ResultType,SourceType> java.util.List<ResultType> filterAndConvertCollection(java.util.Collection<? extends SourceType> source,
                                                                                            @NotNull
                                                                                            Converter<ResultType,SourceType> converter,
                                                                                            @NotNull
                                                                                            Filter<SourceType> filter)
Create collection of type ResultType from collection of elements of type SourceType. Elements are filtered basing on Filter interface and conversion from SourceType to ResultType is performed using Converter interface.

Parameters:
source - source collection
converter - converter from one element to another
filter - filter which allows to skip some elements of the source collection
Returns:
converted and filtered collection

filterAndConvertCollection

public static <ResultType,SourceType> java.util.List<ResultType> filterAndConvertCollection(java.lang.Iterable<? extends SourceType> source,
                                                                                            @NotNull
                                                                                            Converter<ResultType,SourceType> converter,
                                                                                            @NotNull
                                                                                            Filter<SourceType> filter)
Create collection of type ResultType from collection of elements of type SourceType. Elements are filtered basing on Filter interface and conversion from SourceType to ResultType is performed using Converter interface.

Parameters:
source - source collection
converter - converter from one element to another
filter - filter which allows to skip some elements of the source collection
Returns:
converted and filtered collection

filterAndConvertCollection

public static <ResultType,ResultCollection extends java.util.Collection<ResultType>,SourceType> ResultCollection filterAndConvertCollection(java.lang.Iterable<? extends SourceType> source,
                                                                                                                                            @NotNull
                                                                                                                                            ResultCollection collection,
                                                                                                                                            @NotNull
                                                                                                                                            Converter<ResultType,SourceType> converter,
                                                                                                                                            @NotNull
                                                                                                                                            Filter<SourceType> filter)

join

public static <T> java.util.Collection<T> join(java.util.Collection<T>... a)
Merges two collections

Type Parameters:
T - type of collection elements
Parameters:
a - collections to join
Returns:
merged collection
Since:
5.1

join

public static <T> java.util.Collection<T> join(@NotNull
                                               java.util.Collection<T> a,
                                               @NotNull
                                               java.util.Collection<T> b)
Merges two collections

Type Parameters:
T - type of collection elements
Parameters:
a - collection
b - collection
Returns:
merged collection
Since:
5.1

join

public static <T> java.util.Collection<T> join(@NotNull
                                               java.util.Collection<T> a,
                                               @NotNull
                                               java.util.Collection<T> b,
                                               @NotNull
                                               java.util.Collection<T> c)
Merges two collections

Type Parameters:
T - type of collection elements
Parameters:
a - collection
b - collection
c - collection
Returns:
merged collection
Since:
5.1

filterCollection

public static <SourceType> java.util.List<SourceType> filterCollection(java.util.Collection<? extends SourceType> source,
                                                                       Filter<SourceType> filter)
Elements are filtered basing on Filter interface.

Parameters:
source - source collection
filter - filter which allows to skip some elements of the source collection
Returns:
filtered collection

filterCollection

public static <SourceType> java.util.List<SourceType> filterCollection(java.lang.Iterable<? extends SourceType> source,
                                                                       Filter<SourceType> filter)
Elements are filtered basing on Filter interface.

Parameters:
source - source collection
filter - filter which allows to skip some elements of the source collection
Returns:
filtered collection

convertCollection

public static <ResultType,SourceType> java.util.List<ResultType> convertCollection(java.util.Collection<? extends SourceType> source,
                                                                                   Converter<ResultType,SourceType> converter)
Create collection of type ResultType from collection of elements of type SourceType. Elements conversion from SourceType to ResultType is performed using Converter interface.

Parameters:
source - source collection
converter - converter from one element to another
Returns:
converted and filtered collection

convertCollection

public static <ResultType,SourceType> java.util.List<ResultType> convertCollection(java.lang.Iterable<? extends SourceType> source,
                                                                                   Converter<ResultType,SourceType> converter)
Create collection of type ResultType from collection of elements of type SourceType. Elements conversion from SourceType to ResultType is performed using Converter interface.

Parameters:
source - source collection
converter - converter from one element to another
Returns:
converted and filtered collection

convertSet

public static <ResultType,SourceType> java.util.Set<ResultType> convertSet(java.util.Collection<? extends SourceType> source,
                                                                           Converter<ResultType,SourceType> converter)
Create set of type ResultType from collection of elements of type SourceType. Elements conversion from SourceType to ResultType is performed using Converter interface.

Parameters:
source - source collection
converter - converter from one element to another
Returns:
converted and filtered collection

convertSet

public static <ResultType,SourceType> java.util.Set<ResultType> convertSet(java.lang.Iterable<? extends SourceType> source,
                                                                           Converter<ResultType,SourceType> converter)
Create set of type ResultType from collection of elements of type SourceType. Elements conversion from SourceType to ResultType is performed using Converter interface.

Parameters:
source - source collection
converter - converter from one element to another
Returns:
converted and filtered collection

collectUniqueObjectList

public static <SourceType> java.util.List<SourceType> collectUniqueObjectList(java.util.List<SourceType> list)

findFirst

@Nullable
public static <T> T findFirst(java.lang.Iterable<T> list,
                                       Filter<T> filter)
Searches a collection and returns first element which fulfills given filter function

Parameters:
list - collection of elements
filter - filter to satisfy
Returns:
first element for which filter function returns true or null if no such element exists
Since:
7.0

insertWithSorting

public static <T extends java.lang.Comparable<T>> void insertWithSorting(@NotNull
                                                                         T o,
                                                                         @NotNull
                                                                         java.util.List<T> list)
Insert an object into target collection into a position so collection remains sorted ascending

Parameters:
o - object to insert
list - list of objects which should be sorted. All items should be added to the list using this function

insertWithSorting

public static <T> void insertWithSorting(@NotNull
                                         T o,
                                         @NotNull
                                         java.util.List<T> list,
                                         @NotNull
                                         java.util.Comparator<T> comp)
Insert an object into target collection into a position so collection remains sorted ascending

Parameters:
o - object to insert
list - list of objects which should be sorted. All items should be added to the list using this function and using same comparator
comp - comparator for the objects in the list

to_s

@NotNull
public static java.lang.String to_s(@NotNull
                                            java.util.Collection c)
Returns a debug-oriented representation of the collection, when all elements are joined with "\r\n"

Parameters:
c -
Returns:
see above

split

@NotNull
public static <T> java.util.List<java.util.List<T>> split(@NotNull
                                                                  java.util.List<T> toSplit,
                                                                  int numParts)
Split one collection into a collection of smaller collections

Type Parameters:
T - collection items' base type
Parameters:
toSplit - collection to split
numParts - number of parts to have
Returns:
collection of small collections

retainMatched

public static void retainMatched(@Nullable
                                 java.util.Collection<java.lang.String> stringCollection,
                                 @NotNull
                                 java.util.regex.Pattern pattern)

minus

@NotNull
public static <T> java.util.Set<T> minus(java.util.Collection<T> minuend,
                                                 java.util.Set<T> subtrahend)
Differs: items from the first set excluding items from the second set.

Type Parameters:
T - item type.
Parameters:
minuend - first set.
subtrahend - second set.
Returns:
the remainder of differs; if the minuend is a kind of SortedSet the result will be an instance of SortedSet with the same comparator.

minus

@NotNull
public static <T> java.util.SortedSet<T> minus(java.util.SortedSet<T> minuend,
                                                       java.util.Set<T> subtrahend)
Differs: items from the first (sorted) set excluding items from the second set.

Type Parameters:
T - item type.
Parameters:
minuend - first set.
subtrahend - second set.
Returns:
the remainder of differs; with the comparator from minuend.
Since:
8.0

intersect

@NotNull
public static <T> java.util.Set<T> intersect(@Nullable
                                                     java.util.Collection<T> set1,
                                                     @Nullable
                                                     java.util.Collection<T> set2)
Intersection: items present in both sets simultaneously.

Type Parameters:
T - item type.
Parameters:
set1 - first set.
set2 - second set.
Returns:
the intersection of both sets.

removeNulls

@NotNull
public static <T,Coll extends java.lang.Iterable<T>> Coll removeNulls(@NotNull
                                                                              Coll collection)

filterNulls

@NotNull
public static <T> java.util.List<T> filterNulls(@NotNull
                                                        java.util.Collection<T> collection)
Filters the collection, excluding all nulls from it.

Type Parameters:
T - elements' type
Parameters:
collection - the collection to filter
Returns:
a filtered collection
Since:
7.0
See Also:
convertCollection(Iterable, Converter), convertAndFilterNulls(Iterable, Converter)

convertAndFilterNulls

@NotNull
public static <Result,Source> java.util.List<Result> convertAndFilterNulls(@NotNull
                                                                                   java.lang.Iterable<Source> source,
                                                                                   @NotNull
                                                                                   Converter<Result,Source> converter)
Converts the collection (or any iterable) excluding the nulls afterwards.

Type Parameters:
Result - result type
Source - source type
Parameters:
source - the source iterable
converter - the converter
Returns:
a converter collection without nulls
Since:
7.1
See Also:
convertCollection(Iterable, Converter), #filterNulls(java.util.List)

unite

@NotNull
@Deprecated
public static <T> java.util.Collection<T> unite(@NotNull
                                                                   java.util.Collection<? extends T>... col)
Deprecated. use join(java.util.Collection...)

Parameters:
col - collection(s)
Returns:
Returns a new collection containing all the elements from the given collections
Since:
7.0

applyOrder

public static <T,Q> java.util.List<T> applyOrder(@NotNull
                                                 Q[] sortedIds,
                                                 @NotNull
                                                 java.util.List<T> allElements,
                                                 @NotNull
                                                 java.util.Map<Q,T> idToElementMap)
Accepts elements and reorders them according to provided partial order of the elements. Elements must be identifiable by string id. New order of elements is defined as the sorted array of identifiers. Note that array of identifiers size may not be the same as the size of the whole collection.

Type Parameters:
T -
Parameters:
sortedIds - new order of identifiers
allElements - elements to sort
idToElementMap - map from element id to element
Returns:
reordered elements

asMap

@NotNull
public static <T> java.util.Map<T,T> asMap(@NotNull
                                                   T... args)
Creates map from a given even number of parameters

Type Parameters:
T - type of parameters
Parameters:
args - parameters as sequence of key, value
Returns:
map
Since:
7.0

setOf

@NotNull
public static <T> java.util.Set<T> setOf(@NotNull
                                                 T... args)
Creates a set from given parameters

Type Parameters:
T - type of element
Parameters:
args - elements of set
Returns:
set
Since:
7.0

removeDuplicates

public static <T> void removeDuplicates(@NotNull
                                        java.util.Collection<T> collection)
Removes the duplicates from the provided collection of elements.

Notes:

Type Parameters:
T - any type able to store in HashSet
Parameters:
collection - the collection to remove duplicates from

removeDuplicatesPreservingOrder

@NotNull
public static <T> java.util.Collection<T> removeDuplicatesPreservingOrder(@NotNull
                                                                                  java.util.Collection<T> collection)
The same as removeDuplicates(java.util.Collection), but preserves the order of non duplicated elements.

In some cases may return the same collection instance (but without duplicates).

Type Parameters:
T - any type able to store in HashSet
Parameters:
collection - the collection to remove duplicates from
Returns:
a filtered collection
Since:
8.0

binarySearch

public static <T> int binarySearch(@NotNull
                                   java.util.List<? extends T> list,
                                   @NotNull
                                   com.intellij.openapi.util.Condition<? super T> condition)
Returns the index of the first element for which condition is true.

It is supposed for the list that some index K exists such as:
1. 0 <= K <= list size
2. condition is false for all indexes those are less than K
3. condition is true for all indexes those are equal or greater than K

Returns:
K
Since:
6.5.2

toTypedIterator

@NotNull
public static <T> java.util.Iterator<T> toTypedIterator(@NotNull
                                                                java.lang.Class<T> clazz,
                                                                @NotNull
                                                                java.util.Iterator data)
Converts not-typed iterator to a typed iterator with lazy type check

Type Parameters:
T - type of elements
Parameters:
clazz - class of items
data - not-typed iterator
Returns:
lazy wrapper for a given iterator of given type
Since:
7.0

filterMapByKeys

@NotNull
public static <K,V> java.util.Map<K,V> filterMapByKeys(@NotNull
                                                               java.util.Map<K,V> map,
                                                               @NotNull
                                                               Filter<K> filter)
Creates a copy of a map filtering it by keys filter, order is preserved

Type Parameters:
K - type
V - type
Parameters:
map - map
filter - keys filter
Returns:
copy of map with subset of values
Since:
7.1

filterMapByValues

@NotNull
public static <K,V> java.util.Map<K,V> filterMapByValues(@NotNull
                                                                 java.util.Map<K,V> map,
                                                                 @NotNull
                                                                 Filter<V> filter)
Creates a copy of map filtering it by values filter, order is preserved

Type Parameters:
K - type
V - type
Parameters:
map - map
filter - values filter
Returns:
copy of map with subset of values
Since:
7.1

filterMap

@NotNull
public static <K,V> java.util.Map<K,V> filterMap(@NotNull
                                                         java.util.Map<K,V> map,
                                                         @NotNull
                                                         Filter<K> keyFilter,
                                                         @NotNull
                                                         Filter<V> valueFilter)
Creates a copy of map filtering it by values filter, order is preserved

Type Parameters:
K - type
V - type
Parameters:
map - map
keyFilter - keys filter
valueFilter - values filter
Returns:
copy of map with subset of values
Since:
7.1

count

public static <K,V> int count(@NotNull
                              java.util.Map<K,V> map,
                              @NotNull
                              Filter<K> keyFilter,
                              @NotNull
                              Filter<V> valueFilter)
counts a number of entries in map that matches given filters (by &&)

Type Parameters:
K - type
V - type
Parameters:
map - map
keyFilter - keys filter
valueFilter - values filter
Returns:
number of (key,value) pairs matching both filters
Since:
7.1

groupBy

@NotNull
public static <K,V> java.util.Map<K,java.util.List<V>> groupBy(@NotNull
                                                                       java.util.Collection<V> collection,
                                                                       @NotNull
                                                                       Converter<K,V> converter)
Groups values in collection according to some computed attribute.

Type Parameters:
K - grouping attribute
V - value to group
Parameters:
collection - collection to group
converter - converter which computes attribute to group by
Returns:
map where key is grouping attribute and value is list of values corresponding to this attribute
Since:
8.0

convertMap

public static <K,V,K2,V2> void convertMap(@NotNull
                                          java.util.Map<? extends K,? extends V> source,
                                          @NotNull
                                          java.util.Map<? super K2,? super V2> target,
                                          @NotNull
                                          Converter<K2,K> keyConverter,
                                          @NotNull
                                          Converter<V2,V> valueConverter)
Converts `Map<K, V>` into `Map<K2, V2>` coercing keys and values given respective converters

Parameters:
source - source-map
target - target-map
keyConverter - keys converter
valueConverter - values converter