Skip to content

Commit a41f789

Browse files
committed
Add package private unwrap() method
- Add generics for the wrapped type - Add TODO for remove(): This method can be removed in 5.0 since it's implemented as a default method in Iterator. - Improve Javadoc
1 parent 410c5db commit a41f789

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix exception message in org.apache.commons.collections4.functors.FunctorUtils.validate(Consumer...)</action>
3434
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix exception message in org.apache.commons.collections4.iterators.UnmodifiableIterator.remove() to match java.util.Iterator.remove().</action>
3535
<!-- ADD -->
36+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add generics to UnmodifiableIterator for the wrapped type.</action>
3637
<!-- UPDATE -->
3738
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 81 to 84 #612.</action>
3839
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump com.google.guava:guava-testlib from 33.3.1-jre to 33.4.8-jre.</action>

src/main/java/org/apache/commons/collections4/iterators/UnmodifiableIterator.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
/**
2525
* Decorates an iterator such that it cannot be modified.
2626
* <p>
27-
* Attempts to modify it will result in an UnsupportedOperationException.
27+
* Calling {@link #remove()} throws {@link UnsupportedOperationException}.
2828
* </p>
2929
*
3030
* @param <E> the type of elements returned by this iterator.
31+
* @param <T> The wrapped Iterator type.
3132
* @since 3.0
3233
*/
33-
public final class UnmodifiableIterator<E> implements Iterator<E>, Unmodifiable {
34+
public final class UnmodifiableIterator<E, T extends Iterator<? extends E>> implements Iterator<E>, Unmodifiable {
3435

3536
/**
3637
* Decorates the specified iterator such that it cannot be modified.
@@ -50,18 +51,24 @@ public static <E> Iterator<E> unmodifiableIterator(final Iterator<? extends E> i
5051
final Iterator<E> tmpIterator = (Iterator<E>) iterator;
5152
return tmpIterator;
5253
}
54+
return wrap(iterator);
55+
}
56+
57+
static <E, T extends Iterator<? extends E>> UnmodifiableIterator<E, T> wrap(final T iterator) {
5358
return new UnmodifiableIterator<>(iterator);
5459
}
5560

56-
/** The iterator being decorated */
57-
private final Iterator<? extends E> iterator;
61+
/**
62+
* The decorated iterator.
63+
*/
64+
private final T iterator;
5865

5966
/**
6067
* Constructs a new instance.
6168
*
62-
* @param iterator the iterator to decorate
69+
* @param iterator the iterator to decorate.
6370
*/
64-
private UnmodifiableIterator(final Iterator<? extends E> iterator) {
71+
private UnmodifiableIterator(final T iterator) {
6572
this.iterator = iterator;
6673
}
6774

@@ -75,9 +82,14 @@ public E next() {
7582
return iterator.next();
7683
}
7784

85+
// TODO This method can be removed in 5.0 since it's implemented as a default method in Iterator.
7886
@Override
7987
public void remove() {
8088
throw new UnsupportedOperationException("remove");
8189
}
8290

91+
T unwrap() {
92+
return iterator;
93+
}
94+
8395
}

src/test/java/org/apache/commons/collections4/iterators/UnmodifiableIteratorTest.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
1718
package org.apache.commons.collections4.iterators;
1819

1920
import static org.junit.jupiter.api.Assertions.assertNotSame;
@@ -38,8 +39,8 @@
3839
*/
3940
public class UnmodifiableIteratorTest<E> extends AbstractIteratorTest<E> {
4041

41-
protected String[] testArray = { "One", "Two", "Three" };
42-
protected List<E> testList;
42+
private final String[] testArray = { "One", "Two", "Three" };
43+
private List<E> testList;
4344

4445
@Override
4546
public Iterator<E> makeEmptyIterator() {
@@ -66,10 +67,8 @@ public boolean supportsRemove() {
6667
void testDecorateFactory() {
6768
Iterator<E> it = makeObject();
6869
assertSame(it, UnmodifiableIterator.unmodifiableIterator(it));
69-
7070
it = testList.iterator();
7171
assertNotSame(it, UnmodifiableIterator.unmodifiableIterator(it));
72-
7372
assertThrows(NullPointerException.class, () -> UnmodifiableIterator.unmodifiableIterator(null));
7473
}
7574

@@ -78,4 +77,19 @@ void testIterator() {
7877
assertTrue(makeEmptyIterator() instanceof Unmodifiable);
7978
}
8079

80+
@Test
81+
void testUnwrap() {
82+
final Iterator<E> iterator = testList.iterator();
83+
@SuppressWarnings("unchecked")
84+
final UnmodifiableIterator<E, Iterator<E>> unmodifiableIterator = (UnmodifiableIterator<E, Iterator<E>>) UnmodifiableIterator
85+
.unmodifiableIterator(iterator);
86+
assertSame(iterator, unmodifiableIterator.unwrap());
87+
}
88+
89+
@Test
90+
void testWrapUnwrap() {
91+
final Iterator<E> iterator = testList.iterator();
92+
final UnmodifiableIterator<E, Iterator<E>> unmodifiableIterator = UnmodifiableIterator.wrap(iterator);
93+
assertSame(iterator, unmodifiableIterator.unwrap());
94+
}
8195
}

0 commit comments

Comments
 (0)