COLLETION-699 spaces instead of tabs

spaces instead of tabs
This commit is contained in:
Dennis Goermann 2019-05-22 18:40:49 +02:00
parent b118b89436
commit a2b8f33229
2 changed files with 262 additions and 262 deletions

View File

@ -47,120 +47,120 @@ import org.apache.commons.collections4.iterators.PairingIterator.Entry;
*/ */
public class PairingIterator<F, S> implements Iterator<Entry<F, S>> { public class PairingIterator<F, S> implements Iterator<Entry<F, S>> {
private final Iterator<F> firstIterator; private final Iterator<F> firstIterator;
private final Iterator<S> secondIterator; private final Iterator<S> secondIterator;
/** /**
* Constructs a new <code>PairingIterator</code> that will provide a * Constructs a new <code>PairingIterator</code> that will provide a
* {@link Entry} containing the child iterator elements. * {@link Entry} containing the child iterator elements.
* *
* @param firstIterator the first iterator * @param firstIterator the first iterator
* @param secondIterator the second iterator * @param secondIterator the second iterator
*/ */
public PairingIterator(Iterator<F> firstIterator, Iterator<S> secondIterator) { public PairingIterator(Iterator<F> firstIterator, Iterator<S> secondIterator) {
this.firstIterator = firstIterator; this.firstIterator = firstIterator;
this.secondIterator = secondIterator; this.secondIterator = secondIterator;
} }
/** /**
* Returns {@code true} if one of the child iterators has remaining elements. * Returns {@code true} if one of the child iterators has remaining elements.
*/ */
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return (null != firstIterator && firstIterator.hasNext()) return (null != firstIterator && firstIterator.hasNext())
|| (null != secondIterator && secondIterator.hasNext()); || (null != secondIterator && secondIterator.hasNext());
} }
/** /**
* Returns the next {@link Entry} with the next values of the child iterators. * Returns the next {@link Entry} with the next values of the child iterators.
* *
* @return a {@link Entry} with the next values of child iterators * @return a {@link Entry} with the next values of child iterators
* @throws NoSuchElementException if no child iterator has any more elements * @throws NoSuchElementException if no child iterator has any more elements
*/ */
@Override @Override
public Entry<F, S> next() throws NoSuchElementException { public Entry<F, S> next() throws NoSuchElementException {
if (!hasNext()) { if (!hasNext()) {
throw new NoSuchElementException(); throw new NoSuchElementException();
} }
final F firstValue = null != firstIterator && firstIterator.hasNext() ? firstIterator.next() : null; final F firstValue = null != firstIterator && firstIterator.hasNext() ? firstIterator.next() : null;
final S secondValue = null != secondIterator && secondIterator.hasNext() ? secondIterator.next() : null; final S secondValue = null != secondIterator && secondIterator.hasNext() ? secondIterator.next() : null;
return new Entry<F, S>(firstValue, secondValue); return new Entry<F, S>(firstValue, secondValue);
} }
/** /**
* Removes the last returned values of the child iterators. * Removes the last returned values of the child iterators.
*/ */
@Override @Override
public void remove() { public void remove() {
if (firstIterator != null) { if (firstIterator != null) {
firstIterator.remove(); firstIterator.remove();
} }
if (secondIterator != null) { if (secondIterator != null) {
secondIterator.remove(); secondIterator.remove();
} }
} }
/** /**
* Contains two values of different generic types. * Contains two values of different generic types.
* *
* @param <F> the type of the first element * @param <F> the type of the first element
* @param <S> the type of the second element * @param <S> the type of the second element
*/ */
public static class Entry<F, S> { public static class Entry<F, S> {
private final F firstValue; private final F firstValue;
private final S secondValue; private final S secondValue;
/** /**
* Constructs a new {@link Entry} of two generic values. * Constructs a new {@link Entry} of two generic values.
* *
* @param firstValue the first value * @param firstValue the first value
* @param secondValue the second value * @param secondValue the second value
*/ */
Entry(F firstValue, S secondValue) { Entry(F firstValue, S secondValue) {
this.firstValue = firstValue; this.firstValue = firstValue;
this.secondValue = secondValue; this.secondValue = secondValue;
} }
/** /**
* Returns the first value. * Returns the first value.
* *
* @return the first value * @return the first value
*/ */
public F getFirstValue() { public F getFirstValue() {
return firstValue; return firstValue;
} }
/** /**
* Returns the second value. * Returns the second value.
* *
* @return the second value * @return the second value
*/ */
public S getSecondValue() { public S getSecondValue() {
return secondValue; return secondValue;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(firstValue, secondValue); return Objects.hash(firstValue, secondValue);
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) if (this == obj)
return true; return true;
if (obj == null) if (obj == null)
return false; return false;
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
Entry other = (Entry) obj; Entry other = (Entry) obj;
return Objects.equals(firstValue, other.firstValue) && Objects.equals(secondValue, other.secondValue); return Objects.equals(firstValue, other.firstValue) && Objects.equals(secondValue, other.secondValue);
} }
@Override @Override
public String toString() { public String toString() {
return "Entry [firstValue=" + firstValue + ", secondValue=" + secondValue + "]"; return "Entry [firstValue=" + firstValue + ", secondValue=" + secondValue + "]";
} }
} }
} }

