diff --git a/common/src/main/java/io/netty/util/internal/ObjectUtil.java b/common/src/main/java/io/netty/util/internal/ObjectUtil.java index 253fb3a317..6cd5883082 100644 --- a/common/src/main/java/io/netty/util/internal/ObjectUtil.java +++ b/common/src/main/java/io/netty/util/internal/ObjectUtil.java @@ -79,6 +79,28 @@ public final class ObjectUtil { return l; } + /** + * Checks that the given argument is in range. If it is not, throws {@link IllegalArgumentException}. + * Otherwise, returns the argument. + */ + public static int checkInRange(int i, int start, int end, String name) { + if (i < start || i > end) { + throw new IllegalArgumentException(name + ": " + i + " (expected: " + start + "-" + end + ")"); + } + return i; + } + + /** + * Checks that the given argument is in range. If it is not, throws {@link IllegalArgumentException}. + * Otherwise, returns the argument. + */ + public static long checkInRange(long l, long start, long end, String name) { + if (l < start || l > end) { + throw new IllegalArgumentException(name + ": " + l + " (expected: " + start + "-" + end + ")"); + } + return l; + } + /** * Checks that the given argument is neither null nor empty. * If it is, throws {@link NullPointerException} or {@link IllegalArgumentException}.