/* * I provide a simple wrapper around a char reader, which * allow me to examine the current char without moving on * to the next. */ package dk.itu.kasper.Macros; import java.io.Reader; import java.io.IOException; public class ReadIterator { private Reader in; private int current; /* current is -1 if the reader is empty. * current is the next char to be returned by next; */ public ReadIterator(Reader r){ in = r; try{ current = in.read(); }catch(IOException e){ try{ in.close(); }catch(IOException ee){}; current = -1; } } /* pre: none; * post: true if there is a current char; */ public boolean hasMore(){ return current != -1; } /* pre: hasMore() * post: returns the current char */ public char current(){ return (char)current; } /* pre: hasMore() * post: returns current. then moves to next char if any */ public char next(){ char ret = current(); try{ current = in.read(); }catch(IOException e){ try{ in.close(); }catch(IOException ee){}; current = -1; } return ret; } }