View File

@ -34,208 +34,208 @@ import org.junit.Test;
*/ */
public class PairingIteratorTest extends AbstractIteratorTest<Entry<String, String>> { public class PairingIteratorTest extends AbstractIteratorTest<Entry<String, String>> {
public PairingIteratorTest(String testName) { public PairingIteratorTest(String testName) {
super(testName); super(testName);
} }
@Test @Test
public void testNullValuesBoth() { public void testNullValuesBoth() {
final PairingIterator<String, String> pairingIterator = new PairingIterator<>(null, null); final PairingIterator<String, String> pairingIterator = new PairingIterator<>(null, null);
Assert.assertFalse(pairingIterator.hasNext()); Assert.assertFalse(pairingIterator.hasNext());
try { try {
pairingIterator.next(); pairingIterator.next();
fail(); fail();
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
Assert.assertNotNull(e); Assert.assertNotNull(e);
} }
} }
@Test @Test
public void testNullValueFirst() { public void testNullValueFirst() {
final List<String> firstList = Arrays.asList(new String[] { "A" }); final List<String> firstList = Arrays.asList(new String[] { "A" });
final PairingIterator<String, String> pairingIterator = new PairingIterator<>(null, firstList.iterator()); final PairingIterator<String, String> pairingIterator = new PairingIterator<>(null, firstList.iterator());
Assert.assertTrue(pairingIterator.hasNext()); Assert.assertTrue(pairingIterator.hasNext());
Entry<String, String> next = pairingIterator.next(); Entry<String, String> next = pairingIterator.next();
Assert.assertEquals(null, next.getFirstValue()); Assert.assertEquals(null, next.getFirstValue());
Assert.assertEquals("A", next.getSecondValue()); Assert.assertEquals("A", next.getSecondValue());
} }
@Test @Test
public void testNullValueSecond() { public void testNullValueSecond() {
final List<String> firstList = Arrays.asList(new String[] { "A" }); final List<String> firstList = Arrays.asList(new String[] { "A" });
final PairingIterator<String, String> pairingIterator = new PairingIterator<>(firstList.iterator(), null); final PairingIterator<String, String> pairingIterator = new PairingIterator<>(firstList.iterator(), null);
Assert.assertTrue(pairingIterator.hasNext()); Assert.assertTrue(pairingIterator.hasNext());
Entry<String, String> next = pairingIterator.next(); Entry<String, String> next = pairingIterator.next();
Assert.assertEquals("A", next.getFirstValue()); Assert.assertEquals("A", next.getFirstValue());
Assert.assertEquals(null, next.getSecondValue()); Assert.assertEquals(null, next.getSecondValue());
} }
@Test @Test
public void testTwoIteratorsWithBothTwoElements() { public void testTwoIteratorsWithBothTwoElements() {
final List<String> firstList = Arrays.asList(new String[] { "A1", "A2" }); final List<String> firstList = Arrays.asList(new String[] { "A1", "A2" });
final List<String> secondList = Arrays.asList(new String[] { "B1", "B2" }); final List<String> secondList = Arrays.asList(new String[] { "B1", "B2" });
final PairingIterator<String, String> pairingIterator = new PairingIterator<>(firstList.iterator(), final PairingIterator<String, String> pairingIterator = new PairingIterator<>(firstList.iterator(),
secondList.iterator()); secondList.iterator());
Assert.assertTrue(pairingIterator.hasNext()); Assert.assertTrue(pairingIterator.hasNext());
Entry<String, String> next = pairingIterator.next(); Entry<String, String> next = pairingIterator.next();
Assert.assertEquals("A1", next.getFirstValue()); Assert.assertEquals("A1", next.getFirstValue());
Assert.assertEquals("B1", next.getSecondValue()); Assert.assertEquals("B1", next.getSecondValue());
Assert.assertTrue(pairingIterator.hasNext()); Assert.assertTrue(pairingIterator.hasNext());
next = pairingIterator.next(); next = pairingIterator.next();
Assert.assertEquals("A2", next.getFirstValue()); Assert.assertEquals("A2", next.getFirstValue());
Assert.assertEquals("B2", next.getSecondValue()); Assert.assertEquals("B2", next.getSecondValue());
Assert.assertFalse(pairingIterator.hasNext()); Assert.assertFalse(pairingIterator.hasNext());
} }
@Test @Test
public void testTwoIteratorsWithDifferentSize() { public void testTwoIteratorsWithDifferentSize() {
final List<String> firstList = Arrays.asList(new String[] { "A1", "A2", "A3" }); final List<String> firstList = Arrays.asList(new String[] { "A1", "A2", "A3" });
final List<String> secondList = Arrays.asList(new String[] { "B1", "B2" }); final List<String> secondList = Arrays.asList(new String[] { "B1", "B2" });
final PairingIterator<String, String> pairingIterator = new PairingIterator<>(firstList.iterator(), final PairingIterator<String, String> pairingIterator = new PairingIterator<>(firstList.iterator(),
secondList.iterator()); secondList.iterator());
Assert.assertTrue(pairingIterator.hasNext()); Assert.assertTrue(pairingIterator.hasNext());
Entry<String, String> next = pairingIterator.next(); Entry<String, String> next = pairingIterator.next();
Assert.assertEquals("A1", next.getFirstValue()); Assert.assertEquals("A1", next.getFirstValue());
Assert.assertEquals("B1", next.getSecondValue()); Assert.assertEquals("B1", next.getSecondValue());
Assert.assertTrue(pairingIterator.hasNext()); Assert.assertTrue(pairingIterator.hasNext());
next = pairingIterator.next(); next = pairingIterator.next();
Assert.assertEquals("A2", next.getFirstValue()); Assert.assertEquals("A2", next.getFirstValue());
Assert.assertEquals("B2", next.getSecondValue()); Assert.assertEquals("B2", next.getSecondValue());
Assert.assertTrue(pairingIterator.hasNext()); Assert.assertTrue(pairingIterator.hasNext());
next = pairingIterator.next(); next = pairingIterator.next();
Assert.assertEquals("A3", next.getFirstValue()); Assert.assertEquals("A3", next.getFirstValue());
Assert.assertEquals(null, next.getSecondValue()); Assert.assertEquals(null, next.getSecondValue());
Assert.assertFalse(pairingIterator.hasNext()); Assert.assertFalse(pairingIterator.hasNext());
} }
@Test @Test
public void testTwoIteratorsWithDifferentSize2() { public void testTwoIteratorsWithDifferentSize2() {
final List<String> firstList = Arrays.asList(new String[] { "A1", }); final List<String> firstList = Arrays.asList(new String[] { "A1", });
final List<String> secondList = Arrays.asList(new String[] { "B1", "B2" }); final List<String> secondList = Arrays.asList(new String[] { "B1", "B2" });
final PairingIterator<String, String> pairingIterator = new PairingIterator<>(firstList.iterator(), final PairingIterator<String, String> pairingIterator = new PairingIterator<>(firstList.iterator(),
secondList.iterator()); secondList.iterator());
Assert.assertTrue(pairingIterator.hasNext()); Assert.assertTrue(pairingIterator.hasNext());
Entry<String, String> next = pairingIterator.next(); Entry<String, String> next = pairingIterator.next();
Assert.assertEquals("A1", next.getFirstValue()); Assert.assertEquals("A1", next.getFirstValue());
Assert.assertEquals("B1", next.getSecondValue()); Assert.assertEquals("B1", next.getSecondValue());
Assert.assertTrue(pairingIterator.hasNext()); Assert.assertTrue(pairingIterator.hasNext());
next = pairingIterator.next(); next = pairingIterator.next();
Assert.assertEquals(null, next.getFirstValue()); Assert.assertEquals(null, next.getFirstValue());
Assert.assertEquals("B2", next.getSecondValue()); Assert.assertEquals("B2", next.getSecondValue());
Assert.assertFalse(pairingIterator.hasNext()); Assert.assertFalse(pairingIterator.hasNext());
} }
@Test @Test
public void testTwoIteratorsWithNullValues() { public void testTwoIteratorsWithNullValues() {
final List<String> firstList = Arrays.asList(new String[] { null, "A2" }); final List<String> firstList = Arrays.asList(new String[] { null, "A2" });
final List<String> secondList = Arrays.asList(new String[] { "B1", null }); final List<String> secondList = Arrays.asList(new String[] { "B1", null });
final PairingIterator<String, String> pairingIterator = new PairingIterator<>(firstList.iterator(), final PairingIterator<String, String> pairingIterator = new PairingIterator<>(firstList.iterator(),
secondList.iterator()); secondList.iterator());
Assert.assertTrue(pairingIterator.hasNext()); Assert.assertTrue(pairingIterator.hasNext());
Entry<String, String> next = pairingIterator.next(); Entry<String, String> next = pairingIterator.next();
Assert.assertEquals(null, next.getFirstValue()); Assert.assertEquals(null, next.getFirstValue());
Assert.assertEquals("B1", next.getSecondValue()); Assert.assertEquals("B1", next.getSecondValue());
Assert.assertTrue(pairingIterator.hasNext()); Assert.assertTrue(pairingIterator.hasNext());
next = pairingIterator.next(); next = pairingIterator.next();
Assert.assertEquals("A2", next.getFirstValue()); Assert.assertEquals("A2", next.getFirstValue());
Assert.assertEquals(null, next.getSecondValue()); Assert.assertEquals(null, next.getSecondValue());
Assert.assertFalse(pairingIterator.hasNext()); Assert.assertFalse(pairingIterator.hasNext());
} }
@Test @Test
public void testRemoveWithOneNullIterator() { public void testRemoveWithOneNullIterator() {
final List<String> firstList = new ArrayList<>(); final List<String> firstList = new ArrayList<>();
firstList.add("A1"); firstList.add("A1");
final Iterator<String> leftIterator = firstList.iterator(); final Iterator<String> leftIterator = firstList.iterator();
final PairingIterator<String, String> pairingIterator = new PairingIterator<>(leftIterator, null); final PairingIterator<String, String> pairingIterator = new PairingIterator<>(leftIterator, null);
pairingIterator.next(); pairingIterator.next();
pairingIterator.remove(); pairingIterator.remove();
Assert.assertTrue(firstList.isEmpty()); Assert.assertTrue(firstList.isEmpty());
} }
@Test @Test
public void testRemoveWithOneNullIterator2() { public void testRemoveWithOneNullIterator2() {
final List<String> secondList = new ArrayList<>(); final List<String> secondList = new ArrayList<>();
secondList.add("A1"); secondList.add("A1");
final Iterator<String> rightIterator = secondList.iterator(); final Iterator<String> rightIterator = secondList.iterator();
final PairingIterator<String, String> pairingIterator = new PairingIterator<>(null, rightIterator); final PairingIterator<String, String> pairingIterator = new PairingIterator<>(null, rightIterator);
pairingIterator.next(); pairingIterator.next();
pairingIterator.remove(); pairingIterator.remove();
Assert.assertTrue(secondList.isEmpty()); Assert.assertTrue(secondList.isEmpty());
} }
@Test @Test
public void testEntryEquals_notEquals() { public void testEntryEquals_notEquals() {
final String firstValue = "A"; final String firstValue = "A";
final String secondValue = "B"; final String secondValue = "B";
final Entry<String, String> entry1 = new Entry<>(firstValue, secondValue); final Entry<String, String> entry1 = new Entry<>(firstValue, secondValue);
final Entry<String, String> entry2 = new Entry<>(secondValue, firstValue); final Entry<String, String> entry2 = new Entry<>(secondValue, firstValue);
Assert.assertNotEquals(entry1, entry2); Assert.assertNotEquals(entry1, entry2);
} }
@Test @Test
public void testEntryEquals_equals() { public void testEntryEquals_equals() {
final String firstValue = "A"; final String firstValue = "A";
final String secondValue = "A"; final String secondValue = "A";
final Entry<String, String> entry1 = new Entry<>(firstValue, secondValue); final Entry<String, String> entry1 = new Entry<>(firstValue, secondValue);
final Entry<String, String> entry2 = new Entry<>(secondValue, firstValue); final Entry<String, String> entry2 = new Entry<>(secondValue, firstValue);
Assert.assertEquals(entry1, entry2); Assert.assertEquals(entry1, entry2);
} }
@Test @Test
public void testEntryEquals_equalsSameInstance() { public void testEntryEquals_equalsSameInstance() {
final String firstValue = "A"; final String firstValue = "A";
final String secondValue = "A"; final String secondValue = "A";
final Entry<String, String> entry1 = new Entry<>(firstValue, secondValue); final Entry<String, String> entry1 = new Entry<>(firstValue, secondValue);
Assert.assertEquals(entry1, entry1); Assert.assertEquals(entry1, entry1);
} }
@Override @Override
public Iterator<Entry<String, String>> makeEmptyIterator() { public Iterator<Entry<String, String>> makeEmptyIterator() {
return new PairingIterator<String, String>(IteratorUtils.<String>emptyIterator(), return new PairingIterator<String, String>(IteratorUtils.<String>emptyIterator(),
IteratorUtils.<String>emptyIterator()); IteratorUtils.<String>emptyIterator());
} }
@Override @Override
public Iterator<Entry<String, String>> makeObject() { public Iterator<Entry<String, String>> makeObject() {
final List<String> firstList = new ArrayList<>(); final List<String> firstList = new ArrayList<>();
firstList.add("A1"); firstList.add("A1");
firstList.add("A2"); firstList.add("A2");
firstList.add("A3"); firstList.add("A3");
final List<String> secondList = new ArrayList<>(); final List<String> secondList = new ArrayList<>();
secondList.add("B1"); secondList.add("B1");
secondList.add("B2"); secondList.add("B2");
return new PairingIterator<>(firstList.iterator(), secondList.iterator()); return new PairingIterator<>(firstList.iterator(), secondList.iterator());
} }
} }