package dk.itu.oop.lecture5; public class DropParenthesis implements OOPIterator { private OOPIterator in; public DropParenthesis(OOPIterator in){ this.in = in; skipParenthesis(); } private void skipParenthesis(){ if ( !in.hasNext() ) return; char c = ( (Character) in.peek() ).charValue(); if ( c=='(' ){ while ( in.hasNext() &&c != ')' ){ in.next(); c = ( (Character) in.peek() ).charValue(); } if ( in.hasNext() && c == ')' ) in.next(); } } public boolean hasNext(){ return in.hasNext(); } public Object peek(){ return in.peek(); } public Object next(){ Object o = peek(); in.next(); skipParenthesis(); return o; } public OOPIterator cloneMe(){ try{ DropParenthesis clone = (DropParenthesis)super.clone(); clone.in = in.cloneMe(); return clone; }catch(CloneNotSupportedException e){ return null; } } public static void main(String[] args){ String haddock = "You #!@ Miserable (earthworms!) Numbskulls! and Carpet-sellers!"; OOPIterator itr = new DropParenthesis(new CharacterIterator( haddock ) ); while (itr.hasNext()) System.out.print( itr.next() ); System.out.println(); } }