Community
Participate
Working Groups
This code is meant to not emit any warnings, but does.. Eclipse doesn't seem to be honouring the annotations. Theoretically the @SuppressWarnings ({"unchecked"}) should tell Eclipse not to emit this. "Type safety: The cast from T to Comparable<T> is actually checking against the erased type Comparable" % cat Test.java import java.util.Comparator; /** */ public class Test { /** * * @param <T> * @param comparator * @param first * @param second * @return a value * @throws ClassCastException */ @SuppressWarnings ({"unchecked"}) public static <T> int compare(final Comparator<? super T> comparator, final T first, final T second) throws ClassCastException { // Same object (or both null) - quick exit. if (first == second) { return 0; } // If either is null, it is less than the other. if (first == null) { return -1; } else if (second == null) { return 1; } // Ensure that compare is consistent with equals if (first.equals(second)) { return 0; } // If possible use {@link Comparator#compare}. if (comparator != null) { return comparator.compare(first, second); } // Fallback to the {@link Comparable#compareTo} implementation to determine. return ((Comparable<T>) first).compareTo(second); } }
*** This bug has been marked as a duplicate of 83739 ***