From 7141a2e5594eec3008401da37fd75e4646931132 Mon Sep 17 00:00:00 2001 From: Dennis Goermann Date: Tue, 21 May 2019 19:56:13 +0200 Subject: [PATCH 1/6] COLLECTIONS-699 [NEW] An iterator for returning pairs of the childs iterators, askes in COLLECTIONS-699 --- pom.xml | 2 +- .../iterators/PairingIterator.java | 106 +++++++++ .../iterators/PairingIteratorTest.java | 212 ++++++++++++++++++ 3 files changed, 319 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java create mode 100644 src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java diff --git a/pom.xml b/pom.xml index efc41a33c2..bae2284893 100644 --- a/pom.xml +++ b/pom.xml @@ -457,7 +457,7 @@ org.apache.commons commons-lang3 3.9 - test + provided diff --git a/src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java b/src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java new file mode 100644 index 0000000000..c8d574f53b --- /dev/null +++ b/src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java @@ -0,0 +1,106 @@ +/* + * 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 org.apache.commons.collections4.iterators; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +import org.apache.commons.lang3.tuple.Pair; + +/** + * Provides an iteration over the elements of two iterators. It will be return a + * {@link Pair} with the elements of the iterators. + *

+ * Given two {@link Iterator} instances {@code A} and {@code B}. The + * {@link #next()} method will return a {@link Pair} with the elements of the + * {@code A} and {@code B} until both iterators are exhausted. + *

+ *

+ * If one of the iterators is null, the result {@link Pair} has also a null + * value and the value of the other iterator. + *

+ *

+ * If one iterator has more elements then the other, the result {@link Pair} + * will contain null values if one of the iterator exhausted. + * + * @param + * type of left value of {@link Pair} + * + * @param + * type of the right value of {@link Pair} + * + * @since 4.4 + */ +public class PairingIterator implements Iterator> { + + private final Iterator firstIterator; + private final Iterator secondIterator; + + /** + * Constructs a new PairingIterator that will provide a + * {@link} Pair of the child iterator elements. + * + * @param firstIterator + * the first iterator + * @param secondIterator + * the second iterator + */ + public PairingIterator(Iterator firstIterator, Iterator secondIterator) { + this.firstIterator = firstIterator; + this.secondIterator = secondIterator; + } + + /** + * Returns {@code true} if one of the child iterators has remaining elements. + */ + @Override + public boolean hasNext() { + return (null != firstIterator && firstIterator.hasNext()) + || (null != secondIterator && secondIterator.hasNext()); + } + + /** + * Returns the next {@link Pair} with the next values of the child iterators. + * + * @return a {@link Pair} with the next values of child iterators + * @throws NoSuchElementException + * if no child iterator has any more elements + */ + @Override + public Pair next() throws NoSuchElementException { + if (!hasNext()) { + throw new NoSuchElementException(); + } + final L leftValue = null != firstIterator && firstIterator.hasNext() ? firstIterator.next() : null; + final R rightValue = null != secondIterator && secondIterator.hasNext() ? secondIterator.next() : null; + return Pair.of(leftValue, rightValue); + } + + /** + * Removes the last returned values of the child iterators. + */ + @Override + public void remove() { + if (firstIterator != null) { + firstIterator.remove(); + } + if (secondIterator != null) { + secondIterator.remove(); + } + } + +} diff --git a/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java new file mode 100644 index 0000000000..7b3ed6fd85 --- /dev/null +++ b/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java @@ -0,0 +1,212 @@ +/* + * 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 org.apache.commons.collections4.iterators; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; + +import org.apache.commons.collections4.IteratorUtils; +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Assert; +import org.junit.Test; + +/** + * Unit test suite for {@link PairingIterator}. + * + */ +public class PairingIteratorTest extends AbstractIteratorTest> { + + public PairingIteratorTest(String testName) { + super(testName); + } + + @Test + public void testNullValuesBoth() { + final PairingIterator pairingIterator = new PairingIterator<>(null, null); + + Assert.assertFalse(pairingIterator.hasNext()); + try { + pairingIterator.next(); + fail(); + + } catch (NoSuchElementException e) { + Assert.assertNotNull(e); + } + } + + @Test + public void testNullValueFirst() { + final List rightList = Arrays.asList(new String[] { "A" }); + final PairingIterator pairingIterator = new PairingIterator<>(null, rightList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Pair next = pairingIterator.next(); + Assert.assertEquals(null, next.getLeft()); + Assert.assertEquals("A", next.getRight()); + } + + @Test + public void testNullValueSecond() { + final List leftList = Arrays.asList(new String[] { "A" }); + final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), null); + + Assert.assertTrue(pairingIterator.hasNext()); + + Pair next = pairingIterator.next(); + Assert.assertEquals("A", next.getLeft()); + Assert.assertEquals(null, next.getRight()); + } + + @Test + public void testTwoIteratorsWithBothTwoElements() { + final List leftList = Arrays.asList(new String[] { "A1", "A2" }); + final List rightList = Arrays.asList(new String[] { "B1", "B2" }); + final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), + rightList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Pair next = pairingIterator.next(); + Assert.assertEquals("A1", next.getLeft()); + Assert.assertEquals("B1", next.getRight()); + + Assert.assertTrue(pairingIterator.hasNext()); + next = pairingIterator.next(); + Assert.assertEquals("A2", next.getLeft()); + Assert.assertEquals("B2", next.getRight()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testTwoIteratorsWithDifferentSize() { + final List leftList = Arrays.asList(new String[] { "A1", "A2", "A3" }); + final List rightList = Arrays.asList(new String[] { "B1", "B2" }); + final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), + rightList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Pair next = pairingIterator.next(); + Assert.assertEquals("A1", next.getLeft()); + Assert.assertEquals("B1", next.getRight()); + + Assert.assertTrue(pairingIterator.hasNext()); + + next = pairingIterator.next(); + Assert.assertEquals("A2", next.getLeft()); + Assert.assertEquals("B2", next.getRight()); + + Assert.assertTrue(pairingIterator.hasNext()); + next = pairingIterator.next(); + Assert.assertEquals("A3", next.getLeft()); + Assert.assertEquals(null, next.getRight()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testTwoIteratorsWithDifferentSize2() { + final List leftList = Arrays.asList(new String[] { "A1", }); + final List rightList = Arrays.asList(new String[] { "B1", "B2" }); + final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), + rightList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Pair next = pairingIterator.next(); + Assert.assertEquals("A1", next.getLeft()); + Assert.assertEquals("B1", next.getRight()); + + Assert.assertTrue(pairingIterator.hasNext()); + + next = pairingIterator.next(); + Assert.assertEquals(null, next.getLeft()); + Assert.assertEquals("B2", next.getRight()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testTwoIteratorsWithNullValues() { + final List leftList = Arrays.asList(new String[] { null, "A2" }); + final List rightList = Arrays.asList(new String[] { "B1", null }); + final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), + rightList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Pair next = pairingIterator.next(); + Assert.assertEquals(null, next.getLeft()); + Assert.assertEquals("B1", next.getRight()); + + Assert.assertTrue(pairingIterator.hasNext()); + next = pairingIterator.next(); + Assert.assertEquals("A2", next.getLeft()); + Assert.assertEquals(null, next.getRight()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testRemoveWithOneNullIterator() { + final List leftList = new ArrayList<>(); + leftList.add("A1"); + final Iterator leftIterator = leftList.iterator(); + final PairingIterator pairingIterator = new PairingIterator<>(leftIterator, null); + + pairingIterator.next(); + pairingIterator.remove(); + Assert.assertTrue(leftList.isEmpty()); + } + + @Test + public void testRemoveWithOneNullIterator2() { + final List rightList = new ArrayList<>(); + rightList.add("A1"); + final Iterator rightIterator = rightList.iterator(); + final PairingIterator pairingIterator = new PairingIterator<>(null, rightIterator); + + pairingIterator.next(); + pairingIterator.remove(); + Assert.assertTrue(rightList.isEmpty()); + } + + @Override + public Iterator> makeEmptyIterator() { + return new PairingIterator(IteratorUtils.emptyIterator(), + IteratorUtils.emptyIterator()); + } + + @Override + public Iterator> makeObject() { + final List leftList = new ArrayList<>(); + leftList.add("A1"); + leftList.add("A2"); + leftList.add("A3"); + final List rightList = new ArrayList<>(); + rightList.add("B1"); + rightList.add("B2"); + return new PairingIterator<>(leftList.iterator(), rightList.iterator()); + } + +} From 39bd7727702bb43f786d2d7515bec7f84fd51114 Mon Sep 17 00:00:00 2001 From: Dennis Goermann Date: Tue, 21 May 2019 20:00:00 +0200 Subject: [PATCH 2/6] COLLECTIONS-699 [FIXED] replacing tabs with whitespaces --- .../iterators/PairingIterator.java | 102 +++--- .../iterators/PairingIteratorTest.java | 338 +++++++++--------- 2 files changed, 220 insertions(+), 220 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java b/src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java index c8d574f53b..2ad79b51e3 100644 --- a/src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java +++ b/src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java @@ -47,60 +47,60 @@ */ public class PairingIterator implements Iterator> { - private final Iterator firstIterator; - private final Iterator secondIterator; + private final Iterator firstIterator; + private final Iterator secondIterator; - /** - * Constructs a new PairingIterator that will provide a - * {@link} Pair of the child iterator elements. - * - * @param firstIterator - * the first iterator - * @param secondIterator - * the second iterator - */ - public PairingIterator(Iterator firstIterator, Iterator secondIterator) { - this.firstIterator = firstIterator; - this.secondIterator = secondIterator; - } + /** + * Constructs a new PairingIterator that will provide a + * {@link} Pair of the child iterator elements. + * + * @param firstIterator + * the first iterator + * @param secondIterator + * the second iterator + */ + public PairingIterator(Iterator firstIterator, Iterator secondIterator) { + this.firstIterator = firstIterator; + this.secondIterator = secondIterator; + } - /** - * Returns {@code true} if one of the child iterators has remaining elements. - */ - @Override - public boolean hasNext() { - return (null != firstIterator && firstIterator.hasNext()) - || (null != secondIterator && secondIterator.hasNext()); - } + /** + * Returns {@code true} if one of the child iterators has remaining elements. + */ + @Override + public boolean hasNext() { + return (null != firstIterator && firstIterator.hasNext()) + || (null != secondIterator && secondIterator.hasNext()); + } - /** - * Returns the next {@link Pair} with the next values of the child iterators. - * - * @return a {@link Pair} with the next values of child iterators - * @throws NoSuchElementException - * if no child iterator has any more elements - */ - @Override - public Pair next() throws NoSuchElementException { - if (!hasNext()) { - throw new NoSuchElementException(); - } - final L leftValue = null != firstIterator && firstIterator.hasNext() ? firstIterator.next() : null; - final R rightValue = null != secondIterator && secondIterator.hasNext() ? secondIterator.next() : null; - return Pair.of(leftValue, rightValue); - } + /** + * Returns the next {@link Pair} with the next values of the child iterators. + * + * @return a {@link Pair} with the next values of child iterators + * @throws NoSuchElementException + * if no child iterator has any more elements + */ + @Override + public Pair next() throws NoSuchElementException { + if (!hasNext()) { + throw new NoSuchElementException(); + } + final L leftValue = null != firstIterator && firstIterator.hasNext() ? firstIterator.next() : null; + final R rightValue = null != secondIterator && secondIterator.hasNext() ? secondIterator.next() : null; + return Pair.of(leftValue, rightValue); + } - /** - * Removes the last returned values of the child iterators. - */ - @Override - public void remove() { - if (firstIterator != null) { - firstIterator.remove(); - } - if (secondIterator != null) { - secondIterator.remove(); - } - } + /** + * Removes the last returned values of the child iterators. + */ + @Override + public void remove() { + if (firstIterator != null) { + firstIterator.remove(); + } + if (secondIterator != null) { + secondIterator.remove(); + } + } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java index 7b3ed6fd85..7f1788c224 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java @@ -34,179 +34,179 @@ */ public class PairingIteratorTest extends AbstractIteratorTest> { - public PairingIteratorTest(String testName) { - super(testName); - } - - @Test - public void testNullValuesBoth() { - final PairingIterator pairingIterator = new PairingIterator<>(null, null); - - Assert.assertFalse(pairingIterator.hasNext()); - try { - pairingIterator.next(); - fail(); + public PairingIteratorTest(String testName) { + super(testName); + } + + @Test + public void testNullValuesBoth() { + final PairingIterator pairingIterator = new PairingIterator<>(null, null); + + Assert.assertFalse(pairingIterator.hasNext()); + try { + pairingIterator.next(); + fail(); - } catch (NoSuchElementException e) { - Assert.assertNotNull(e); - } - } - - @Test - public void testNullValueFirst() { - final List rightList = Arrays.asList(new String[] { "A" }); - final PairingIterator pairingIterator = new PairingIterator<>(null, rightList.iterator()); + } catch (NoSuchElementException e) { + Assert.assertNotNull(e); + } + } + + @Test + public void testNullValueFirst() { + final List rightList = Arrays.asList(new String[] { "A" }); + final PairingIterator pairingIterator = new PairingIterator<>(null, rightList.iterator()); - Assert.assertTrue(pairingIterator.hasNext()); + Assert.assertTrue(pairingIterator.hasNext()); - Pair next = pairingIterator.next(); - Assert.assertEquals(null, next.getLeft()); - Assert.assertEquals("A", next.getRight()); - } + Pair next = pairingIterator.next(); + Assert.assertEquals(null, next.getLeft()); + Assert.assertEquals("A", next.getRight()); + } - @Test - public void testNullValueSecond() { - final List leftList = Arrays.asList(new String[] { "A" }); - final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), null); + @Test + public void testNullValueSecond() { + final List leftList = Arrays.asList(new String[] { "A" }); + final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), null); - Assert.assertTrue(pairingIterator.hasNext()); - - Pair next = pairingIterator.next(); - Assert.assertEquals("A", next.getLeft()); - Assert.assertEquals(null, next.getRight()); - } - - @Test - public void testTwoIteratorsWithBothTwoElements() { - final List leftList = Arrays.asList(new String[] { "A1", "A2" }); - final List rightList = Arrays.asList(new String[] { "B1", "B2" }); - final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), - rightList.iterator()); - - Assert.assertTrue(pairingIterator.hasNext()); - - Pair next = pairingIterator.next(); - Assert.assertEquals("A1", next.getLeft()); - Assert.assertEquals("B1", next.getRight()); - - Assert.assertTrue(pairingIterator.hasNext()); - next = pairingIterator.next(); - Assert.assertEquals("A2", next.getLeft()); - Assert.assertEquals("B2", next.getRight()); - - Assert.assertFalse(pairingIterator.hasNext()); - } - - @Test - public void testTwoIteratorsWithDifferentSize() { - final List leftList = Arrays.asList(new String[] { "A1", "A2", "A3" }); - final List rightList = Arrays.asList(new String[] { "B1", "B2" }); - final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), - rightList.iterator()); - - Assert.assertTrue(pairingIterator.hasNext()); - - Pair next = pairingIterator.next(); - Assert.assertEquals("A1", next.getLeft()); - Assert.assertEquals("B1", next.getRight()); - - Assert.assertTrue(pairingIterator.hasNext()); - - next = pairingIterator.next(); - Assert.assertEquals("A2", next.getLeft()); - Assert.assertEquals("B2", next.getRight()); - - Assert.assertTrue(pairingIterator.hasNext()); - next = pairingIterator.next(); - Assert.assertEquals("A3", next.getLeft()); - Assert.assertEquals(null, next.getRight()); - - Assert.assertFalse(pairingIterator.hasNext()); - } - - @Test - public void testTwoIteratorsWithDifferentSize2() { - final List leftList = Arrays.asList(new String[] { "A1", }); - final List rightList = Arrays.asList(new String[] { "B1", "B2" }); - final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), - rightList.iterator()); - - Assert.assertTrue(pairingIterator.hasNext()); - - Pair next = pairingIterator.next(); - Assert.assertEquals("A1", next.getLeft()); - Assert.assertEquals("B1", next.getRight()); - - Assert.assertTrue(pairingIterator.hasNext()); - - next = pairingIterator.next(); - Assert.assertEquals(null, next.getLeft()); - Assert.assertEquals("B2", next.getRight()); - - Assert.assertFalse(pairingIterator.hasNext()); - } - - @Test - public void testTwoIteratorsWithNullValues() { - final List leftList = Arrays.asList(new String[] { null, "A2" }); - final List rightList = Arrays.asList(new String[] { "B1", null }); - final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), - rightList.iterator()); - - Assert.assertTrue(pairingIterator.hasNext()); - - Pair next = pairingIterator.next(); - Assert.assertEquals(null, next.getLeft()); - Assert.assertEquals("B1", next.getRight()); - - Assert.assertTrue(pairingIterator.hasNext()); - next = pairingIterator.next(); - Assert.assertEquals("A2", next.getLeft()); - Assert.assertEquals(null, next.getRight()); - - Assert.assertFalse(pairingIterator.hasNext()); - } - - @Test - public void testRemoveWithOneNullIterator() { - final List leftList = new ArrayList<>(); - leftList.add("A1"); - final Iterator leftIterator = leftList.iterator(); - final PairingIterator pairingIterator = new PairingIterator<>(leftIterator, null); - - pairingIterator.next(); - pairingIterator.remove(); - Assert.assertTrue(leftList.isEmpty()); - } - - @Test - public void testRemoveWithOneNullIterator2() { - final List rightList = new ArrayList<>(); - rightList.add("A1"); - final Iterator rightIterator = rightList.iterator(); - final PairingIterator pairingIterator = new PairingIterator<>(null, rightIterator); - - pairingIterator.next(); - pairingIterator.remove(); - Assert.assertTrue(rightList.isEmpty()); - } - - @Override - public Iterator> makeEmptyIterator() { - return new PairingIterator(IteratorUtils.emptyIterator(), - IteratorUtils.emptyIterator()); - } - - @Override - public Iterator> makeObject() { - final List leftList = new ArrayList<>(); - leftList.add("A1"); - leftList.add("A2"); - leftList.add("A3"); - final List rightList = new ArrayList<>(); - rightList.add("B1"); - rightList.add("B2"); - return new PairingIterator<>(leftList.iterator(), rightList.iterator()); - } + Assert.assertTrue(pairingIterator.hasNext()); + + Pair next = pairingIterator.next(); + Assert.assertEquals("A", next.getLeft()); + Assert.assertEquals(null, next.getRight()); + } + + @Test + public void testTwoIteratorsWithBothTwoElements() { + final List leftList = Arrays.asList(new String[] { "A1", "A2" }); + final List rightList = Arrays.asList(new String[] { "B1", "B2" }); + final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), + rightList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Pair next = pairingIterator.next(); + Assert.assertEquals("A1", next.getLeft()); + Assert.assertEquals("B1", next.getRight()); + + Assert.assertTrue(pairingIterator.hasNext()); + next = pairingIterator.next(); + Assert.assertEquals("A2", next.getLeft()); + Assert.assertEquals("B2", next.getRight()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testTwoIteratorsWithDifferentSize() { + final List leftList = Arrays.asList(new String[] { "A1", "A2", "A3" }); + final List rightList = Arrays.asList(new String[] { "B1", "B2" }); + final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), + rightList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Pair next = pairingIterator.next(); + Assert.assertEquals("A1", next.getLeft()); + Assert.assertEquals("B1", next.getRight()); + + Assert.assertTrue(pairingIterator.hasNext()); + + next = pairingIterator.next(); + Assert.assertEquals("A2", next.getLeft()); + Assert.assertEquals("B2", next.getRight()); + + Assert.assertTrue(pairingIterator.hasNext()); + next = pairingIterator.next(); + Assert.assertEquals("A3", next.getLeft()); + Assert.assertEquals(null, next.getRight()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testTwoIteratorsWithDifferentSize2() { + final List leftList = Arrays.asList(new String[] { "A1", }); + final List rightList = Arrays.asList(new String[] { "B1", "B2" }); + final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), + rightList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Pair next = pairingIterator.next(); + Assert.assertEquals("A1", next.getLeft()); + Assert.assertEquals("B1", next.getRight()); + + Assert.assertTrue(pairingIterator.hasNext()); + + next = pairingIterator.next(); + Assert.assertEquals(null, next.getLeft()); + Assert.assertEquals("B2", next.getRight()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testTwoIteratorsWithNullValues() { + final List leftList = Arrays.asList(new String[] { null, "A2" }); + final List rightList = Arrays.asList(new String[] { "B1", null }); + final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), + rightList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Pair next = pairingIterator.next(); + Assert.assertEquals(null, next.getLeft()); + Assert.assertEquals("B1", next.getRight()); + + Assert.assertTrue(pairingIterator.hasNext()); + next = pairingIterator.next(); + Assert.assertEquals("A2", next.getLeft()); + Assert.assertEquals(null, next.getRight()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testRemoveWithOneNullIterator() { + final List leftList = new ArrayList<>(); + leftList.add("A1"); + final Iterator leftIterator = leftList.iterator(); + final PairingIterator pairingIterator = new PairingIterator<>(leftIterator, null); + + pairingIterator.next(); + pairingIterator.remove(); + Assert.assertTrue(leftList.isEmpty()); + } + + @Test + public void testRemoveWithOneNullIterator2() { + final List rightList = new ArrayList<>(); + rightList.add("A1"); + final Iterator rightIterator = rightList.iterator(); + final PairingIterator pairingIterator = new PairingIterator<>(null, rightIterator); + + pairingIterator.next(); + pairingIterator.remove(); + Assert.assertTrue(rightList.isEmpty()); + } + + @Override + public Iterator> makeEmptyIterator() { + return new PairingIterator(IteratorUtils.emptyIterator(), + IteratorUtils.emptyIterator()); + } + + @Override + public Iterator> makeObject() { + final List leftList = new ArrayList<>(); + leftList.add("A1"); + leftList.add("A2"); + leftList.add("A3"); + final List rightList = new ArrayList<>(); + rightList.add("B1"); + rightList.add("B2"); + return new PairingIterator<>(leftList.iterator(), rightList.iterator()); + } } From b118b894362c63284e59d093a6cea3449a713b69 Mon Sep 17 00:00:00 2001 From: Dennis Goermann Date: Wed, 22 May 2019 18:39:40 +0200 Subject: [PATCH 3/6] COLLECTION-699 new implementation Instead of a common-lang Pair we use a custom lightweight dto. --- .../iterators/PairingIterator.java | 200 ++++++---- .../iterators/PairingIteratorTest.java | 365 ++++++++++-------- 2 files changed, 327 insertions(+), 238 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java b/src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java index 2ad79b51e3..eaf656f906 100644 --- a/src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java +++ b/src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java @@ -18,89 +18,149 @@ import java.util.Iterator; import java.util.NoSuchElementException; - -import org.apache.commons.lang3.tuple.Pair; +import java.util.Objects; +import org.apache.commons.collections4.iterators.PairingIterator.Entry; /** * Provides an iteration over the elements of two iterators. It will be return a - * {@link Pair} with the elements of the iterators. + * {@link Entry} containing the child iterators elements. *

* Given two {@link Iterator} instances {@code A} and {@code B}. The - * {@link #next()} method will return a {@link Pair} with the elements of the - * {@code A} and {@code B} until both iterators are exhausted. + * {@link #next()} method will return a {@link Entry} with the elements of the + * {@code A} and {@code B} as values of the {@link Entry} until both iterators + * are exhausted. *

*

- * If one of the iterators is null, the result {@link Pair} has also a null - * value and the value of the other iterator. + * If one of the iterators is null, the result {@link Entry} contains also a + * null values until both child iterators exhausted. *

*

- * If one iterator has more elements then the other, the result {@link Pair} - * will contain null values if one of the iterator exhausted. + * If one iterator has more elements then the other, the result {@link Entry} + * will contain a null value and the value of the not empty child iterator until + * both of the iterator exhausted. * - * @param - * type of left value of {@link Pair} + * @param type of first value. The first value of {@link Entry} * - * @param - * type of the right value of {@link Pair} + * @param type of second value. The second value of {@link Entry} * * @since 4.4 */ -public class PairingIterator implements Iterator> { - - private final Iterator firstIterator; - private final Iterator secondIterator; - - /** - * Constructs a new PairingIterator that will provide a - * {@link} Pair of the child iterator elements. - * - * @param firstIterator - * the first iterator - * @param secondIterator - * the second iterator - */ - public PairingIterator(Iterator firstIterator, Iterator secondIterator) { - this.firstIterator = firstIterator; - this.secondIterator = secondIterator; - } - - /** - * Returns {@code true} if one of the child iterators has remaining elements. - */ - @Override - public boolean hasNext() { - return (null != firstIterator && firstIterator.hasNext()) - || (null != secondIterator && secondIterator.hasNext()); - } - - /** - * Returns the next {@link Pair} with the next values of the child iterators. - * - * @return a {@link Pair} with the next values of child iterators - * @throws NoSuchElementException - * if no child iterator has any more elements - */ - @Override - public Pair next() throws NoSuchElementException { - if (!hasNext()) { - throw new NoSuchElementException(); - } - final L leftValue = null != firstIterator && firstIterator.hasNext() ? firstIterator.next() : null; - final R rightValue = null != secondIterator && secondIterator.hasNext() ? secondIterator.next() : null; - return Pair.of(leftValue, rightValue); - } - - /** - * Removes the last returned values of the child iterators. - */ - @Override - public void remove() { - if (firstIterator != null) { - firstIterator.remove(); - } - if (secondIterator != null) { - secondIterator.remove(); - } - } +public class PairingIterator implements Iterator> { + + private final Iterator firstIterator; + private final Iterator secondIterator; + + /** + * Constructs a new PairingIterator that will provide a + * {@link Entry} containing the child iterator elements. + * + * @param firstIterator the first iterator + * @param secondIterator the second iterator + */ + public PairingIterator(Iterator firstIterator, Iterator secondIterator) { + this.firstIterator = firstIterator; + this.secondIterator = secondIterator; + } + + /** + * Returns {@code true} if one of the child iterators has remaining elements. + */ + @Override + public boolean hasNext() { + return (null != firstIterator && firstIterator.hasNext()) + || (null != secondIterator && secondIterator.hasNext()); + } + + /** + * Returns the next {@link Entry} with the next values of the child iterators. + * + * @return a {@link Entry} with the next values of child iterators + * @throws NoSuchElementException if no child iterator has any more elements + */ + @Override + public Entry next() throws NoSuchElementException { + if (!hasNext()) { + throw new NoSuchElementException(); + } + final F firstValue = null != firstIterator && firstIterator.hasNext() ? firstIterator.next() : null; + final S secondValue = null != secondIterator && secondIterator.hasNext() ? secondIterator.next() : null; + return new Entry(firstValue, secondValue); + } + + /** + * Removes the last returned values of the child iterators. + */ + @Override + public void remove() { + if (firstIterator != null) { + firstIterator.remove(); + } + if (secondIterator != null) { + secondIterator.remove(); + } + } + + /** + * Contains two values of different generic types. + * + * @param the type of the first element + * @param the type of the second element + */ + public static class Entry { + private final F firstValue; + private final S secondValue; + + /** + * Constructs a new {@link Entry} of two generic values. + * + * @param firstValue the first value + * @param secondValue the second value + */ + Entry(F firstValue, S secondValue) { + this.firstValue = firstValue; + this.secondValue = secondValue; + } + + /** + * Returns the first value. + * + * @return the first value + */ + public F getFirstValue() { + return firstValue; + } + + /** + * Returns the second value. + * + * @return the second value + */ + public S getSecondValue() { + return secondValue; + } + + @Override + public int hashCode() { + return Objects.hash(firstValue, secondValue); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Entry other = (Entry) obj; + return Objects.equals(firstValue, other.firstValue) && Objects.equals(secondValue, other.secondValue); + } + + @Override + public String toString() { + return "Entry [firstValue=" + firstValue + ", secondValue=" + secondValue + "]"; + } + + } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java index 7f1788c224..ca9121600c 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java @@ -24,7 +24,7 @@ import java.util.NoSuchElementException; import org.apache.commons.collections4.IteratorUtils; -import org.apache.commons.lang3.tuple.Pair; +import org.apache.commons.collections4.iterators.PairingIterator.Entry; import org.junit.Assert; import org.junit.Test; @@ -32,181 +32,210 @@ * Unit test suite for {@link PairingIterator}. * */ -public class PairingIteratorTest extends AbstractIteratorTest> { +public class PairingIteratorTest extends AbstractIteratorTest> { - public PairingIteratorTest(String testName) { - super(testName); - } + public PairingIteratorTest(String testName) { + super(testName); + } - @Test - public void testNullValuesBoth() { - final PairingIterator pairingIterator = new PairingIterator<>(null, null); + @Test + public void testNullValuesBoth() { + final PairingIterator pairingIterator = new PairingIterator<>(null, null); - Assert.assertFalse(pairingIterator.hasNext()); - try { - pairingIterator.next(); - fail(); + Assert.assertFalse(pairingIterator.hasNext()); + try { + pairingIterator.next(); + fail(); - } catch (NoSuchElementException e) { - Assert.assertNotNull(e); - } - } - - @Test - public void testNullValueFirst() { - final List rightList = Arrays.asList(new String[] { "A" }); - final PairingIterator pairingIterator = new PairingIterator<>(null, rightList.iterator()); + } catch (NoSuchElementException e) { + Assert.assertNotNull(e); + } + } + + @Test + public void testNullValueFirst() { + final List firstList = Arrays.asList(new String[] { "A" }); + final PairingIterator pairingIterator = new PairingIterator<>(null, firstList.iterator()); - Assert.assertTrue(pairingIterator.hasNext()); + Assert.assertTrue(pairingIterator.hasNext()); - Pair next = pairingIterator.next(); - Assert.assertEquals(null, next.getLeft()); - Assert.assertEquals("A", next.getRight()); - } + Entry next = pairingIterator.next(); + Assert.assertEquals(null, next.getFirstValue()); + Assert.assertEquals("A", next.getSecondValue()); + } - @Test - public void testNullValueSecond() { - final List leftList = Arrays.asList(new String[] { "A" }); - final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), null); + @Test + public void testNullValueSecond() { + final List firstList = Arrays.asList(new String[] { "A" }); + final PairingIterator pairingIterator = new PairingIterator<>(firstList.iterator(), null); - Assert.assertTrue(pairingIterator.hasNext()); + Assert.assertTrue(pairingIterator.hasNext()); - Pair next = pairingIterator.next(); - Assert.assertEquals("A", next.getLeft()); - Assert.assertEquals(null, next.getRight()); - } - - @Test - public void testTwoIteratorsWithBothTwoElements() { - final List leftList = Arrays.asList(new String[] { "A1", "A2" }); - final List rightList = Arrays.asList(new String[] { "B1", "B2" }); - final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), - rightList.iterator()); - - Assert.assertTrue(pairingIterator.hasNext()); - - Pair next = pairingIterator.next(); - Assert.assertEquals("A1", next.getLeft()); - Assert.assertEquals("B1", next.getRight()); - - Assert.assertTrue(pairingIterator.hasNext()); - next = pairingIterator.next(); - Assert.assertEquals("A2", next.getLeft()); - Assert.assertEquals("B2", next.getRight()); - - Assert.assertFalse(pairingIterator.hasNext()); - } - - @Test - public void testTwoIteratorsWithDifferentSize() { - final List leftList = Arrays.asList(new String[] { "A1", "A2", "A3" }); - final List rightList = Arrays.asList(new String[] { "B1", "B2" }); - final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), - rightList.iterator()); - - Assert.assertTrue(pairingIterator.hasNext()); - - Pair next = pairingIterator.next(); - Assert.assertEquals("A1", next.getLeft()); - Assert.assertEquals("B1", next.getRight()); - - Assert.assertTrue(pairingIterator.hasNext()); - - next = pairingIterator.next(); - Assert.assertEquals("A2", next.getLeft()); - Assert.assertEquals("B2", next.getRight()); - - Assert.assertTrue(pairingIterator.hasNext()); - next = pairingIterator.next(); - Assert.assertEquals("A3", next.getLeft()); - Assert.assertEquals(null, next.getRight()); - - Assert.assertFalse(pairingIterator.hasNext()); - } - - @Test - public void testTwoIteratorsWithDifferentSize2() { - final List leftList = Arrays.asList(new String[] { "A1", }); - final List rightList = Arrays.asList(new String[] { "B1", "B2" }); - final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), - rightList.iterator()); - - Assert.assertTrue(pairingIterator.hasNext()); - - Pair next = pairingIterator.next(); - Assert.assertEquals("A1", next.getLeft()); - Assert.assertEquals("B1", next.getRight()); - - Assert.assertTrue(pairingIterator.hasNext()); - - next = pairingIterator.next(); - Assert.assertEquals(null, next.getLeft()); - Assert.assertEquals("B2", next.getRight()); - - Assert.assertFalse(pairingIterator.hasNext()); - } - - @Test - public void testTwoIteratorsWithNullValues() { - final List leftList = Arrays.asList(new String[] { null, "A2" }); - final List rightList = Arrays.asList(new String[] { "B1", null }); - final PairingIterator pairingIterator = new PairingIterator<>(leftList.iterator(), - rightList.iterator()); - - Assert.assertTrue(pairingIterator.hasNext()); - - Pair next = pairingIterator.next(); - Assert.assertEquals(null, next.getLeft()); - Assert.assertEquals("B1", next.getRight()); - - Assert.assertTrue(pairingIterator.hasNext()); - next = pairingIterator.next(); - Assert.assertEquals("A2", next.getLeft()); - Assert.assertEquals(null, next.getRight()); - - Assert.assertFalse(pairingIterator.hasNext()); - } - - @Test - public void testRemoveWithOneNullIterator() { - final List leftList = new ArrayList<>(); - leftList.add("A1"); - final Iterator leftIterator = leftList.iterator(); - final PairingIterator pairingIterator = new PairingIterator<>(leftIterator, null); - - pairingIterator.next(); - pairingIterator.remove(); - Assert.assertTrue(leftList.isEmpty()); - } - - @Test - public void testRemoveWithOneNullIterator2() { - final List rightList = new ArrayList<>(); - rightList.add("A1"); - final Iterator rightIterator = rightList.iterator(); - final PairingIterator pairingIterator = new PairingIterator<>(null, rightIterator); - - pairingIterator.next(); - pairingIterator.remove(); - Assert.assertTrue(rightList.isEmpty()); - } - - @Override - public Iterator> makeEmptyIterator() { - return new PairingIterator(IteratorUtils.emptyIterator(), - IteratorUtils.emptyIterator()); - } - - @Override - public Iterator> makeObject() { - final List leftList = new ArrayList<>(); - leftList.add("A1"); - leftList.add("A2"); - leftList.add("A3"); - final List rightList = new ArrayList<>(); - rightList.add("B1"); - rightList.add("B2"); - return new PairingIterator<>(leftList.iterator(), rightList.iterator()); - } + Entry next = pairingIterator.next(); + Assert.assertEquals("A", next.getFirstValue()); + Assert.assertEquals(null, next.getSecondValue()); + } + + @Test + public void testTwoIteratorsWithBothTwoElements() { + final List firstList = Arrays.asList(new String[] { "A1", "A2" }); + final List secondList = Arrays.asList(new String[] { "B1", "B2" }); + final PairingIterator pairingIterator = new PairingIterator<>(firstList.iterator(), + secondList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Entry next = pairingIterator.next(); + Assert.assertEquals("A1", next.getFirstValue()); + Assert.assertEquals("B1", next.getSecondValue()); + + Assert.assertTrue(pairingIterator.hasNext()); + next = pairingIterator.next(); + Assert.assertEquals("A2", next.getFirstValue()); + Assert.assertEquals("B2", next.getSecondValue()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testTwoIteratorsWithDifferentSize() { + final List firstList = Arrays.asList(new String[] { "A1", "A2", "A3" }); + final List secondList = Arrays.asList(new String[] { "B1", "B2" }); + final PairingIterator pairingIterator = new PairingIterator<>(firstList.iterator(), + secondList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Entry next = pairingIterator.next(); + Assert.assertEquals("A1", next.getFirstValue()); + Assert.assertEquals("B1", next.getSecondValue()); + + Assert.assertTrue(pairingIterator.hasNext()); + + next = pairingIterator.next(); + Assert.assertEquals("A2", next.getFirstValue()); + Assert.assertEquals("B2", next.getSecondValue()); + + Assert.assertTrue(pairingIterator.hasNext()); + next = pairingIterator.next(); + Assert.assertEquals("A3", next.getFirstValue()); + Assert.assertEquals(null, next.getSecondValue()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testTwoIteratorsWithDifferentSize2() { + final List firstList = Arrays.asList(new String[] { "A1", }); + final List secondList = Arrays.asList(new String[] { "B1", "B2" }); + final PairingIterator pairingIterator = new PairingIterator<>(firstList.iterator(), + secondList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Entry next = pairingIterator.next(); + Assert.assertEquals("A1", next.getFirstValue()); + Assert.assertEquals("B1", next.getSecondValue()); + + Assert.assertTrue(pairingIterator.hasNext()); + + next = pairingIterator.next(); + Assert.assertEquals(null, next.getFirstValue()); + Assert.assertEquals("B2", next.getSecondValue()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testTwoIteratorsWithNullValues() { + final List firstList = Arrays.asList(new String[] { null, "A2" }); + final List secondList = Arrays.asList(new String[] { "B1", null }); + final PairingIterator pairingIterator = new PairingIterator<>(firstList.iterator(), + secondList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Entry next = pairingIterator.next(); + Assert.assertEquals(null, next.getFirstValue()); + Assert.assertEquals("B1", next.getSecondValue()); + + Assert.assertTrue(pairingIterator.hasNext()); + next = pairingIterator.next(); + Assert.assertEquals("A2", next.getFirstValue()); + Assert.assertEquals(null, next.getSecondValue()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testRemoveWithOneNullIterator() { + final List firstList = new ArrayList<>(); + firstList.add("A1"); + final Iterator leftIterator = firstList.iterator(); + final PairingIterator pairingIterator = new PairingIterator<>(leftIterator, null); + + pairingIterator.next(); + pairingIterator.remove(); + Assert.assertTrue(firstList.isEmpty()); + } + + @Test + public void testRemoveWithOneNullIterator2() { + final List secondList = new ArrayList<>(); + secondList.add("A1"); + final Iterator rightIterator = secondList.iterator(); + final PairingIterator pairingIterator = new PairingIterator<>(null, rightIterator); + + pairingIterator.next(); + pairingIterator.remove(); + Assert.assertTrue(secondList.isEmpty()); + } + + @Test + public void testEntryEquals_notEquals() { + final String firstValue = "A"; + final String secondValue = "B"; + final Entry entry1 = new Entry<>(firstValue, secondValue); + final Entry entry2 = new Entry<>(secondValue, firstValue); + + Assert.assertNotEquals(entry1, entry2); + } + + @Test + public void testEntryEquals_equals() { + final String firstValue = "A"; + final String secondValue = "A"; + final Entry entry1 = new Entry<>(firstValue, secondValue); + final Entry entry2 = new Entry<>(secondValue, firstValue); + + Assert.assertEquals(entry1, entry2); + } + + @Test + public void testEntryEquals_equalsSameInstance() { + final String firstValue = "A"; + final String secondValue = "A"; + final Entry entry1 = new Entry<>(firstValue, secondValue); + + Assert.assertEquals(entry1, entry1); + } + + @Override + public Iterator> makeEmptyIterator() { + return new PairingIterator(IteratorUtils.emptyIterator(), + IteratorUtils.emptyIterator()); + } + + @Override + public Iterator> makeObject() { + final List firstList = new ArrayList<>(); + firstList.add("A1"); + firstList.add("A2"); + firstList.add("A3"); + final List secondList = new ArrayList<>(); + secondList.add("B1"); + secondList.add("B2"); + return new PairingIterator<>(firstList.iterator(), secondList.iterator()); + } } From a2b8f33229be2da04425e39e84bf594297b1d0d2 Mon Sep 17 00:00:00 2001 From: Dennis Goermann Date: Wed, 22 May 2019 18:40:49 +0200 Subject: [PATCH 4/6] COLLETION-699 spaces instead of tabs spaces instead of tabs --- .../iterators/PairingIterator.java | 230 +++++----- .../iterators/PairingIteratorTest.java | 396 +++++++++--------- 2 files changed, 313 insertions(+), 313 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java b/src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java index eaf656f906..4b95884ef5 100644 --- a/src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java +++ b/src/main/java/org/apache/commons/collections4/iterators/PairingIterator.java @@ -47,120 +47,120 @@ */ public class PairingIterator implements Iterator> { - private final Iterator firstIterator; - private final Iterator secondIterator; - - /** - * Constructs a new PairingIterator that will provide a - * {@link Entry} containing the child iterator elements. - * - * @param firstIterator the first iterator - * @param secondIterator the second iterator - */ - public PairingIterator(Iterator firstIterator, Iterator secondIterator) { - this.firstIterator = firstIterator; - this.secondIterator = secondIterator; - } - - /** - * Returns {@code true} if one of the child iterators has remaining elements. - */ - @Override - public boolean hasNext() { - return (null != firstIterator && firstIterator.hasNext()) - || (null != secondIterator && secondIterator.hasNext()); - } - - /** - * Returns the next {@link Entry} with the next values of the child iterators. - * - * @return a {@link Entry} with the next values of child iterators - * @throws NoSuchElementException if no child iterator has any more elements - */ - @Override - public Entry next() throws NoSuchElementException { - if (!hasNext()) { - throw new NoSuchElementException(); - } - final F firstValue = null != firstIterator && firstIterator.hasNext() ? firstIterator.next() : null; - final S secondValue = null != secondIterator && secondIterator.hasNext() ? secondIterator.next() : null; - return new Entry(firstValue, secondValue); - } - - /** - * Removes the last returned values of the child iterators. - */ - @Override - public void remove() { - if (firstIterator != null) { - firstIterator.remove(); - } - if (secondIterator != null) { - secondIterator.remove(); - } - } - - /** - * Contains two values of different generic types. - * - * @param the type of the first element - * @param the type of the second element - */ - public static class Entry { - private final F firstValue; - private final S secondValue; - - /** - * Constructs a new {@link Entry} of two generic values. - * - * @param firstValue the first value - * @param secondValue the second value - */ - Entry(F firstValue, S secondValue) { - this.firstValue = firstValue; - this.secondValue = secondValue; - } - - /** - * Returns the first value. - * - * @return the first value - */ - public F getFirstValue() { - return firstValue; - } - - /** - * Returns the second value. - * - * @return the second value - */ - public S getSecondValue() { - return secondValue; - } - - @Override - public int hashCode() { - return Objects.hash(firstValue, secondValue); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - Entry other = (Entry) obj; - return Objects.equals(firstValue, other.firstValue) && Objects.equals(secondValue, other.secondValue); - } - - @Override - public String toString() { - return "Entry [firstValue=" + firstValue + ", secondValue=" + secondValue + "]"; - } - - } + private final Iterator firstIterator; + private final Iterator secondIterator; + + /** + * Constructs a new PairingIterator that will provide a + * {@link Entry} containing the child iterator elements. + * + * @param firstIterator the first iterator + * @param secondIterator the second iterator + */ + public PairingIterator(Iterator firstIterator, Iterator secondIterator) { + this.firstIterator = firstIterator; + this.secondIterator = secondIterator; + } + + /** + * Returns {@code true} if one of the child iterators has remaining elements. + */ + @Override + public boolean hasNext() { + return (null != firstIterator && firstIterator.hasNext()) + || (null != secondIterator && secondIterator.hasNext()); + } + + /** + * Returns the next {@link Entry} with the next values of the child iterators. + * + * @return a {@link Entry} with the next values of child iterators + * @throws NoSuchElementException if no child iterator has any more elements + */ + @Override + public Entry next() throws NoSuchElementException { + if (!hasNext()) { + throw new NoSuchElementException(); + } + final F firstValue = null != firstIterator && firstIterator.hasNext() ? firstIterator.next() : null; + final S secondValue = null != secondIterator && secondIterator.hasNext() ? secondIterator.next() : null; + return new Entry(firstValue, secondValue); + } + + /** + * Removes the last returned values of the child iterators. + */ + @Override + public void remove() { + if (firstIterator != null) { + firstIterator.remove(); + } + if (secondIterator != null) { + secondIterator.remove(); + } + } + + /** + * Contains two values of different generic types. + * + * @param the type of the first element + * @param the type of the second element + */ + public static class Entry { + private final F firstValue; + private final S secondValue; + + /** + * Constructs a new {@link Entry} of two generic values. + * + * @param firstValue the first value + * @param secondValue the second value + */ + Entry(F firstValue, S secondValue) { + this.firstValue = firstValue; + this.secondValue = secondValue; + } + + /** + * Returns the first value. + * + * @return the first value + */ + public F getFirstValue() { + return firstValue; + } + + /** + * Returns the second value. + * + * @return the second value + */ + public S getSecondValue() { + return secondValue; + } + + @Override + public int hashCode() { + return Objects.hash(firstValue, secondValue); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Entry other = (Entry) obj; + return Objects.equals(firstValue, other.firstValue) && Objects.equals(secondValue, other.secondValue); + } + + @Override + public String toString() { + return "Entry [firstValue=" + firstValue + ", secondValue=" + secondValue + "]"; + } + + } } diff --git a/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java index ca9121600c..d0ca7e119b 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java @@ -34,208 +34,208 @@ */ public class PairingIteratorTest extends AbstractIteratorTest> { - public PairingIteratorTest(String testName) { - super(testName); - } - - @Test - public void testNullValuesBoth() { - final PairingIterator pairingIterator = new PairingIterator<>(null, null); - - Assert.assertFalse(pairingIterator.hasNext()); - try { - pairingIterator.next(); - fail(); + public PairingIteratorTest(String testName) { + super(testName); + } + + @Test + public void testNullValuesBoth() { + final PairingIterator pairingIterator = new PairingIterator<>(null, null); + + Assert.assertFalse(pairingIterator.hasNext()); + try { + pairingIterator.next(); + fail(); - } catch (NoSuchElementException e) { - Assert.assertNotNull(e); - } - } - - @Test - public void testNullValueFirst() { - final List firstList = Arrays.asList(new String[] { "A" }); - final PairingIterator pairingIterator = new PairingIterator<>(null, firstList.iterator()); + } catch (NoSuchElementException e) { + Assert.assertNotNull(e); + } + } + + @Test + public void testNullValueFirst() { + final List firstList = Arrays.asList(new String[] { "A" }); + final PairingIterator pairingIterator = new PairingIterator<>(null, firstList.iterator()); - Assert.assertTrue(pairingIterator.hasNext()); + Assert.assertTrue(pairingIterator.hasNext()); - Entry next = pairingIterator.next(); - Assert.assertEquals(null, next.getFirstValue()); - Assert.assertEquals("A", next.getSecondValue()); - } + Entry next = pairingIterator.next(); + Assert.assertEquals(null, next.getFirstValue()); + Assert.assertEquals("A", next.getSecondValue()); + } - @Test - public void testNullValueSecond() { - final List firstList = Arrays.asList(new String[] { "A" }); - final PairingIterator pairingIterator = new PairingIterator<>(firstList.iterator(), null); + @Test + public void testNullValueSecond() { + final List firstList = Arrays.asList(new String[] { "A" }); + final PairingIterator pairingIterator = new PairingIterator<>(firstList.iterator(), null); - Assert.assertTrue(pairingIterator.hasNext()); - - Entry next = pairingIterator.next(); - Assert.assertEquals("A", next.getFirstValue()); - Assert.assertEquals(null, next.getSecondValue()); - } - - @Test - public void testTwoIteratorsWithBothTwoElements() { - final List firstList = Arrays.asList(new String[] { "A1", "A2" }); - final List secondList = Arrays.asList(new String[] { "B1", "B2" }); - final PairingIterator pairingIterator = new PairingIterator<>(firstList.iterator(), - secondList.iterator()); - - Assert.assertTrue(pairingIterator.hasNext()); - - Entry next = pairingIterator.next(); - Assert.assertEquals("A1", next.getFirstValue()); - Assert.assertEquals("B1", next.getSecondValue()); - - Assert.assertTrue(pairingIterator.hasNext()); - next = pairingIterator.next(); - Assert.assertEquals("A2", next.getFirstValue()); - Assert.assertEquals("B2", next.getSecondValue()); - - Assert.assertFalse(pairingIterator.hasNext()); - } - - @Test - public void testTwoIteratorsWithDifferentSize() { - final List firstList = Arrays.asList(new String[] { "A1", "A2", "A3" }); - final List secondList = Arrays.asList(new String[] { "B1", "B2" }); - final PairingIterator pairingIterator = new PairingIterator<>(firstList.iterator(), - secondList.iterator()); - - Assert.assertTrue(pairingIterator.hasNext()); - - Entry next = pairingIterator.next(); - Assert.assertEquals("A1", next.getFirstValue()); - Assert.assertEquals("B1", next.getSecondValue()); - - Assert.assertTrue(pairingIterator.hasNext()); - - next = pairingIterator.next(); - Assert.assertEquals("A2", next.getFirstValue()); - Assert.assertEquals("B2", next.getSecondValue()); - - Assert.assertTrue(pairingIterator.hasNext()); - next = pairingIterator.next(); - Assert.assertEquals("A3", next.getFirstValue()); - Assert.assertEquals(null, next.getSecondValue()); - - Assert.assertFalse(pairingIterator.hasNext()); - } - - @Test - public void testTwoIteratorsWithDifferentSize2() { - final List firstList = Arrays.asList(new String[] { "A1", }); - final List secondList = Arrays.asList(new String[] { "B1", "B2" }); - final PairingIterator pairingIterator = new PairingIterator<>(firstList.iterator(), - secondList.iterator()); - - Assert.assertTrue(pairingIterator.hasNext()); - - Entry next = pairingIterator.next(); - Assert.assertEquals("A1", next.getFirstValue()); - Assert.assertEquals("B1", next.getSecondValue()); - - Assert.assertTrue(pairingIterator.hasNext()); - - next = pairingIterator.next(); - Assert.assertEquals(null, next.getFirstValue()); - Assert.assertEquals("B2", next.getSecondValue()); - - Assert.assertFalse(pairingIterator.hasNext()); - } - - @Test - public void testTwoIteratorsWithNullValues() { - final List firstList = Arrays.asList(new String[] { null, "A2" }); - final List secondList = Arrays.asList(new String[] { "B1", null }); - final PairingIterator pairingIterator = new PairingIterator<>(firstList.iterator(), - secondList.iterator()); - - Assert.assertTrue(pairingIterator.hasNext()); - - Entry next = pairingIterator.next(); - Assert.assertEquals(null, next.getFirstValue()); - Assert.assertEquals("B1", next.getSecondValue()); - - Assert.assertTrue(pairingIterator.hasNext()); - next = pairingIterator.next(); - Assert.assertEquals("A2", next.getFirstValue()); - Assert.assertEquals(null, next.getSecondValue()); - - Assert.assertFalse(pairingIterator.hasNext()); - } - - @Test - public void testRemoveWithOneNullIterator() { - final List firstList = new ArrayList<>(); - firstList.add("A1"); - final Iterator leftIterator = firstList.iterator(); - final PairingIterator pairingIterator = new PairingIterator<>(leftIterator, null); - - pairingIterator.next(); - pairingIterator.remove(); - Assert.assertTrue(firstList.isEmpty()); - } - - @Test - public void testRemoveWithOneNullIterator2() { - final List secondList = new ArrayList<>(); - secondList.add("A1"); - final Iterator rightIterator = secondList.iterator(); - final PairingIterator pairingIterator = new PairingIterator<>(null, rightIterator); - - pairingIterator.next(); - pairingIterator.remove(); - Assert.assertTrue(secondList.isEmpty()); - } - - @Test - public void testEntryEquals_notEquals() { - final String firstValue = "A"; - final String secondValue = "B"; - final Entry entry1 = new Entry<>(firstValue, secondValue); - final Entry entry2 = new Entry<>(secondValue, firstValue); - - Assert.assertNotEquals(entry1, entry2); - } - - @Test - public void testEntryEquals_equals() { - final String firstValue = "A"; - final String secondValue = "A"; - final Entry entry1 = new Entry<>(firstValue, secondValue); - final Entry entry2 = new Entry<>(secondValue, firstValue); - - Assert.assertEquals(entry1, entry2); - } - - @Test - public void testEntryEquals_equalsSameInstance() { - final String firstValue = "A"; - final String secondValue = "A"; - final Entry entry1 = new Entry<>(firstValue, secondValue); - - Assert.assertEquals(entry1, entry1); - } - - @Override - public Iterator> makeEmptyIterator() { - return new PairingIterator(IteratorUtils.emptyIterator(), - IteratorUtils.emptyIterator()); - } - - @Override - public Iterator> makeObject() { - final List firstList = new ArrayList<>(); - firstList.add("A1"); - firstList.add("A2"); - firstList.add("A3"); - final List secondList = new ArrayList<>(); - secondList.add("B1"); - secondList.add("B2"); - return new PairingIterator<>(firstList.iterator(), secondList.iterator()); - } + Assert.assertTrue(pairingIterator.hasNext()); + + Entry next = pairingIterator.next(); + Assert.assertEquals("A", next.getFirstValue()); + Assert.assertEquals(null, next.getSecondValue()); + } + + @Test + public void testTwoIteratorsWithBothTwoElements() { + final List firstList = Arrays.asList(new String[] { "A1", "A2" }); + final List secondList = Arrays.asList(new String[] { "B1", "B2" }); + final PairingIterator pairingIterator = new PairingIterator<>(firstList.iterator(), + secondList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Entry next = pairingIterator.next(); + Assert.assertEquals("A1", next.getFirstValue()); + Assert.assertEquals("B1", next.getSecondValue()); + + Assert.assertTrue(pairingIterator.hasNext()); + next = pairingIterator.next(); + Assert.assertEquals("A2", next.getFirstValue()); + Assert.assertEquals("B2", next.getSecondValue()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testTwoIteratorsWithDifferentSize() { + final List firstList = Arrays.asList(new String[] { "A1", "A2", "A3" }); + final List secondList = Arrays.asList(new String[] { "B1", "B2" }); + final PairingIterator pairingIterator = new PairingIterator<>(firstList.iterator(), + secondList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Entry next = pairingIterator.next(); + Assert.assertEquals("A1", next.getFirstValue()); + Assert.assertEquals("B1", next.getSecondValue()); + + Assert.assertTrue(pairingIterator.hasNext()); + + next = pairingIterator.next(); + Assert.assertEquals("A2", next.getFirstValue()); + Assert.assertEquals("B2", next.getSecondValue()); + + Assert.assertTrue(pairingIterator.hasNext()); + next = pairingIterator.next(); + Assert.assertEquals("A3", next.getFirstValue()); + Assert.assertEquals(null, next.getSecondValue()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testTwoIteratorsWithDifferentSize2() { + final List firstList = Arrays.asList(new String[] { "A1", }); + final List secondList = Arrays.asList(new String[] { "B1", "B2" }); + final PairingIterator pairingIterator = new PairingIterator<>(firstList.iterator(), + secondList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Entry next = pairingIterator.next(); + Assert.assertEquals("A1", next.getFirstValue()); + Assert.assertEquals("B1", next.getSecondValue()); + + Assert.assertTrue(pairingIterator.hasNext()); + + next = pairingIterator.next(); + Assert.assertEquals(null, next.getFirstValue()); + Assert.assertEquals("B2", next.getSecondValue()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testTwoIteratorsWithNullValues() { + final List firstList = Arrays.asList(new String[] { null, "A2" }); + final List secondList = Arrays.asList(new String[] { "B1", null }); + final PairingIterator pairingIterator = new PairingIterator<>(firstList.iterator(), + secondList.iterator()); + + Assert.assertTrue(pairingIterator.hasNext()); + + Entry next = pairingIterator.next(); + Assert.assertEquals(null, next.getFirstValue()); + Assert.assertEquals("B1", next.getSecondValue()); + + Assert.assertTrue(pairingIterator.hasNext()); + next = pairingIterator.next(); + Assert.assertEquals("A2", next.getFirstValue()); + Assert.assertEquals(null, next.getSecondValue()); + + Assert.assertFalse(pairingIterator.hasNext()); + } + + @Test + public void testRemoveWithOneNullIterator() { + final List firstList = new ArrayList<>(); + firstList.add("A1"); + final Iterator leftIterator = firstList.iterator(); + final PairingIterator pairingIterator = new PairingIterator<>(leftIterator, null); + + pairingIterator.next(); + pairingIterator.remove(); + Assert.assertTrue(firstList.isEmpty()); + } + + @Test + public void testRemoveWithOneNullIterator2() { + final List secondList = new ArrayList<>(); + secondList.add("A1"); + final Iterator rightIterator = secondList.iterator(); + final PairingIterator pairingIterator = new PairingIterator<>(null, rightIterator); + + pairingIterator.next(); + pairingIterator.remove(); + Assert.assertTrue(secondList.isEmpty()); + } + + @Test + public void testEntryEquals_notEquals() { + final String firstValue = "A"; + final String secondValue = "B"; + final Entry entry1 = new Entry<>(firstValue, secondValue); + final Entry entry2 = new Entry<>(secondValue, firstValue); + + Assert.assertNotEquals(entry1, entry2); + } + + @Test + public void testEntryEquals_equals() { + final String firstValue = "A"; + final String secondValue = "A"; + final Entry entry1 = new Entry<>(firstValue, secondValue); + final Entry entry2 = new Entry<>(secondValue, firstValue); + + Assert.assertEquals(entry1, entry2); + } + + @Test + public void testEntryEquals_equalsSameInstance() { + final String firstValue = "A"; + final String secondValue = "A"; + final Entry entry1 = new Entry<>(firstValue, secondValue); + + Assert.assertEquals(entry1, entry1); + } + + @Override + public Iterator> makeEmptyIterator() { + return new PairingIterator(IteratorUtils.emptyIterator(), + IteratorUtils.emptyIterator()); + } + + @Override + public Iterator> makeObject() { + final List firstList = new ArrayList<>(); + firstList.add("A1"); + firstList.add("A2"); + firstList.add("A3"); + final List secondList = new ArrayList<>(); + secondList.add("B1"); + secondList.add("B2"); + return new PairingIterator<>(firstList.iterator(), secondList.iterator()); + } } From 5b342e43722b3f686485f3242de90a5c8900f5ad Mon Sep 17 00:00:00 2001 From: Dennis Goermann Date: Wed, 22 May 2019 18:43:10 +0200 Subject: [PATCH 5/6] COLLECTIONS-699 change dependcy back Change dependency scope of commons-lang3 back to "test" --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index bae2284893..efc41a33c2 100644 --- a/pom.xml +++ b/pom.xml @@ -457,7 +457,7 @@ org.apache.commons commons-lang3 3.9 - provided + test From ba54ebc093ee4fd38e7f6878909e831c5d38cb90 Mon Sep 17 00:00:00 2001 From: Dennis Goermann Date: Thu, 23 May 2019 18:33:20 +0200 Subject: [PATCH 6/6] COLLECTIONS-699 more unit tests unit tests for equals and toString of the dto entry --- .../iterators/PairingIteratorTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java b/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java index d0ca7e119b..8c1d188909 100644 --- a/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java +++ b/src/test/java/org/apache/commons/collections4/iterators/PairingIteratorTest.java @@ -219,6 +219,36 @@ public void testEntryEquals_equalsSameInstance() { Assert.assertEquals(entry1, entry1); } + + @Test + public void testEntryEquals_null() { + final String firstValue = "A"; + final String secondValue = "A"; + final Entry entry1 = new Entry<>(firstValue, secondValue); + + Assert.assertNotEquals(entry1, null); + } + + @Test + public void testEntryEquals_differentClasses() { + final String firstValue = "A"; + final String secondValue = "A"; + final Entry entry1 = new Entry<>(firstValue, secondValue); + + Assert.assertNotEquals(entry1, ""); + } + + @Test + public void testEntryToString() { + final String firstValue = "A"; + final String secondValue = "A"; + final Entry entry = new Entry<>(firstValue, secondValue); + + String string = entry.toString(); + Assert.assertTrue(string.contains(firstValue)); + Assert.assertTrue(string.contains(secondValue)); + + } @Override public Iterator> makeEmptyIterator() {