public class Expressive extends Object
Expressive is designed to allow coders to write more expressive code, focusing on what they want to do with collections of objects, rather than the mechanics of the manipulation of the Java
Collections API.
In this vein, Expressive supplies static methods for the easy creation of different collection types, and returns types compatible with the Java Collections API that allow for easier use
and manipulation.
EList| Modifier and Type | Class and Description |
|---|---|
static class |
Expressive.Comparators
Provides common implementations of comparators.
|
static class |
Expressive.Filter
Filter operations return views on an Iterable or Collection by applying a given
EPredicate to them. |
static class |
Expressive.Predicate
Provides common implementations of
EPredicate. |
static class |
Expressive.Transformers
Provides convenience access to
ETransformers that are of common use. |
| Modifier and Type | Method and Description |
|---|---|
static <T> T[] |
array(Collection<T> values)
Convenience method for creating an array from a collection.
|
static <T> T[] |
array(T... values)
Convenience method for creating an array.
|
static <T> Collection<T> |
collection(T... values)
Convenience method for creating a collection.
|
static <T> EList<T> |
flatten(Collection<? extends Collection<? extends T>> collectionOfCollection)
Flattens the given collections of collections into a single list containing all entries.
|
static <T> EList<T> |
flatten(Collection<? extends T>... collectionOfCollection)
Flattens the given varargs of collections into a single list containing all entries.
|
static <T> EList<T> |
flatten(T[]... values)
Convenience method for creating an array containing the concatenated contents of the given set of arrays.
|
static <T> boolean |
isEmpty(Collection<T> collection)
Returns true if the given
Collection is empty or null |
static <T> boolean |
isEmpty(Iterable<T> iterable)
Returns true if the given
Iterable is empty or null |
static <K,V> boolean |
isEmpty(Map<K,V> map)
Returns true if the given
Map is empty or null |
static <T> boolean |
isNotEmpty(Collection<T> collection)
Returns true if the given
Collection is not null and is not empty |
static <T> boolean |
isNotEmpty(Iterable<T> iterable)
Returns true if the given
Iterable is not null and is not empty |
static <K,V> boolean |
isNotEmpty(Map<K,V> map)
Returns true if the given
Map is not null empty and is not null |
static <T> Iterable<T> |
iterable(Enumeration<T> enumeration)
Convenience method for creating an
Iterable from an Enumeration so that it can easily be used in a for each loop. |
static <T> Iterable<T> |
iterable(Iterator<T> iterator)
|
static <T> EList<T> |
list(Collection<? extends T> values)
Convenience method for creating an
EList from a Collection. |
static <T> EList<T> |
list(Iterable<? extends T> values)
|
static <T> EList<T> |
list(Iterator<? extends T> values)
|
static <T> EList<T> |
list(T... values)
Convenience method for creating an
EList. |
static <K,V> Map<K,V> |
map(Object... values)
Returns a map based on the inputs interpreted as key-value pairs.
|
static <K> MapKeys<K> |
mapKeys(K... keys)
Supports a fluid syntax for creating maps conveniently.
|
static <K> MapKeys<K> |
mapKeys(List<K> keys)
Supports a fluid syntax for creating maps conveniently.
|
static <K,V> Map<V,Set<K>> |
reverse(Map<K,V> map)
Reverses a
Map. |
static <K,V> Map<V,K> |
reverseUnique(Map<K,V> map)
Reverses a
Map, mapping each value in the given map to a single key value. |
static <T> Set<T> |
set(T... values)
Convenience method for creating a
Set. |
public static <T> T[] array(T... values)
Convenience method for creating an array.
T - values - the objects to be placed in an arraypublic static <T> T[] array(Collection<T> values)
Convenience method for creating an array from a collection.
T - values - a collection of objects to be placed in an arrayCollection.toArray()public static <T> Collection<T> collection(T... values)
Convenience method for creating a collection.
T - values - the objects to be placed in a collectionpublic static <T> Set<T> set(T... values)
Convenience method for creating a Set.
T - values - the objects to be placed in a setSet containing the given itemspublic static <K,V> Map<K,V> map(Object... values)
Returns a map based on the inputs interpreted as key-value pairs.
i.e.
Map<String, Integer> result = map(key1, value1, key2, value2)
Due to the nature of mixed type arrays, any object can be used as a key or value pair, regardless of whether it matches the key or value generic types for the resulting map. Inserting the
incorrect type will not result in a failure. Care should be taken, or class cast exceptions can occur at execution time. For compile time type safery, prefer mapKeys(Object...)
If an uneven number of values are supplied, the final key will be omitted from the map.
values - an alternating set of key/value pairs matching the desired
resulting generic typespublic static <K> MapKeys<K> mapKeys(List<K> keys)
Supports a fluid syntax for creating maps conveniently. Unlike #map(List), also ensures compile time type safety using generics.
Returns a MapKeys, which will produce a Map when the MapKeys.to(List) method is invoked.
K - keys - the set of keys to be present in the map produced by the
resulting MapKeysMapKeys with the specified keysMapKeys.to(List)public static <K> MapKeys<K> mapKeys(K... keys)
Supports a fluid syntax for creating maps conveniently. Unlike map(Object...), also ensures compile time type safety using generics.
Returns a MapKeys, which will produce a Map when the MapKeys.to(Object...) method is invoked.
K - keys - the set of keys to be present in the map produced by the
resulting MapKeysMapKeys with the specified keysMapKeys.to(List)public static <T> EList<T> flatten(T[]... values)
Convenience method for creating an array containing the concatenated contents of the given set of arrays.
values - the set of arrays to be concatenated together. Null array
entries will be ignored.EList containing of the contents of the given items in
the given orderpublic static <T> EList<T> flatten(Collection<? extends Collection<? extends T>> collectionOfCollection)
Flattens the given collections of collections into a single list containing all entries.
This is useful in the scenario where we have grouped or categorised collections, such as a map of lists, when we want to search, sort, or iterate all entries.
For example:
Map<String, List<String>> referenceInformation; Collection<List<String>> values = referenceInformation.values(); EList<String> allValues = flatten(values);
T - collectionOfCollection - public static <T> EList<T> flatten(Collection<? extends T>... collectionOfCollection)
Flattens the given varargs of collections into a single list containing all entries.
This scenario is useful generally in test code where we want to assemble specific sets of collections from expected and unexpected values
For example:
List<String> expected = list("A", "E", "Y");
List<String> notExpected = list("B", "D", "Z");
List<String> vowels = giveMeOnlyVowels(flatten(expected, notExpected));
assertThat(vowels, is(expected));
T - collectionOfCollection - public static <T> EList<T> list(T... values)
Convenience method for creating an EList.
This can also be used to create a List as an alternative to Arrays.asList(Object...).
T - values - the objects to be placed in a listEList containing the given items in the given orderpublic static <T> EList<T> list(Collection<? extends T> values)
Convenience method for creating an EList from a Collection. The resulting EList will contain all elements from the given Collections.
The resulting order of the items is dependent on the order defined by the supplied collection.
T - values - the objects to be placed in a collectionEList containing the given items in the given orderpublic static <T> EList<T> list(Iterable<? extends T> values)
Convenience method for creating an EList from an Iterable. The resulting EList will contain all elements available from the Iterator obtained from the
Iterable.
The resulting order of the items is dependent on the order defined by the Iterator underlying the Iterable
T - values - the iterable containing the objects to be placed in a listEList containing the given items in the given orderpublic static <T> EList<T> list(Iterator<? extends T> values)
Convenience method for creating an EList from an Iterator.
The resulting order of the items is dependent on the order of the Iterator.
The given iterator should be considered stale once passed to this method. If Iterator.next() has already been called on the given iterator, those elements will not be present in the
resulting list.
T - values - the iterator containing the objects to be placed in a listEList containing the given items in the given orderpublic static <T> boolean isEmpty(Iterable<T> iterable)
Iterable is empty or nulliterable - an iterable, or nullIterable is empty or nullpublic static <T> boolean isEmpty(Collection<T> collection)
Collection is empty or nullcollection - a collection, or nullCollection is empty or nullpublic static <K,V> boolean isEmpty(Map<K,V> map)
Map is empty or nullmap - a map, or nullMap is empty or nullpublic static <T> boolean isNotEmpty(Iterable<T> iterable)
Iterable is not null and is not emptyiterable - a iterable, or nullIterable is not null and is not emptypublic static <T> boolean isNotEmpty(Collection<T> collection)
Collection is not null and is not emptycollection - a collection, or nullCollection is not null and is not emptypublic static <K,V> boolean isNotEmpty(Map<K,V> map)
Map is not null empty and is not nullmap - a map, or nullMap is not null and is not emptypublic static <K,V> Map<V,Set<K>> reverse(Map<K,V> map)
Map.
Returns a new map where the values of the given map are mapped to the set of keys.
This function is similar to the reverseUnique(Map) function, but can handle multiple keys of the input map having a single value.map - reverseUnique(Map)public static <K,V> Map<V,K> reverseUnique(Map<K,V> map)
Map, mapping each value in the given map to a single key value.
If a value may appear more than once, reverse(Map) should be used instead.map - reverse(Map)public static <T> Iterable<T> iterable(Iterator<T> iterator)
Convenience method for creating an Iterable from an Iterator so that it can easily be used in a for each loop.
Iterators used in this fashion should be discarded, as their state will be altered permanently once they are accessed (the for loop is executed).
Likewise, the resulting Iterable should only have Iterable.iterator() invoked once.
The Iterator returned from the resulting Iterable is the same as the supplied instance, and as such supports Iterator.remove() if the given iterator does.
e.g.
public boolean supportsColorModel(ImageReader reader, ColorModel colorModel) throws IOException {
Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
for (ImageTypeSpecifier imageTypeSpecifier : iterable(imageTypes)) {
if (colorModel.equals(imageTypeSpecifier.getColorModel())) {
return true;
}
}
return false;
}
T - iterator - the Iterator to wrap in an Iterablepublic static <T> Iterable<T> iterable(Enumeration<T> enumeration)
Convenience method for creating an Iterable from an Enumeration so that it can easily be used in a for each loop.
Enumerations used in this fashion should be discarded, as their state will be altered once the for loop is executed.
Likewise, the resulting Iterable should only have Iterable.iterator() invoked once.
The iterator created by the resulting iterable does not support Iterator.remove()
e.g.
NetworkInterface networkInterface;
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
for (InetAddress inetAddress : Expressive.iterable(inetAddresses)) {
...
}
T - enumeration - the enumeration to wrap in an iterableCopyright © 2013 Atomic Leopard. All Rights Reserved.