1 package com.aragost.javahg.internals; 2 3 import java.io.IOException; 4 import java.util.Iterator; 5 import java.util.NoSuchElementException; 6 7 public class LineIterator implements Iterator<String>, Iterable<String> { 8 9 private boolean atEnd = false; 10 11 private HgInputStream stream; 12 13 public LineIterator(HgInputStream stream) { 14 this.stream = stream; 15 } 16 17 public Iterator<String> iterator() { 18 return this; 19 } 20 21 public boolean hasNext() { 22 if (!this.atEnd) { 23 try { 24 this.atEnd = (this.stream.isEof()); 25 } catch (IOException e) { 26 throw new RuntimeIOException(e); 27 } 28 } 29 return !this.atEnd; 30 } 31 32 public String next() { 33 if (hasNext()) { 34 try { 35 return this.stream.textUpTo('\n'); 36 } catch (IOException e) { 37 throw new RuntimeIOException(e); 38 } 39 } else { 40 throw new NoSuchElementException(); 41 } 42 } 43 44 public void remove() { 45 throw new UnsupportedOperationException(); 46 } 47 48 }