Package Torello.Java.Additional
Class RemoveUnsupportedIterator<E>
- java.lang.Object
-
- Torello.Java.Additional.RemoveUnsupportedIterator<E>
-
- Type Parameters:
E
- The type that thisIterator
will iterate.
- All Implemented Interfaces:
java.util.Iterator<E>
public class RemoveUnsupportedIterator<E> extends java.lang.Object implements java.util.Iterator<E>
This functions identically to the normalIterator<E>
class, but preventsIterator.remove()
from ever being invoked. It does not seem clear why such an option is not included for the standardIterator
producing methods in java. There are many times when anIterator
of the contents of aVector
would be useful, but allowing the user to modify the contents would not be.
Hi-Lited Source-Code:- View Here: Torello/Java/Additional/RemoveUnsupportedIterator.java
- Open New Browser-Tab: Torello/Java/Additional/RemoveUnsupportedIterator.java
File Size: 3,462 Bytes Line Count: 72 '\n' Characters Found
-
-
Constructor Summary
Constructors Constructor Description RemoveUnsupportedIterator(Iterator<E> iterator)
This constructor is the only offered constructor.
-
-
-
Constructor Detail
-
RemoveUnsupportedIterator
public RemoveUnsupportedIterator(java.util.Iterator<E> iterator)
This constructor is the only offered constructor. All it does is "wrap" one iterator that throws theUnsupportedOperationException
if a user attempts to invoke the'remove()'
method.- Parameters:
iterator
- This should be any java iterator. Regardless of whether this iterator already throws the UnsupportedOperationException on an invocation of the 'remove()' method, this class will guarantee that the exception is thrown.
-
-
Method Detail
-
forEachRemaining
public void forEachRemaining (java.util.function.Consumer<? super E> action)
This is a standard Java Iterator-provided method.- Specified by:
forEachRemaining
in interfacejava.util.Iterator<E>
- Parameters:
action
- This (should) accept an instance of the'Consumer<E>'
functional interface, but indeed it uses a "Raw-Type" and will accept any instance of class'Consumer'
.- Throws:
java.lang.ClassCastException
- This will throw if you "screw it up" (and pass a consumer that cannot accept variable type'E'
. This will throw at RunTime, and there is no way to check this at compile-type, unfortunately.
Lucky for you, it is extremely unlikely you would ever need to invoke this method on a'RemoveUnsupportedIterator'
. Have a nice day!- Code:
- Exact Method Body:
this.iterator.forEachRemaining(action);
-
hasNext
public boolean hasNext()
This method is identical to the standard java iterator'hasNext()'
method. It merely invokes'hasNext()'
on the internally stored reference to the originalIterator
reference. (- Specified by:
hasNext
in interfacejava.util.Iterator<E>
- Returns:
- This will return
TRUE
if the internally stored iterator reference has more elements to return. It will returnFALSE
otherwise. - Code:
- Exact Method Body:
return this.iterator.hasNext();
-
next
public E next()
This method is identical to the standard java iterator'next()'
method. It merely invokes'next()'
on the internally stored reference to the originalIterator
reference.- Specified by:
next
in interfacejava.util.Iterator<E>
- Returns:
- returns the next element available.
- Code:
- Exact Method Body:
return this.iterator.next();
-
remove
public final void remove()
This ensures that any attempt to modify the internal data structure will cause theIterator
to throw'UnsupportedOperationException'
to throw.- Specified by:
remove
in interfacejava.util.Iterator<E>
- Throws:
java.lang.UnsupportedOperationException
- If this method is invoked for any reason, and under any circumstances.- Code:
- Exact Method Body:
throw new UnsupportedOperationException("Remove Operation is not supported.");
-
-