diff --git a/libs/common/src/main/java/org/opensearch/common/round/BidirectionalLinearSearcher.java b/libs/common/src/main/java/org/opensearch/common/round/BidirectionalLinearSearcher.java index 6d16b41e6a2de..5c3dcf2bd4708 100644 --- a/libs/common/src/main/java/org/opensearch/common/round/BidirectionalLinearSearcher.java +++ b/libs/common/src/main/java/org/opensearch/common/round/BidirectionalLinearSearcher.java @@ -28,7 +28,9 @@ class BidirectionalLinearSearcher implements Roundable { private final long[] descending; BidirectionalLinearSearcher(long[] values, int size) { - assert size > 0 : "at least one value must be present"; + if (size <= 0) { + throw new IllegalArgumentException("at least one value must be present"); + } int len = (size + 1) >>> 1; // rounded-up to handle odd number of values ascending = new long[len]; diff --git a/libs/common/src/main/java/org/opensearch/common/round/BinarySearcher.java b/libs/common/src/main/java/org/opensearch/common/round/BinarySearcher.java index f14b311a36edc..b9d76945115ed 100644 --- a/libs/common/src/main/java/org/opensearch/common/round/BinarySearcher.java +++ b/libs/common/src/main/java/org/opensearch/common/round/BinarySearcher.java @@ -23,7 +23,9 @@ class BinarySearcher implements Roundable { private final int size; BinarySearcher(long[] values, int size) { - assert size > 0 : "at least one value must be present"; + if (size <= 0) { + throw new IllegalArgumentException("at least one value must be present"); + } this.values = values; this.size = size; diff --git a/libs/common/src/test/java/org/opensearch/common/round/RoundableTests.java b/libs/common/src/test/java/org/opensearch/common/round/RoundableTests.java index 5623dc80d1a16..ae9f629c59024 100644 --- a/libs/common/src/test/java/org/opensearch/common/round/RoundableTests.java +++ b/libs/common/src/test/java/org/opensearch/common/round/RoundableTests.java @@ -41,17 +41,17 @@ public void testFloor() { } } - public void testAssertions() { - AssertionError exception; - - exception = assertThrows(AssertionError.class, () -> new BinarySearcher(new long[0], 0)); - assertEquals("at least one value must be present", exception.getMessage()); - exception = assertThrows(AssertionError.class, () -> new BidirectionalLinearSearcher(new long[0], 0)); - assertEquals("at least one value must be present", exception.getMessage()); - - exception = assertThrows(AssertionError.class, () -> new BinarySearcher(new long[] { 100 }, 1).floor(50)); - assertEquals("key must be greater than or equal to 100", exception.getMessage()); - exception = assertThrows(AssertionError.class, () -> new BidirectionalLinearSearcher(new long[] { 100 }, 1).floor(50)); - assertEquals("key must be greater than or equal to 100", exception.getMessage()); + public void testFailureCases() { + Throwable throwable; + + throwable = assertThrows(IllegalArgumentException.class, () -> new BinarySearcher(new long[0], 0)); + assertEquals("at least one value must be present", throwable.getMessage()); + throwable = assertThrows(IllegalArgumentException.class, () -> new BidirectionalLinearSearcher(new long[0], 0)); + assertEquals("at least one value must be present", throwable.getMessage()); + + throwable = assertThrows(AssertionError.class, () -> new BinarySearcher(new long[] { 100 }, 1).floor(50)); + assertEquals("key must be greater than or equal to 100", throwable.getMessage()); + throwable = assertThrows(AssertionError.class, () -> new BidirectionalLinearSearcher(new long[] { 100 }, 1).floor(50)); + assertEquals("key must be greater than or equal to 100", throwable.getMessage()); } }