Skip to content

Commit cb7c159

Browse files
committed
Enforce at least one value or defaultValue to always be non-null when IgnoreAbove is initialized
1 parent b07ddf4 commit cb7c159

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

server/src/main/java/org/elasticsearch/index/IgnoreAbove.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
import org.elasticsearch.xcontent.XContentString;
1313

14-
import java.util.Objects;
15-
1614
import static org.elasticsearch.index.IndexSettings.IGNORE_ABOVE_DEFAULT_LOGSDB_INDICES;
1715
import static org.elasticsearch.index.IndexSettings.IGNORE_ABOVE_DEFAULT_STANDARD_INDICES;
1816

@@ -33,8 +31,13 @@ public class IgnoreAbove {
3331
private final Integer defaultValue;
3432

3533
public IgnoreAbove(Integer value, Integer defaultValue) {
34+
if (value == null && defaultValue == null) {
35+
throw new IllegalArgumentException(
36+
"IgnoreAbove must be initialized with at least one non-null argument: either 'value' or 'defaultValue'"
37+
);
38+
}
3639
this.value = value;
37-
this.defaultValue = Objects.requireNonNull(defaultValue);
40+
this.defaultValue = defaultValue;
3841
}
3942

4043
public int get() {

server/src/test/java/org/elasticsearch/index/IgnoreAboveTests.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,16 @@ public void test_ignore_above_with_value_and_default() {
2323
assertTrue(ignoreAbove.isSet());
2424
}
2525

26-
public void test_ignore_above_with_default_only_is_valid() {
26+
public void test_ignore_above_with_value_only() {
27+
// given
28+
IgnoreAbove ignoreAbove = IgnoreAbove.builder().value(123).build();
29+
30+
// when/then
31+
assertEquals(123, ignoreAbove.get());
32+
assertTrue(ignoreAbove.isSet());
33+
}
34+
35+
public void test_ignore_above_with_default_only() {
2736
// given
2837
IgnoreAbove ignoreAbove = IgnoreAbove.builder().defaultValue(456).build();
2938

@@ -41,14 +50,9 @@ public void test_ignore_above_with_same_value_and_default() {
4150
assertFalse(ignoreAbove.isSet());
4251
}
4352

44-
public void test_ignore_above_with_value_only_should_throw() {
45-
// given/when/then
46-
assertThrows(NullPointerException.class, () -> IgnoreAbove.builder().value(123).build());
47-
}
48-
4953
public void test_ignore_above_with_nothing_should_throw() {
5054
// given/when/then
51-
assertThrows(NullPointerException.class, () -> IgnoreAbove.builder().build());
55+
assertThrows(IllegalArgumentException.class, () -> IgnoreAbove.builder().build());
5256
}
5357

5458
public void test_string_isIgnored() {

0 commit comments

Comments
 (0)