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