Package Torello.Java.Additional
Class RemoveUnsupportedIterator<E>
- java.lang.Object
-
- Torello.Java.Additional.RemoveUnsupportedIterator<E>
-
- Type Parameters:
E- The type that thisIteratorwill 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 standardIteratorproducing methods in java. There are many times when anIteratorof the contents of aVectorwould 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 RemoveUnsupportedIterator(Iterator<E> iterator)
-
-
-
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 theUnsupportedOperationExceptionif 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:
forEachRemainingin 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 originalIteratorreference. (- Specified by:
hasNextin interfacejava.util.Iterator<E>- Returns:
- This will return
TRUEif the internally stored iterator reference has more elements to return. It will returnFALSEotherwise. - 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 originalIteratorreference.- Specified by:
nextin 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 theIteratorto throw'UnsupportedOperationException'to throw.- Specified by:
removein 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.");
-
-