Skip to content

Commit

Permalink
Use ObjectUtil#checkInRange instead of if-else (netty#12059)
Browse files Browse the repository at this point in the history
Motivation:
We should use `ObjectUtil#checkInRange` instead of if-else to make the code look better and easier to understand.

Modification:
Replaced if-else with `ObjectUtil#checkInRange`

Result:
More readable and better code
  • Loading branch information
hyperxpro authored Jan 31, 2022
1 parent 561a7cb commit 62ef8f9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,11 @@ public JZlibEncoder(ZlibWrapper wrapper, int compressionLevel) {
* @throws CompressionException if failed to initialize zlib
*/
public JZlibEncoder(ZlibWrapper wrapper, int compressionLevel, int windowBits, int memLevel) {

if (compressionLevel < 0 || compressionLevel > 9) {
throw new IllegalArgumentException(
"compressionLevel: " + compressionLevel +
" (expected: 0-9)");
}
if (windowBits < 9 || windowBits > 15) {
throw new IllegalArgumentException(
"windowBits: " + windowBits + " (expected: 9-15)");
}
if (memLevel < 1 || memLevel > 9) {
throw new IllegalArgumentException(
"memLevel: " + memLevel + " (expected: 1-9)");
}
ObjectUtil.checkInRange(compressionLevel, 0, 9, "compressionLevel");
ObjectUtil.checkInRange(windowBits, 9, 15, "windowBits");
ObjectUtil.checkInRange(memLevel, 1, 9, "memLevel");
ObjectUtil.checkNotNull(wrapper, "wrapper");

if (wrapper == ZlibWrapper.ZLIB_OR_NONE) {
throw new IllegalArgumentException(
"wrapper '" + ZlibWrapper.ZLIB_OR_NONE + "' is not " +
Expand Down Expand Up @@ -208,17 +198,9 @@ public JZlibEncoder(int compressionLevel, byte[] dictionary) {
* @throws CompressionException if failed to initialize zlib
*/
public JZlibEncoder(int compressionLevel, int windowBits, int memLevel, byte[] dictionary) {
if (compressionLevel < 0 || compressionLevel > 9) {
throw new IllegalArgumentException("compressionLevel: " + compressionLevel + " (expected: 0-9)");
}
if (windowBits < 9 || windowBits > 15) {
throw new IllegalArgumentException(
"windowBits: " + windowBits + " (expected: 9-15)");
}
if (memLevel < 1 || memLevel > 9) {
throw new IllegalArgumentException(
"memLevel: " + memLevel + " (expected: 1-9)");
}
ObjectUtil.checkInRange(compressionLevel, 0, 9, "compressionLevel");
ObjectUtil.checkInRange(windowBits, 9, 15, "windowBits");
ObjectUtil.checkInRange(memLevel, 1, 9, "memLevel");
ObjectUtil.checkNotNull(dictionary, "dictionary");

int resultCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,9 @@ public JdkZlibEncoder(ZlibWrapper wrapper) {
* @throws CompressionException if failed to initialize zlib
*/
public JdkZlibEncoder(ZlibWrapper wrapper, int compressionLevel) {
if (compressionLevel < 0 || compressionLevel > 9) {
throw new IllegalArgumentException(
"compressionLevel: " + compressionLevel + " (expected: 0-9)");
}
ObjectUtil.checkInRange(compressionLevel, 0, 9, "compressionLevel");
ObjectUtil.checkNotNull(wrapper, "wrapper");

if (wrapper == ZlibWrapper.ZLIB_OR_NONE) {
throw new IllegalArgumentException(
"wrapper '" + ZlibWrapper.ZLIB_OR_NONE + "' is not " +
Expand Down Expand Up @@ -138,10 +136,7 @@ public JdkZlibEncoder(byte[] dictionary) {
* @throws CompressionException if failed to initialize zlib
*/
public JdkZlibEncoder(int compressionLevel, byte[] dictionary) {
if (compressionLevel < 0 || compressionLevel > 9) {
throw new IllegalArgumentException(
"compressionLevel: " + compressionLevel + " (expected: 0-9)");
}
ObjectUtil.checkInRange(compressionLevel, 0, 9, "compressionLevel");
ObjectUtil.checkNotNull(dictionary, "dictionary");

wrapper = ZlibWrapper.ZLIB;
Expand Down

0 comments on commit 62ef8f9

Please sign in to comment.