Liz Douglass

Lists, maps etc

with one comment

On our project we are using some utility/toolbox Java classes that Tom, Alex and others on our project have made.  You can find versions of them similar to the ones we are using on our project in Tom’s example J2EE web application. There is also a slightly different flavour of them available here. Most of the classes help you to manage and mutate collections.  We have found them to be very useful on our project. In fact, we currently have 152 uses of the create method from the Lists class (see below) in our code base (which works out to be about one in every three classes).

We use the Lists the most overall. Tom and I used it a couple of weeks ago to get some data from a repository. This was based on what we’d grabbed in another repository and then narrowed the result further using a select statement. All this in only three quickly assembled lines of Java:

Listfoos=fooRepository.findBySomeCriteria();
Listbars=Lists.map(foos,new BarMapper(barRepository));
bar=Lists.select(bars,new CompletedBarsMatcher());

Here is the code in the Lists class:

import org.hamcrest.Matcher;
import org.hamcrestcollections.Function;
import org.hamcrestcollections.FunctionMapper;
import org.hamcrestcollections.RejectMatcher;
import org.hamcrestcollections.Selector;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class Lists {

    public static  List create() {
        return new ArrayList();
    }

    public static  List create(Collection instances) {
        return new ArrayList(instances);
    }

    public static  List create(T... instances) {
        List list = create();
        Collections.addAll(list, instances);
        return list;
    }

    public static  T first(List list) {
        return first(list, null);
    }

    public static  T first(List list, T defaultValue) {
        return isEmpty(list) ? defaultValue : list.get(0);
    }

    public static  T last(List list) {
        return last(list, null);
    }

    public static  T last(List list, T defaultValue) {
        return isEmpty(list) ? defaultValue : list.get(list.size() - 1);
    }

    public static  boolean isEmpty(List list) {
        return (list == null) || list.isEmpty();
    }

    public static  boolean isNotEmpty(List list) {
        return !isEmpty(list);
    }

    public static  boolean contains(List list, Matcher matcher) {
        for (T item : list) {
            if (matcher.matches(item)) {
                return true;
            }
        }
        return false;
    }

    public static  T findFirst(List list, Matcher matcher) {
        for (T item : list) {
            if (matcher.matches(item)) {
                return item;
            }
        }
        return null;
    }

    public static  int count(List list, Matcher matcher) {
        int count = 0;
        for (T item : list) {
            if (matcher.matches(item)) {
                count++;
            }
        }
        return count;
    }

    public static  List select(List list, Matcher matcher) {
        return (List) Selector.select(list, matcher);
    }

    public static  List reject(List list, Matcher matcher) {
        return (List) RejectMatcher.reject(list, matcher);
    }

    public static  List map(List list, Function function) {
        return (List) FunctionMapper.map(list, function);
    }

    public static  U reduce(List list, U initialValue, Reducer reducer) {
        U result = initialValue;
        for (T item : list) {
            result = reducer.reduce(item, result);
        }
        return result;
    }
}

You can see that several of the methods call functions in the hamcrest-collections library in this version of Lists. Tom was saying that he doesn’t like that they return an Iterable that then needs to be cast to a List (in this case). Note also the implementation of the reduce function that Tom and Alex wrote. It is actually mapping and reducing – but I’ll let Alex give all the details about that one in his promised blog post 🙂

Advertisements

Written by lizdouglass

September 7, 2009 at 10:27 am

Posted in Uncategorized

One Response

Subscribe to comments with RSS.

  1. […] what I liked a lot, and what I didn’t like a lot. My friend Liz Douglass has also written a post sharing our experience, and I will just complement it a […]


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: