tags:

views:

89

answers:

1

I'm implementing a high-order split function, which takes a collection of items to be splitted, a filter delegate which defines the boundary criteria of the split, and two ReturnDelegate, which are delegates that provides the concrete collection type of the return collections (so I don't need to fix ArrayList or HashSet in the split function, but rather, leaving the creation of the return collections to the calling code, yielding flexibility). The return is a Tuple2 class, which is basically a wrapper around two heterogeneous objects.

Here's the split method:

public static <T, T1 extends Collection<T>, T2 extends Collection<T>>
    Tuple2<T1, T2> split(final Collection<T> coll,
                         final FilterDelegate<T> filterDelegate,
                         final FilterReturnDelegate<T1> truthyDelegate1,
                         final FilterReturnDelegate<T2> falsyDelegate2) {
    final Collection<T> t1 = truthyDelegate1.createReturnCollection();
    final Collection<T> t2 = falsyDelegate2.createReturnCollection();

    for (final T item : coll) {
        if (filterDelegate.filter(item)) {
            t1.add(item);
        } else {
            t2.add(item);
        }
    }

    final Tuple2<T1, T2> retval = new Tuple2<T1, T2>();
    retval.setItem1(t1);
    retval.setItem2(t2);

    return retval;
}

Here's the Tuple2 class. It's a simple generic-aware wrapper of heterogeneous objects.

public final class Tuple2<T1, T2> {

    private T1 _item1;

    private T2 _item2;

    public Tuple2() {
    }

    public Tuple2(final T1 item1, final T2 item2) {
        _item1 = item1;
        _item2 = item2;
    }

    public T1 getItem1() {
        return _item1;
    }

    public T2 getItem2() {
        return _item2;
    }

    public void setItem1(final T1 item1) {
        _item1 = item1;
    }

    public void setItem2(final T2 item2) {
        _item2 = item2;
    }

}

FilterDelegate:

public interface FilterDelegate<T> {

    /**
     *
     * @param item The item to be filtered.
     * @return true if the item should be retained. false if the item should be
     */
    boolean filter(T item);

}

FilterReturnDelegate:

public interface FilterReturnDelegate<R extends Collection<?>> {

    R createReturnCollection();

}

However, the above split method doesn't even compile. Javac is complaining on the lines

retval.setItems(t1); retval.setItems(t2);

that "The method setItem1(T1) in the type Tuple2<T1,T2> is not applicable for the arguments (Collection<T>)".

Wondering what am I doing wrong here?

+3  A: 

Your t1 and t2 should be of type T1 and T2:

final T1 t1 = truthyDelegate1.createReturnCollection();
final T2 t2 = falsyDelegate2.createReturnCollection();

Collection<T> isn't a subtype of T1, so setItem(t1) would fail.

notnoop
Darn...such a silly mistake! Thanks!
EnToutCas