// // Enumerator.java // // // Created by Maria Eugenia Occhiuto on Fri Jul 04 2003. // Copyright (c) 2003 __MyCompanyName__. All rights reserved. // import java.util.*; import java.lang.reflect.*; import java.lang.*; public abstract class Enumerator { public abstract boolean hasMoreElements(); public abstract Object nextElement() throws NoSuchElementException; public void forall (Object o,Method f){ while (hasMoreElements()) { Object [] Parm={nextElement()}; try{f.invoke(o,Parm);} catch(Exception e){System.out.println("forall error"+e);}} } public Enumerator map (String f){return new MapEnumerator(f,this);} public Enumerator takeWhile(String s){return new TakeWhileEnumerator(s,this);} public Enumerator dropWhile(String s){return new DropWhileEnumerator(s,this);} public Enumerator filter(String s){return new FilterEnumerator(s,this);} public Object reduceRight(Object z, String s){ if (hasMoreElements()){ Class [] ParTy={z.getClass()}; Object next=nextElement(); Object [] Parm={reduceRight(z,s)}; try{ Method m=next.getClass().getMethod(s,ParTy); z=m.invoke(next,Parm);} catch(Exception e){System.out.println("reduce error"+e);}} return z;} public Object reduceLeft (Object z, String s){ if (hasMoreElements()){ Class [] ParTy={z.getClass()}; Object [] Parm ={z}; try{Object next=nextElement(); Method m=next.getClass().getMethod(s,ParTy); z=reduceLeft(m.invoke(next,Parm),s); } catch(Exception e){System.out.println("reduceLeft error"+e);}} return z;} }