From 0cf63cf25878ce1d616a95aa848c7fabd04b1dc5 Mon Sep 17 00:00:00 2001 From: Zhangmei Li Date: Mon, 30 Dec 2019 13:24:30 +0800 Subject: [PATCH] support ListIterator construct from list Change-Id: I3ed1e3431f3d9b4ba6f66479fd557808c4c28ddc --- .../hugegraph/iterator/IterableIterator.java | 52 --------- .../hugegraph/iterator/ListIterator.java | 24 ++-- .../unit/iterator/ListIteratorTest.java | 108 +++++++++++++++--- 3 files changed, 106 insertions(+), 78 deletions(-) delete mode 100644 src/main/java/com/baidu/hugegraph/iterator/IterableIterator.java diff --git a/src/main/java/com/baidu/hugegraph/iterator/IterableIterator.java b/src/main/java/com/baidu/hugegraph/iterator/IterableIterator.java deleted file mode 100644 index eb2a96637..000000000 --- a/src/main/java/com/baidu/hugegraph/iterator/IterableIterator.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2017 HugeGraph Authors - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with this - * work for additional information regarding copyright ownership. The ASF - * licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ - -package com.baidu.hugegraph.iterator; - -import java.util.Iterator; - -public class IterableIterator implements Iterator { - - private final Iterable iterable; - private final Iterator iterator; - - public IterableIterator(Iterable iterable) { - this.iterable = iterable; - this.iterator = iterable.iterator(); - } - - @Override - public boolean hasNext() { - return this.iterator.hasNext(); - } - - @Override - public T next() { - return this.iterator.next(); - } - - @Override - public void remove() { - this.iterator.remove(); - } - - public Iterable iterable() { - return iterable; - } -} diff --git a/src/main/java/com/baidu/hugegraph/iterator/ListIterator.java b/src/main/java/com/baidu/hugegraph/iterator/ListIterator.java index a46f73857..f21a43646 100644 --- a/src/main/java/com/baidu/hugegraph/iterator/ListIterator.java +++ b/src/main/java/com/baidu/hugegraph/iterator/ListIterator.java @@ -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; @@ -29,18 +30,27 @@ public class ListIterator extends WrappedIterator { private final Iterator originIterator; private final Iterator resultsIterator; - private final List results; + private final Collection results; public ListIterator(long capacity, Iterator origin) { - this.originIterator = origin; - this.results = InsertionOrderUtil.newList(); + List 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 origin) { + this.originIterator = origin.iterator(); + this.results = origin instanceof List ? + Collections.unmodifiableList((List) origin) : + Collections.unmodifiableCollection(origin); this.resultsIterator = this.results.iterator(); } @@ -49,8 +59,8 @@ public void remove() { this.resultsIterator.remove(); } - public List list() { - return Collections.unmodifiableList(this.results); + public Collection list() { + return this.results; } @Override diff --git a/src/test/java/com/baidu/hugegraph/unit/iterator/ListIteratorTest.java b/src/test/java/com/baidu/hugegraph/unit/iterator/ListIteratorTest.java index 8fff02ff5..8ddd995ba 100644 --- a/src/test/java/com/baidu/hugegraph/unit/iterator/ListIteratorTest.java +++ b/src/test/java/com/baidu/hugegraph/unit/iterator/ListIteratorTest.java @@ -141,40 +141,36 @@ public void testHasNextAndNext() { @Test public void testRemove() { - List list3 = new ArrayList<>(DATA3); - Iterator results = new ListIterator<>(-1, list3.iterator()); + List list = new ArrayList<>(DATA3); + ListIterator 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 results = new ListIterator<>(-1, EMPTY); - Assert.assertThrows(IllegalStateException.class, () -> { + Assert.assertThrows(UnsupportedOperationException.class, () -> { results.remove(); }); List list0 = new ArrayList<>(); Iterator results2 = new ListIterator<>(-1, list0.iterator()); - Assert.assertThrows(IllegalStateException.class, () -> { + Assert.assertThrows(UnsupportedOperationException.class, () -> { results2.remove(); }); - - List list1 = new ArrayList<>(DATA1); - Iterator results3 = new ListIterator<>(-1, list1.iterator()); - results3.next(); - Assert.assertThrows(NoSuchElementException.class, () -> { - results3.next(); - }); - results3.remove(); // OK - Assert.assertEquals(ImmutableList.of(1), list1); } @Test @@ -189,4 +185,78 @@ public void testClose() throws Exception { Assert.assertTrue(c1.closed()); } + + @Test + public void testListWithConstructFromList() { + ListIterator 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 results0 = new ListIterator<>(ImmutableList.of()); + Assert.assertFalse(results0.hasNext()); + Assert.assertThrows(NoSuchElementException.class, () -> { + results0.next(); + }); + + ListIterator results1 = new ListIterator<>(DATA1); + Assert.assertTrue(results1.hasNext()); + Assert.assertEquals(1, results1.next()); + Assert.assertFalse(results1.hasNext()); + Assert.assertThrows(NoSuchElementException.class, () -> { + results1.next(); + }); + + ListIterator 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 results0 = new ListIterator<>(ImmutableList.of()); + Assert.assertThrows(NoSuchElementException.class, () -> { + results0.next(); + }); + + ListIterator 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 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(); + }); + } }