Q:

Comparator of generic class in Java

I would like the following thing to work:  I have a generic class public class TypeType<T1,T2> that has two fields public T1 t1 and public T2 t2.  I would like to write two public static Comparator methods for this class, compareFirst() and compareSecond().  I would like the first method to compare only based on the t1s, and the second only based on the t2s.  The first method produces valid output if T1 implements Comparable<T1> and the second if T2 implements Comparable<T2>.
Is something like this possible?  Is there a way to check that T1 and T2 implement Comparable?  If not, can I still make this work?  (This class is only for internal usage.)
Thanks a bunch!
public class TypeType<T1,T2> {

    public T1 t1;
    public T2 t2;

    public TypeType() {
        this(null, null);
    }

    public TypeType(T1 t1, T2 t2) {
        this.t1 = t1;
        this.t2 = t2;
    }

    /*
     * I know that the following does not work.
     * This is just pseudo-code for what I would like to achieve.
     */
    public static Comparator<TypeType<?,?>> comparatorFirst() {
        return new Comparator<TypeType<?,?>> {
            public int compare(TypeType<?,?> tt1, TypeType<?,?> tt2) {
                return tt1.t1.compareTo(tt2.t1);
            }
        }
    }

}

In case interesting to others:  I ended up implementing @pbabcdefp's Java 8 solution using Lambda Expressions.
public static <T1 extends Comparable<? super T1>> Comparator<TypeType<T1,?>> compareOne() {
    return (tt1, tt2) -> tt1.t1.compareTo(tt2.t1);
}

public static <T2 extends Comparable<? super T2>> Comparator<TypeType<?,T2>> compareTwo() {
    return (tt1, tt2) -> tt1.t2.compareTo(tt2.t2);
}

public static <T1 extends Comparable<? super T1>,T2 extends Comparable<? super T2>> Comparator<TypeType<T1,T2>> compareOneThenTwo() {
    return (tt1, tt2) -> {
        if ( tt1.t1.equals(tt2.t1) )
            return tt1.t2.compareTo(tt2.t2);
        else
            return tt1.t1.compareTo(tt2.t1);
    };
}

public static <T1 extends Comparable<? super T1>,T2 extends Comparable<? super T2>> Comparator<TypeType<T1,T2>> compareTwoThenOne() {
    return (tt1, tt2) -> {
        if ( tt1.t2.equals(tt2.t2) )
            return tt1.t1.compareTo(tt2.t1);
        else
            return tt1.t2.compareTo(tt2.t2);
    };
}

A:

There are several good answers here, but note that with Java 8 you can return a lambda.
public static <T1 extends Comparable<? super T1>> Comparator<TypeType<T1,?>> comparatorFirst() {
    return (tt1, tt2) -> tt1.t1.compareTo(tt2.t1);
}

public static <T2 extends Comparable<? super T2>> Comparator<TypeType<?, T2>> comparatorSecond() {
    return (tt1, tt2) -> tt1.t2.compareTo(tt2.t2);
}

