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