Chapter 8. Utilities

Table of Contents

8.1. Lists
8.1.1. Converting primitive type arrays
8.1.2. Concatenating arrays
8.1.3. Safe iteration
8.1.4. Working with parts of an array or Collection
8.2. Operators
8.3. Sorter
8.4. Strings
8.4.1. Concatenation
8.4.2. Interspersing
8.4.3. Safe length
8.4.4. String representation of an array
8.5. Utils

There are some general utility methods used within FunctionalJ that are available in the classes of the util package.

8.1. Lists

Although named "Lists", this class contains methods to operate on arrays and general collections as well.

8.1.1. Converting primitive type arrays

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.

8.1.2. Concatenating arrays

To concatenate two arrays into a single array, use Object[] Lists.concat(Object[], Object[]).

8.1.3. Safe iteration

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))
{
    // ...
}

8.1.4. Working with parts of an array or Collection

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
You can also split up an array or 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:

  • If the specified index is less than or equal to zero, the first list of the returned pair is empty and the second list contains all of the elements of the specified list.
  • If the specified index is greater than or equal to the size of the list, the first list of the returned pair contains all of the elements of the specified list and the second list is empty.
  • If the specified list is empty, two empty lists are returned regardless of the specified index.

8.2. Operators

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;

8.3. Sorter

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}));

8.4. Strings

The Strings class contains a few String-related convenience methods.

8.4.1. Concatenation

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.

8.4.2. Interspersing

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";

8.4.3. Safe length

The Strings.length(String) method returns the length of a string, but returns 0 instead of throwing a NullPointerException if the string is null.

8.4.4. String representation of an array

The Strings.toString(Object[]) method returns the String representation of an array, following the "[A, B, C, D]" format, and using the String representation of each object in the array.

8.5. Utils

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);