01: package uk.co.jezuk.mango.iterators;
02:
03: /**
04: *
05: * @author jez@jezuk.co.uk
06: */
07: public class StringIterator implements java.util.Iterator {
08: public StringIterator(String s) {
09: s_ = s;
10: if ((s_ != null) && (s_.length() > 0))
11: pos_ = 0;
12: } // StringIterator
13:
14: public boolean hasNext() {
15: return (pos_ != -1);
16: } // hasNext
17:
18: public Object next() {
19: String c = s_.substring(pos_, pos_ + 1);
20: if (++pos_ == s_.length())
21: pos_ = -1;
22: return c;
23: } // next
24:
25: public void remove() {
26: throw new UnsupportedOperationException(
27: "uk.co.jezuk.mango.StringIterator does not support the remove method. In fact it's probably a logic error that you called it at all. Strings are immutable");
28: } // remove
29:
30: private String s_;
31: private int pos_ = -1;
32: } // StringIterator
|