SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:Why not add generics to the combo box?
If you need to compare on V without wanting to use a java.util.Comparator you can add bounds to it:
Note that the ? super V part is there to allow classes like java.sql.Timestamp - classes that extends another Comparable class.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:Make MyString implement Comparable<MyString>.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
And if it is, you might still be able to wrap it in a MyStringWrapper class of your own, and have it implement Comparable.Campbell Ritchie wrote:If it isn’t a final class you can extend it and make it implement Comparable...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Rob Spoor wrote:You'll have to drop the extends Comparable<? super V> part and use a Comparator<V>. However, this means that you either have to provide a Comparator<String> if you want a MyComboBox<String>, or you need to sacrifice a bit of type safety - in a similar way to how TreeMap does it. TreeMap allows you to use a non-comparable class as the key type without needing to provide a Comparator, which will cause a ClassCastException during runtime.
What good ideasWinston Gutkowski wrote: . . . wrap it in a MyStringWrapper class . . . have your wrapper class implement CharSequence, . . .
albert kao wrote:However, the commented out class StrComparator has compile error if it is uncommented.
Please help to fix the compile error.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:
albert kao wrote:However, the commented out class StrComparator has compile error if it is uncommented.
Please help to fix the compile error.
V doesn't have a compareTo method, because V is not bound. Change it into this:
Note that I also made it implement Comparator<V> which removes the need of casting to V.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
If it doesn't implement Comprarable, it seems odd that it would implement CharSequence; the authors don't appear to have had ease-of-use high on their list when they wrote it. Or maybe they expect everybody to use toString() if they need one.Campbell Ritchie wrote:If MyString implements CharSequence, can you use Comparator<T extends CharSequence> or similar?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
If it doesn't implement Comprarable, it seems odd that it would implement CharSequence; the authors don't appear to have had ease-of-use high on their list when they wrote it. Or maybe they expect everybody to use toString() if they need one.Campbell Ritchie wrote:If MyString implements CharSequence, can you use Comparator<T extends CharSequence> or similar?
OP could help us out by providing its public API...or at least the relevant bits.
Winston
albert kao wrote:
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
For the class definition: yes.Campbell Ritchie wrote:If MyString implements CharSequence, can you use Comparator<T extends CharSequence> or similar?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
albert kao wrote:It implements:
...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |