KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > mybatchfwk > history > LineIterator


1 /*
2  * MyBatchFramework - Open-source batch framework.
3  * Copyright (C) 2006 Jérôme Bertèche cyberteche@users.sourceforge.net
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * Jérôme Bertèche
16  * Email: cyberteche@users.sourceforge.net
17  */

18 package net.sf.mybatchfwk.history;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 /**
25  * An adapter that exposes a buffered reader as an iterator over the returned lines.<br>
26  * The <i>close()</i> method must be called after the iteration to close the reader.
27  *
28  * @author Jérôme Bertèche (cyberteche@users.sourceforge.net)
29  */

30 public class LineIterator implements Iterator JavaDoc {
31
32     private final BufferedReader JavaDoc reader;
33
34     private String JavaDoc line = null;
35
36     /**
37      * @param reader
38      */

39     public LineIterator(BufferedReader JavaDoc reader) {
40         this.reader = reader;
41     }
42
43     /* (non-Javadoc)
44      * @see java.util.Iterator#next()
45      */

46     public Object JavaDoc next() {
47         if ((this.line == null) && (!this.hasNext())) {
48             return null;
49         } else {
50             String JavaDoc toReturn = this.line;
51             this.line = null;
52             return toReturn;
53         }
54     }
55
56     /* (non-Javadoc)
57      * @see java.util.Iterator#hasNext()
58      */

59     public boolean hasNext() throws IOEWrapper {
60         if (this.line == null) {
61             try {
62                 line = this.reader.readLine();
63             } catch (IOException JavaDoc e) {
64                 throw new IOEWrapper(e);
65             }
66             return (line != null);
67         } else {
68             return true;
69         }
70     }
71
72     /* (non-Javadoc)
73      * @see java.util.Iterator#remove()
74      */

75     public void remove() {
76         throw new UnsupportedOperationException JavaDoc(
77                 "LineIterator does not support remove()");
78     }
79     
80     /**
81      * Close the bufferred reader
82      */

83     public void close() {
84         try {
85             this.reader.close();
86         } catch (IOException JavaDoc e) {
87         }
88     }
89
90     /**
91      * A wrapper of IOException
92      */

93     public static class IOEWrapper extends RuntimeException JavaDoc {
94
95         private static final long serialVersionUID = 4963463932839852732L;
96         
97         private final IOException JavaDoc ioe;
98
99         public IOEWrapper(IOException JavaDoc ioe) {
100             this.ioe = ioe;
101         }
102
103         public IOException JavaDoc getIOException() {
104             return this.ioe;
105         }
106     }
107 }
108
Popular Tags