@Override
    public boolean hasNext() {
        if (this.next == null) {
            if (this.inView != null) {
                if (this.currentElementNum < this.elementCount) {
                    try {
                        this.instance = this.serializer.deserialize(this.instance, this.inView);
                    } catch (IOException e) {
                        throw new RuntimeException(
                                "SpillingIterator: Error reading element from buffer.", e);
                    }
                    this.next = this.instance;
                    this.currentElementNum++;
                    return true;
                } else {
                    return false;
                }
            } else {
                // writing pass (first)
                if (this.input.hasNext()) {
                    this.next = this.input.next();
                    try {
                        this.serializer.serialize(this.next, this.buffer);
                    } catch (IOException e) {
                        throw new RuntimeException(
                                "SpillingIterator: Error writing element to buffer.", e);
                    }
                    this.elementCount++;
                    return true;
                } else {
                    return false;
                }
            }
        } else {
            return true;
        }
    }
@Test
    void testHasNext() {
        try {
            // create the resettable Iterator
            SpillingResettableIterator<IntValue> iterator =
                    new SpillingResettableIterator<IntValue>(
                            this.reader,
                            this.serializer,
                            this.memman,
                            this.ioman,
                            2,
                            this.memOwner);
            // open the iterator
            iterator.open();

            int cnt = 0;
            while (iterator.hasNext()) {
                iterator.hasNext();
                iterator.next();
                cnt++;
            }

            assertThat(cnt)
                    .withFailMessage(
                            "%d elements read from iterator, but %d expected", cnt, NUM_TESTRECORDS)
                    .isEqualTo(NUM_TESTRECORDS);

            iterator.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("Test encountered an exception.");
        }
    }