1/* 2 ******************************************************************************* 3 * Copyright (C) 2009, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ******************************************************************************* 6 */ 7package com.ibm.icu.impl.locale; 8 9public class StringTokenIterator { 10 private String _text; 11 private String _dlms; 12 13 private String _token; 14 private int _start; 15 private int _end; 16 private boolean _done; 17 18 public StringTokenIterator(String text, String dlms) { 19 _text = text; 20 _dlms = dlms; 21 setStart(0); 22 } 23 24 public String first() { 25 setStart(0); 26 return _token; 27 } 28 29 public String current() { 30 return _token; 31 } 32 33 public int currentStart() { 34 return _start; 35 } 36 37 public int currentEnd() { 38 return _end; 39 } 40 41 public boolean isDone() { 42 return _done; 43 } 44 45 public String next() { 46 if (hasNext()) { 47 _start = _end + 1; 48 _end = nextDelimiter(_start); 49 _token = _text.substring(_start, _end); 50 } else { 51 _start = _end; 52 _token = null; 53 _done = true; 54 } 55 return _token; 56 } 57 58 public boolean hasNext() { 59 return (_end < _text.length()); 60 } 61 62 public StringTokenIterator setStart(int offset) { 63 if (offset > _text.length()) { 64 throw new IndexOutOfBoundsException(); 65 } 66 _start = offset; 67 _end = nextDelimiter(_start); 68 _token = _text.substring(_start, _end); 69 _done = false; 70 return this; 71 } 72 73 public StringTokenIterator setText(String text) { 74 _text = text; 75 setStart(0); 76 return this; 77 } 78 79 private int nextDelimiter(int start) { 80 int idx = start; 81 outer: while (idx < _text.length()) { 82 char c = _text.charAt(idx); 83 for (int i = 0; i < _dlms.length(); i++) { 84 if (c == _dlms.charAt(i)) { 85 break outer; 86 } 87 } 88 idx++; 89 } 90 return idx; 91 } 92} 93 94