[COLLECTIONS-796] SetUniqueList.createSetBasedOnList doesn't add list elements to return value

- Request in Comment: https://issues.apache.org/jira/browse/COLLECTIONS-796?focusedCommentId=17419058&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-17419058
- Adressed typo in method name.
- Method is public therefore it must be backwards compatible.
- The method 'umodifiableListIterator' was not removed but annotated with @Deprecated and calls the actual method 'unmodifiableListIterator'.
- Test was simplified, with appropriate assert-method-call
This commit is contained in:
Clemens Kurz 2021-09-23 14:27:24 +02:00 committed by Bruno P. Kinoshita
parent 14291172fe
commit ffd2a02d85
7 changed files with 59 additions and 37 deletions

View File

@ -469,7 +469,7 @@ public class IteratorUtils {
* @return an immutable version of the iterator * @return an immutable version of the iterator
*/ */
public static <E> ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator) { public static <E> ListIterator<E> unmodifiableListIterator(final ListIterator<E> listIterator) {
return UnmodifiableListIterator.umodifiableListIterator(listIterator); return UnmodifiableListIterator.unmodifiableListIterator(listIterator);
} }
/** /**

View File

@ -43,7 +43,7 @@ public final class UnmodifiableListIterator<E> implements ListIterator<E>, Unmod
* @return a new unmodifiable list iterator * @return a new unmodifiable list iterator
* @throws NullPointerException if the iterator is null * @throws NullPointerException if the iterator is null
*/ */
public static <E> ListIterator<E> umodifiableListIterator(final ListIterator<? extends E> iterator) { public static <E> ListIterator<E> unmodifiableListIterator(final ListIterator<? extends E> iterator) {
Objects.requireNonNull(iterator, "iterator"); Objects.requireNonNull(iterator, "iterator");
if (iterator instanceof Unmodifiable) { if (iterator instanceof Unmodifiable) {
@SuppressWarnings("unchecked") // safe to upcast @SuppressWarnings("unchecked") // safe to upcast
@ -53,6 +53,14 @@ public final class UnmodifiableListIterator<E> implements ListIterator<E>, Unmod
return new UnmodifiableListIterator<>(iterator); return new UnmodifiableListIterator<>(iterator);
} }
/**
* @deprecated method name has typo in it. Use {@link org.apache.commons.collections4.iterators.UnmodifiableListIterator#unmodifiableListIterator(ListIterator)} instead.
*/
@Deprecated
public static <E> ListIterator<E> umodifiableListIterator(final ListIterator<? extends E> iterator) {
return unmodifiableListIterator(iterator);
}
/** /**
* Constructor. * Constructor.
* *

View File

@ -352,6 +352,7 @@ public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
subSet = new HashSet<>(); subSet = new HashSet<>();
} }
} }
subSet.addAll(list);
return subSet; return subSet;
} }

View File

@ -118,12 +118,12 @@ public final class UnmodifiableList<E>
@Override @Override
public ListIterator<E> listIterator() { public ListIterator<E> listIterator() {
return UnmodifiableListIterator.umodifiableListIterator(decorated().listIterator()); return UnmodifiableListIterator.unmodifiableListIterator(decorated().listIterator());
} }
@Override @Override
public ListIterator<E> listIterator(final int index) { public ListIterator<E> listIterator(final int index) {
return UnmodifiableListIterator.umodifiableListIterator(decorated().listIterator(index)); return UnmodifiableListIterator.unmodifiableListIterator(decorated().listIterator(index));
} }
@Override @Override

View File

@ -307,12 +307,12 @@ public class LinkedMap<K, V> extends AbstractLinkedMap<K, V> implements Serializ
@Override @Override
public ListIterator<K> listIterator() { public ListIterator<K> listIterator() {
return UnmodifiableListIterator.umodifiableListIterator(super.listIterator()); return UnmodifiableListIterator.unmodifiableListIterator(super.listIterator());
} }
@Override @Override
public ListIterator<K> listIterator(final int fromIndex) { public ListIterator<K> listIterator(final int fromIndex) {
return UnmodifiableListIterator.umodifiableListIterator(super.listIterator(fromIndex)); return UnmodifiableListIterator.unmodifiableListIterator(super.listIterator(fromIndex));
} }
@Override @Override

View File

@ -24,13 +24,14 @@ import java.util.ListIterator;
import org.apache.commons.collections4.Unmodifiable; import org.apache.commons.collections4.Unmodifiable;
import static org.junit.jupiter.api.Assertions.assertThrows;
/** /**
* Tests the UnmodifiableListIterator. * Tests the UnmodifiableListIterator.
*
*/ */
public class UnmodifiableListIteratorTest<E> extends AbstractListIteratorTest<E> { public class UnmodifiableListIteratorTest<E> extends AbstractListIteratorTest<E> {
protected String[] testArray = { "One", "Two", "Three" }; protected String[] testArray = {"One", "Two", "Three"};
protected List<E> testList; protected List<E> testList;
public UnmodifiableListIteratorTest(final String testName) { public UnmodifiableListIteratorTest(final String testName) {
@ -49,12 +50,12 @@ public class UnmodifiableListIteratorTest<E> extends AbstractListIteratorTest<E>
@Override @Override
public ListIterator<E> makeEmptyIterator() { public ListIterator<E> makeEmptyIterator() {
return UnmodifiableListIterator.umodifiableListIterator(Collections.<E>emptyList().listIterator()); return UnmodifiableListIterator.unmodifiableListIterator(Collections.<E>emptyList().listIterator());
} }
@Override @Override
public ListIterator<E> makeObject() { public ListIterator<E> makeObject() {
return UnmodifiableListIterator.umodifiableListIterator(testList.listIterator()); return UnmodifiableListIterator.unmodifiableListIterator(testList.listIterator());
} }
@Override @Override
@ -78,15 +79,12 @@ public class UnmodifiableListIteratorTest<E> extends AbstractListIteratorTest<E>
public void testDecorateFactory() { public void testDecorateFactory() {
ListIterator<E> it = makeObject(); ListIterator<E> it = makeObject();
assertSame(it, UnmodifiableListIterator.umodifiableListIterator(it)); assertSame(it, UnmodifiableListIterator.unmodifiableListIterator(it));
it = testList.listIterator(); it = testList.listIterator();
assertTrue(it != UnmodifiableListIterator.umodifiableListIterator(it)); assertNotSame(it, UnmodifiableListIterator.unmodifiableListIterator(it));
try { assertThrows(NullPointerException.class, () -> UnmodifiableListIterator.unmodifiableListIterator(null));
UnmodifiableListIterator.umodifiableListIterator(null);
fail();
} catch (final NullPointerException ex) {}
} }
} }

View File

@ -16,14 +16,13 @@
*/ */
package org.apache.commons.collections4.list; package org.apache.commons.collections4.list;
import java.util.ArrayList; import org.apache.commons.collections4.set.UnmodifiableSet;
import java.util.Arrays; import org.junit.Assert;
import java.util.Collection; import org.junit.jupiter.api.Assertions;
import java.util.HashSet;
import java.util.LinkedList; import java.util.*;
import java.util.List;
import java.util.ListIterator; import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Set;
/** /**
* JUnit tests. * JUnit tests.
@ -194,7 +193,7 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
// repeat the test with a different class than HashSet; // repeat the test with a different class than HashSet;
// which means subclassing SetUniqueList below // which means subclassing SetUniqueList below
list = new ArrayList<>(); list = new ArrayList<>();
uniqueList = new SetUniqueList307(list, new java.util.TreeSet<E>()); uniqueList = new SetUniqueList307(list, new TreeSet<E>());
uniqueList.add((E) hello); uniqueList.add((E) hello);
uniqueList.add((E) world); uniqueList.add((E) world);
@ -558,12 +557,8 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
public void testSubListIsUnmodifiable() { public void testSubListIsUnmodifiable() {
resetFull(); resetFull();
final List<E> subList = getCollection().subList(1, 3); final List<E> subList = getCollection().subList(1, 3);
try { assertEquals(2, subList.size());
subList.remove(0); assertThrows(UnsupportedOperationException.class, () -> subList.remove(0));
fail("subList should be unmodifiable");
} catch (final UnsupportedOperationException e) {
// expected
}
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -616,11 +611,31 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
} }
} }
// public void testCreate() throws Exception { @SuppressWarnings("unchecked")
// resetEmpty(); public void testCreateSetBasedOnList() {
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/SetUniqueList.emptyCollection.version4.obj"); final List<String> list = new ArrayList<>();
// resetFull(); list.add("One");
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/SetUniqueList.fullCollection.version4.obj"); list.add("Two");
// } @SuppressWarnings("rawtypes") final SetUniqueList setUniqueList = (SetUniqueList) makeObject();
// Standard case with HashSet
final Set<String> setBasedOnList = setUniqueList.createSetBasedOnList(new HashSet<>(), list);
assertEquals(list.size(), setBasedOnList.size());
list.forEach(item -> assertTrue(setBasedOnList.contains(item)));
// Use different Set than HashSet
final Set<String> setBasedOnList1 = setUniqueList.createSetBasedOnList(new TreeSet<>(), list);
assertEquals(list.size(), setBasedOnList1.size());
list.forEach(item -> assertTrue(setBasedOnList1.contains(item)));
// throws internally NoSuchMethodException --> results in HashSet
final Set<String> setBasedOnList2 = setUniqueList.createSetBasedOnList(UnmodifiableSet.unmodifiableSet(new HashSet<>()), list);
assertEquals(list.size(), setBasedOnList2.size());
list.forEach(item -> assertTrue(setBasedOnList2.contains(item)));
// provide null values as Parameter
assertThrows(NullPointerException.class, () -> setUniqueList.createSetBasedOnList(null, list));
assertThrows(NullPointerException.class, () -> setUniqueList.createSetBasedOnList(new HashSet<>(), null));
}
} }