package dk.itu.oop.lecture5; public class CNL implements OOPIterator { private OOPIterator in; private char head; // if packed, then head is ".", else head is in.head public CNL(OOPIterator in){ this.in = in; setHead(); } private void setHead(){ if ( in.hasNext() ) if ( !isLetter()){ head ='.'; while (in.hasNext() && !isLetter() ) in.next(); } else head = ( (Character) in.peek() ).charValue(); else head = '.'; } public boolean hasNext(){ return in.hasNext() || head=='.'; } public Object peek(){ return new Character( head ); } public Object next(){ Object o = peek(); if (head !='.') in.next(); setHead(); return o; } private boolean isLetter(){ return Character.isLetter( ( (Character) in.peek() ).charValue() ); } public OOPIterator cloneMe(){ try{ CNL clone = (CNL)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 CNL(new CharacterIterator( haddock ) ); while (itr.hasNext()) System.out.print( itr.next() ); System.out.println(); } }