Class RemoveUnsupportedIterator<E>

  • Type Parameters:
    E - The type that this Iterator will iterate.
    All Implemented Interfaces:

    public class RemoveUnsupportedIterator<E>
    extends java.lang.Object
    implements java.util.Iterator<E>
    This functions identically to the normal Iterator<E> class, but prevents Iterator.remove() from ever being invoked. It does not seem clear why such an option is not included for the standard Iterator producing methods in java. There are many times when an Iterator of the contents of a Vector would be useful, but allowing the user to modify the contents would not be.

    • Constructor Summary

      Constructor Description
      RemoveUnsupportedIterator​(Iterator<E> iterator)
      This constructor is the only offered constructor.
    • Method Summary

      Methods: interface java.lang.Iterator
      Modifier and Type Method
      void forEachRemaining​(Consumer<? super E> action)
      boolean hasNext()
      E next()
      void remove()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 the UnsupportedOperationException if a user attempts to invoke the 'remove()' method.
        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 interface java.util.Iterator<E>
        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'.
        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!
        Exact Method Body:
      • 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 original Iterator reference. (
        Specified by:
        hasNext in interface java.util.Iterator<E>
        This will return TRUE if the internally stored iterator reference has more elements to return. It will return FALSE otherwise.
        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 original Iterator reference.
        Specified by:
        next in interface java.util.Iterator<E>
        returns the next element available.
        Exact Method Body:
      • remove

        🡅     🗕  🗗  🗖
        public final void remove()
        This ensures that any attempt to modify the internal data structure will cause the Iterator to throw 'UnsupportedOperationException' to throw.
        Specified by:
        remove in interface java.util.Iterator<E>
        java.lang.UnsupportedOperationException - If this method is invoked for any reason, and under any circumstances.
        Exact Method Body:
         throw new UnsupportedOperationException("Remove Operation is not supported.");