Table of Contents
There are some general utility methods used within FunctionalJ that are available in
the classes of the util package.
Although named "Lists", this class contains methods to operate on arrays and general collections as well.
To convert an array of a primitive type to a List of
the corresponding wrapper class, use List<T>
Lists.asList(t[]). It is overloaded for each primitive type.
To convert a collection of objects of a wrapper class into an array of the
corresponding primitive type, use the appropriate
as[type]Array, such as int[]
asIntArray(Collection<Integer>), char[]
asCharArray(Collection<Character>), etc.
To concatenate two arrays into a single array, use
Object[] Lists.concat(Object[], Object[]).
The Lists class contains methods to safely iterate
over an array or a Collection, treating a
null value as an empty set, without throwing a
NullPointerException. You can also safely obtain the
size of an array or Collection using
Lists.sizeOf().
If you are using JDK 1.4, you can use:
// Iterate over an array Object[] array = ...; for (Iterator iter = Lists.iterator(array); iter.hasNext();) { // ... } // Iterate over a Collection Collection coll = ...; for (Iterator iter = Lists.iterator(coll); iter.hasNext();) { // ... }If you are using JDK 1.5, you can use:
// Iterate over an array T[] array = ...; for (T next : Lists.iterable(array)) { // ... } // Iterate over a Collection Collection<T> coll = ...; for (T next : Lists.iterable(coll)) { // ... }
You can work with parts of an array or Collection
using the following methods of the List class:
first: obtain the first element
last: obtain the last element
tail: obtain every element except the first
init: obtain every element except the last
List at a
specific index, to obtain a Pair of lists:
public static <T> Pair<List<T>,List<T>> splitAt(int p_index, List<T> p_list); public static <T> Pair<List<T>,List<T>> splitAt(int p_index, T[] p_array) public static Pair<Object[],Object[]> splitAtArray(int p_index, Object[] p_array)In the returned
Pair, the first list contains as
many elements as the specified index; the second list contains the rest of the
elements.
Particular cases:
The Operators class contains operators defined as
ready-to-use functions:
Function2<Integer,Integer,Integer> add; Function2<Integer,Integer,Integer> subtract; Function2<Integer,Integer,Integer> multiply; Function2<Integer,Integer,Integer> divide; Function2<Integer,Integer,Integer> min; Function2<Integer,Integer,Integer> max; Function2<Boolean,Boolean,Boolean> and; Function2<Boolean,Boolean,Boolean> or; Function1<Boolean,Boolean> not; Function2<String,String,String> concat;
The Sorter class contains the
sortBy(List p_list, String[] p_properties) method
that sorts a list according to the given properties, using the next property when the
values for the previous property are the same. Because the properties by which to
sort are given by name, reflection is used to obtain the value of those properties.
It is assumed that the values of the properties implement the
Comparable interface.
For example, given the following Car class:
public class Car { private String m_make; private String m_model; private int m_year; private String m_color; public Car(String p_make, String p_model, int p_year, String p_color) { m_make = p_make; m_model = p_model; m_year = p_year; m_color = p_color; } public String getMake() { return m_make; } public String getModel() { return m_model; } public int getYear() { return m_year; } public String getColor() { return m_color; } }Suppose you have the following cars:
Car car1 = new Car("Acura","Integra",1992,"Black"); Car car2 = new Car("Mazda","Protege5",2002,"Blue"); Car car3 = new Car("Acura","TSX",2000,"Silver"); Car car4 = new Car("Acura","RSX",2000,"Black"); List cars = Arrays.asList(new Car[] {car1,car2,car3,car4});To sort the cars by make and model, you could use:
Sorter.sortBy(cars, new String[] {"make", "model"}); Assert.assertEquals(cars, Arrays.asList(new Car[] {car1,car4,car3,car2}));
The Strings class contains a few
String-related convenience methods.
The Strings.concat(Collection) and
Strings.concat(Object[]) methods concatenate a set
of objects into a single String, by using the
String representation of each object.
Interspersing consists of inserting a string in between each
pair of strings in a List or array. Combined with
concat, this can be useful for example to insert a
space or comma between each string in a list, without doing so after the last string:
String[] strings = {"A", "B", "C", "D", "E"}; String result = Strings.concat(Strings.intersperse(", ", strings)); assertEquals(result, "A, B, C, D, E";
The Strings.length(String) method returns the length
of a string, but returns 0 instead of throwing a
NullPointerException if the string is
null.
The Utils class contains some miscellaneous utility
methods:
// Verifies if the parameter is null, and throws an exception if it is. void disallowNull(Object p_parameter, String p_name); // Returns an int from an Object, using the numerical value of its String representation. int getInt(Object p_object); // Returns the result of comparing two objects, by considering two null values to be // equal, a null to come after a non-null value, and by using the compareTo() method // to compare two non-null values. int safeCompare(Comparable p_first, Comparable p_second); // Verifies if two arrays of objects are equal, considering null values to be equal, // and comparing corresponding elements of each array. boolean safeEqualArrays(Object[] p_first, Object[] p_second); // Verifies if two objects are equal, by considering two null values to be equal, a // null and non-null value not to be equal, and by using the equals() method to // compare two non-null values. boolean safeEquals(Object p_first, Object p_second);