Skip to content

Instantly share code, notes, and snippets.

@mkscrg
Created January 13, 2013 22:24
Show Gist options
  • Save mkscrg/4526536 to your computer and use it in GitHub Desktop.
Save mkscrg/4526536 to your computer and use it in GitHub Desktop.
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