Commit 8b782fab authored by Mariusz Smykuła's avatar Mariusz Smykuła Committed by Greg Messner
Browse files

Spliterator improvments (#297)

* fix for NPE on empty source
* add NPE when action is missing (required by contract)
* declare characteristics
parent 0085d120
package org.gitlab4j.api;
import java.util.Collections;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;
......@@ -14,11 +15,16 @@ class PagerSpliterator<T> implements Spliterator<T> {
this.pager = pager;
if (pager.hasNext()) {
elements = this.pager.next().iterator();
} else {
elements = Collections.emptyIterator();
}
}
@Override
public boolean tryAdvance(Consumer<? super T> action) {
if (action == null) {
throw new NullPointerException("Action is null");
}
if (elements.hasNext()) {
action.accept(elements.next());
return true;
......@@ -42,6 +48,6 @@ class PagerSpliterator<T> implements Spliterator<T> {
@Override
public int characteristics() {
return 0;
return SIZED | NONNULL;
}
}
package org.gitlab4j.api;
import java.util.Collections;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class PagerSpliteratorTest {
PagerSpliterator<Integer> pagerSpliterator;
@Mock
Pager pager;
@Before
public void setUp() {
pagerSpliterator = new PagerSpliterator<>(pager);
}
@Test
public void shouldAcceptElementFromSource() {
when(pager.hasNext()).thenReturn(true);
when(pager.next()).thenReturn(Collections.singletonList(1));
boolean success = pagerSpliterator.tryAdvance(System.out::println);
assertTrue(success);
}
@Test
public void shouldDoNotFailOnEmptySource() {
boolean success = pagerSpliterator.tryAdvance(System.out::println);
assertFalse(success);
}
@Test
public void shouldThrowNullPointerExceptionWhenActionIsMissing() {
try {
pagerSpliterator.tryAdvance(null);
Assert.fail("Missing NullPointerException");
} catch (Throwable e) {
assertEquals(NullPointerException.class, e.getClass());
}
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment