Class RemoveUnsupportedIterator<E>

  • Type Parameters:
    E - The type that this Iterator 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 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

      Constructors 
      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.
        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 interface java.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 original Iterator reference. (
        Specified by:
        hasNext in interface java.util.Iterator<E>
        Returns:
        This will return TRUE if the internally stored iterator reference has more elements to return. It will return FALSE 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 original Iterator reference.
        Specified by:
        next in interface java.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 the Iterator to throw 'UnsupportedOperationException' to throw.
        Specified by:
        remove in interface java.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.");