3

When I try to complete a task, there's one case need to be handled: throw exception of generic type of class is not comparable. Refer to the following code for the detail.

 public class C <T>
    {
        public C()
        {
            // throw exception if T is not comparable
        }
    }
Da Ma
  • 391
  • 4
  • 17
  • This class can also accept class which is not subclass of Comparator; and there is another function to assign there comparator for it. If key is un-comparable and not comparator is not assign, throw exception. – Da Ma May 03 '15 at 01:33

6 Answers6

2

You can enforce the generic to be a subclass of Comparator like so:

public class C <T extends Comparator> {
    public C(){
    }
}

As you see in the below code, it would be a good idea to add another generic (here it is K), which you supply to Comparator, since the generic of Comparator will otherwise default to Object.

public class C <K, T extends Comparator<K>> {
    public C(){
    }
}

You generally use this in the form of T x K, where T is the generic, x is super or extends and K is the class/interface.

Comparator docs

SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
  • Thanks for your answer, but this class can also accept class which is not subclass of Comparator; and there is another function to assign there comparator for it. – Da Ma May 03 '15 at 01:33
1

You should make sure that the generic parameter T is a Comparableby writing:

public class C <T extends Comparable>
alainlompo
  • 4,414
  • 4
  • 32
  • 41
1

There are two ways:

1) Make it T extends Comparable, so you know it will always be.

2) In the constructor, pass Class < T > as a parameter, so you'll know at runtime what T is. (because it's erased)

Luciano
  • 8,552
  • 5
  • 32
  • 56
  • A subclass of a generic class can get at the generic type using reflection: http://stackoverflow.com/questions/1901164/get-type-of-a-generic-parameter-in-java-with-reflection – Mirvnillith May 02 '15 at 14:35
1

Add a generic constraint, that is would be better since it will be handle it at compile time rather than throw an exception on runtime.

class C <T extends Comparable>
Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44
1

You should verify that your parameter T is Comparable.

 public class C <T extends Comparable>

This is the same if you want a generic type implements some interface.

public class C <T implements <interface what you want> >

Or if you want that were a superclass of another one.

public class C <T super <class what you want> > 
Shondeslitch
  • 1,049
  • 1
  • 13
  • 26
0

If you always want the type T to implement Comparable you can enforce this as follows.

public class C<T extends Comparable<? super T>> {

    public C() {

    }
}

This is better than throwing an exception at runtime. It will not even compile if someone tries to write new C<some non-Comparable type>(). Note that Comparable is a generic type, so it should not be used without type parameters; it should not simply be

public class C<T extends Comparable>

If the type T will not always implement Comparable, but you want a specific constructor that will only work for Comparable types, then the answer is that you can't do this with constructors, but you can do it with a static method.

public class C<T> {

    public C() {
        // Constructor that works with any T
    }

    public static <T extends Comparable<? super T>> C<T> getNew() {
        C<T> c = new C<>();
        // Do stuff that depends on T being Comparable
        return c;
    }
}
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116