Created
January 13, 2013 22:24
-
-
Save mkscrg/4526536 to your computer and use it in GitHub Desktop.
Full example for SO question @ http://stackoverflow.com/questions/14308275/why-does-this-generic-code-produce-a-nosuchmethoderror
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
ensureLessThanWithCast(6, 5); | |
ensureLessThan(6, 5); | |
} | |
public static <N extends Number & Comparable<N>, S extends N> S ensureLessThanWithCast(N threshold, S input) { | |
if (((N) input).compareTo(threshold) >= 0) { | |
throw new IllegalArgumentException("Input " + input + " is not less than " + threshold); | |
} | |
return input; | |
} | |
public static <N extends Number & Comparable<N>, S extends N> S ensureLessThan(N threshold, S input) { | |
if (input.compareTo(threshold) >= 0) { | |
throw new IllegalArgumentException("Input " + input + " is not less than " + threshold); | |
} | |
return input; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment