diff --git a/warehouse/query-core/src/main/java/datawave/query/util/sortedset/MultiSetBackedSortedSet.java b/warehouse/query-core/src/main/java/datawave/query/util/sortedset/MultiSetBackedSortedSet.java index 6e3d2555cc3..880827b3f12 100644 --- a/warehouse/query-core/src/main/java/datawave/query/util/sortedset/MultiSetBackedSortedSet.java +++ b/warehouse/query-core/src/main/java/datawave/query/util/sortedset/MultiSetBackedSortedSet.java @@ -5,6 +5,7 @@ import java.util.Comparator; import java.util.Iterator; import java.util.List; +import java.util.NoSuchElementException; import java.util.SortedSet; import java.util.TreeSet; @@ -52,8 +53,11 @@ public int size() { @Override public boolean isEmpty() { + if (sets == null) { + return true; + } for (SortedSet set : sets) { - if (!set.isEmpty()) { + if (set != null && !set.isEmpty()) { return false; } } @@ -141,26 +145,38 @@ public SortedSet tailSet(E fromElement) { } @Override - public E first() { + public E first() throws NoSuchElementException { + if (sets == null || sets.isEmpty()) { + throw new NoSuchElementException("No elements in input sets"); + } SortedSet firstSet = new TreeSet<>(comparator()); for (SortedSet set : sets) { - E s = set.first(); - if (s != null) { + if (set != null && !set.isEmpty()) { + E s = set.first(); firstSet.add(s); } } + if (firstSet.isEmpty()) { + throw new NoSuchElementException("No elements in input sets"); + } return firstSet.first(); } @Override - public E last() { + public E last() throws NoSuchElementException { + if (sets == null || sets.isEmpty()) { + throw new NoSuchElementException("No elements in input sets"); + } SortedSet lastSet = new TreeSet<>(comparator()); for (SortedSet set : sets) { - E s = set.last(); - if (s != null) { + if (set != null && !set.isEmpty()) { + E s = set.last(); lastSet.add(s); } } + if (lastSet.isEmpty()) { + throw new NoSuchElementException("No elements in input sets"); + } return lastSet.last(); } } diff --git a/warehouse/query-core/src/test/java/datawave/query/util/sortedset/MultiSetBackedSortedSetTest.java b/warehouse/query-core/src/test/java/datawave/query/util/sortedset/MultiSetBackedSortedSetTest.java index 54e8b4bfe17..091ba311620 100644 --- a/warehouse/query-core/src/test/java/datawave/query/util/sortedset/MultiSetBackedSortedSetTest.java +++ b/warehouse/query-core/src/test/java/datawave/query/util/sortedset/MultiSetBackedSortedSetTest.java @@ -361,12 +361,14 @@ public void testHeadSet() { // verify order assertFalse(subSet.isEmpty()); assertArrayEquals(data[sortedOrder[start]], subSet.iterator().next()); + assertArrayEquals(data[sortedOrder[start]], subSet.first()); int index = start; for (byte[] value : subSet) { assertArrayEquals(data[sortedOrder[index++]], value); } ArrayList list = new ArrayList<>(subSet); assertArrayEquals(data[sortedOrder[end - 1]], list.get(list.size() - 1)); + assertArrayEquals(data[sortedOrder[end - 1]], subSet.last()); subSet = set.headSet(data[sortedOrder[end]]);