Skip to content

Commit

Permalink
support ListIterator construct from list
Browse files Browse the repository at this point in the history
Change-Id: I3ed1e3431f3d9b4ba6f66479fd557808c4c28ddc
  • Loading branch information
javeme committed Dec 31, 2019
1 parent ab2d221 commit 684d841
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 78 deletions.
52 changes: 0 additions & 52 deletions src/main/java/com/baidu/hugegraph/iterator/IterableIterator.java

This file was deleted.

24 changes: 17 additions & 7 deletions src/main/java/com/baidu/hugegraph/iterator/ListIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.baidu.hugegraph.iterator;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand All @@ -29,18 +30,27 @@ public class ListIterator<T> extends WrappedIterator<T> {

private final Iterator<T> originIterator;
private final Iterator<T> resultsIterator;
private final List<T> results;
private final Collection<T> results;

public ListIterator(long capacity, Iterator<T> origin) {
this.originIterator = origin;
this.results = InsertionOrderUtil.newList();
List<T> results = InsertionOrderUtil.newList();
while (origin.hasNext()) {
if (capacity >= 0L && this.results.size() >= capacity) {
if (capacity >= 0L && results.size() >= capacity) {
throw new IllegalArgumentException(
"The iterator exceeded capacity " + capacity);
}
this.results.add(origin.next());
results.add(origin.next());
}
this.originIterator = origin;
this.results = Collections.unmodifiableList(results);
this.resultsIterator = this.results.iterator();
}

public ListIterator(Collection<T> origin) {
this.originIterator = origin.iterator();
this.results = origin instanceof List ?
Collections.unmodifiableList((List<T>) origin) :
Collections.unmodifiableCollection(origin);
this.resultsIterator = this.results.iterator();
}

Expand All @@ -49,8 +59,8 @@ public void remove() {
this.resultsIterator.remove();
}

public List<T> list() {
return Collections.unmodifiableList(this.results);
public Collection<T> list() {
return this.results;
}

@Override
Expand Down
108 changes: 89 additions & 19 deletions src/test/java/com/baidu/hugegraph/unit/iterator/ListIteratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,40 +141,36 @@ public void testHasNextAndNext() {

@Test
public void testRemove() {
List<Integer> list3 = new ArrayList<>(DATA3);
Iterator<Integer> results = new ListIterator<>(-1, list3.iterator());
List<Integer> list = new ArrayList<>(DATA3);
ListIterator<Integer> results = new ListIterator<>(-1, list.iterator());

Assert.assertEquals(ImmutableList.of(4, 5, 6), list3);
Assert.assertEquals(ImmutableList.of(4, 5, 6), list);
Assert.assertEquals(ImmutableList.of(4, 5, 6), results.list());

Assert.assertEquals(4, (int) results.next());
Assert.assertEquals(5, (int) results.next());
results.remove();
Assert.assertEquals(6, (int) results.next());
Assert.assertThrows(UnsupportedOperationException.class, () -> {
results.remove();
});
results.next();
Assert.assertThrows(UnsupportedOperationException.class, () -> {
results.remove();
});

Assert.assertEquals(ImmutableList.of(4, 5, 6), list3);
Assert.assertEquals(ImmutableList.of(4, 5, 6), list);
Assert.assertEquals(ImmutableList.of(4, 5, 6), results.list());
}

@Test
public void testRemoveWithoutResult() {
Iterator<Integer> results = new ListIterator<>(-1, EMPTY);
Assert.assertThrows(IllegalStateException.class, () -> {
Assert.assertThrows(UnsupportedOperationException.class, () -> {
results.remove();
});

List<Integer> list0 = new ArrayList<>();
Iterator<Integer> results2 = new ListIterator<>(-1, list0.iterator());
Assert.assertThrows(IllegalStateException.class, () -> {
Assert.assertThrows(UnsupportedOperationException.class, () -> {
results2.remove();
});

List<Integer> list1 = new ArrayList<>(DATA1);
Iterator<Integer> results3 = new ListIterator<>(-1, list1.iterator());
results3.next();
Assert.assertThrows(NoSuchElementException.class, () -> {
results3.next();
});
results3.remove(); // OK
Assert.assertEquals(ImmutableList.of(1), list1);
}

@Test
Expand All @@ -189,4 +185,78 @@ public void testClose() throws Exception {

Assert.assertTrue(c1.closed());
}

@Test
public void testListWithConstructFromList() {
ListIterator<Integer> results;

results = new ListIterator<>(ImmutableList.of());
Assert.assertEquals(ImmutableList.of(), results.list());

results = new ListIterator<>(DATA1);
Assert.assertEquals(ImmutableList.of(1), results.list());

results = new ListIterator<>(DATA2);
Assert.assertEquals(ImmutableList.of(2, 3), results.list());

results = new ListIterator<>(DATA3);
Assert.assertEquals(ImmutableList.of(4, 5, 6), results.list());
}

@Test
public void testHasNextAndNextWithConstructFromList() {
ListIterator<Integer> results0 = new ListIterator<>(ImmutableList.of());
Assert.assertFalse(results0.hasNext());
Assert.assertThrows(NoSuchElementException.class, () -> {
results0.next();
});

ListIterator<Integer> results1 = new ListIterator<>(DATA1);
Assert.assertTrue(results1.hasNext());
Assert.assertEquals(1, results1.next());
Assert.assertFalse(results1.hasNext());
Assert.assertThrows(NoSuchElementException.class, () -> {
results1.next();
});

ListIterator<Integer> results3 = new ListIterator<>(DATA3);
Assert.assertTrue(results3.hasNext());
Assert.assertEquals(4, results3.next());
Assert.assertTrue(results3.hasNext());
Assert.assertEquals(5, results3.next());
Assert.assertTrue(results3.hasNext());
Assert.assertEquals(6, results3.next());
Assert.assertFalse(results3.hasNext());
Assert.assertThrows(NoSuchElementException.class, () -> {
results3.next();
});
}

@Test
public void testNextWithConstructFromList() {
ListIterator<Integer> results0 = new ListIterator<>(ImmutableList.of());
Assert.assertThrows(NoSuchElementException.class, () -> {
results0.next();
});

ListIterator<Integer> results3 = new ListIterator<>(DATA3);
Assert.assertEquals(4, results3.next());
Assert.assertEquals(5, results3.next());
Assert.assertEquals(6, results3.next());
Assert.assertThrows(NoSuchElementException.class, () -> {
results3.next();
});
}

@Test
public void testNextAfterListWithConstructFromList() {
ListIterator<Integer> results3 = new ListIterator<>(DATA3);
Assert.assertEquals(ImmutableList.of(4, 5, 6), results3.list());
Assert.assertEquals(4, results3.next());
Assert.assertEquals(5, results3.next());
Assert.assertEquals(6, results3.next());
Assert.assertThrows(NoSuchElementException.class, () -> {
results3.next();
});
}
}

0 comments on commit 684d841

Please sign in to comment.