public static < X > Iterator < X > after ( final Iterator < X > iterator , final Runnable f ) { return new Iterator < X > ( ) { private final Runnable fOnlyOnce = new RunOnlyOnce ( f ) ; @ Override public boolean hasNext ( ) { final boolean hasNext = iterator . hasNext ( ) ; if ( ! hasNext ) { fOnlyOnce . run ( ) ; } return hasNext ; } @ Override public X next ( ) { try { return iterator . next ( ) ; } catch ( NoSuchElementException e ) { fOnlyOnce . run ( ) ; // (f exceptions are prohibited because they destroy e here) throw e ; } } @ Override public void remove ( ) { iterator . remove ( ) ; } } ; }
Run f immediately after the last element of iterator is generated . Exceptions must not be raised by f .