1 /* |
1 /* |
2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
29 ******************************************************************************* |
29 ******************************************************************************* |
30 */ |
30 */ |
31 package sun.util.locale; |
31 package sun.util.locale; |
32 |
32 |
33 public class StringTokenIterator { |
33 public class StringTokenIterator { |
34 private String _text; |
34 private String text; |
35 private String _dlms; |
35 private String dlms; // null if a single char delimiter |
|
36 private char delimiterChar; // delimiter if a single char delimiter |
36 |
37 |
37 private String _token; |
38 private String token; |
38 private int _start; |
39 private int start; |
39 private int _end; |
40 private int end; |
40 private boolean _done; |
41 private boolean done; |
41 |
42 |
42 public StringTokenIterator(String text, String dlms) { |
43 public StringTokenIterator(String text, String dlms) { |
43 _text = text; |
44 this.text = text; |
44 _dlms = dlms; |
45 if (dlms.length() == 1) { |
|
46 delimiterChar = dlms.charAt(0); |
|
47 } else { |
|
48 this.dlms = dlms; |
|
49 } |
45 setStart(0); |
50 setStart(0); |
46 } |
51 } |
47 |
52 |
48 public String first() { |
53 public String first() { |
49 setStart(0); |
54 setStart(0); |
50 return _token; |
55 return token; |
51 } |
56 } |
52 |
57 |
53 public String current() { |
58 public String current() { |
54 return _token; |
59 return token; |
55 } |
60 } |
56 |
61 |
57 public int currentStart() { |
62 public int currentStart() { |
58 return _start; |
63 return start; |
59 } |
64 } |
60 |
65 |
61 public int currentEnd() { |
66 public int currentEnd() { |
62 return _end; |
67 return end; |
63 } |
68 } |
64 |
69 |
65 public boolean isDone() { |
70 public boolean isDone() { |
66 return _done; |
71 return done; |
67 } |
72 } |
68 |
73 |
69 public String next() { |
74 public String next() { |
70 if (hasNext()) { |
75 if (hasNext()) { |
71 _start = _end + 1; |
76 start = end + 1; |
72 _end = nextDelimiter(_start); |
77 end = nextDelimiter(start); |
73 _token = _text.substring(_start, _end); |
78 token = text.substring(start, end); |
74 } else { |
79 } else { |
75 _start = _end; |
80 start = end; |
76 _token = null; |
81 token = null; |
77 _done = true; |
82 done = true; |
78 } |
83 } |
79 return _token; |
84 return token; |
80 } |
85 } |
81 |
86 |
82 public boolean hasNext() { |
87 public boolean hasNext() { |
83 return (_end < _text.length()); |
88 return (end < text.length()); |
84 } |
89 } |
85 |
90 |
86 public StringTokenIterator setStart(int offset) { |
91 public StringTokenIterator setStart(int offset) { |
87 if (offset > _text.length()) { |
92 if (offset > text.length()) { |
88 throw new IndexOutOfBoundsException(); |
93 throw new IndexOutOfBoundsException(); |
89 } |
94 } |
90 _start = offset; |
95 start = offset; |
91 _end = nextDelimiter(_start); |
96 end = nextDelimiter(start); |
92 _token = _text.substring(_start, _end); |
97 token = text.substring(start, end); |
93 _done = false; |
98 done = false; |
94 return this; |
99 return this; |
95 } |
100 } |
96 |
101 |
97 public StringTokenIterator setText(String text) { |
102 public StringTokenIterator setText(String text) { |
98 _text = text; |
103 this.text = text; |
99 setStart(0); |
104 setStart(0); |
100 return this; |
105 return this; |
101 } |
106 } |
102 |
107 |
103 private int nextDelimiter(int start) { |
108 private int nextDelimiter(int start) { |
104 int idx = start; |
109 int textlen = this.text.length(); |
105 outer: while (idx < _text.length()) { |
110 if (dlms == null) { |
106 char c = _text.charAt(idx); |
111 for (int idx = start; idx < textlen; idx++) { |
107 for (int i = 0; i < _dlms.length(); i++) { |
112 if (text.charAt(idx) == delimiterChar) { |
108 if (c == _dlms.charAt(i)) { |
113 return idx; |
109 break outer; |
|
110 } |
114 } |
111 } |
115 } |
112 idx++; |
116 } else { |
|
117 int dlmslen = dlms.length(); |
|
118 for (int idx = start; idx < textlen; idx++) { |
|
119 char c = text.charAt(idx); |
|
120 for (int i = 0; i < dlmslen; i++) { |
|
121 if (c == dlms.charAt(i)) { |
|
122 return idx; |
|
123 } |
|
124 } |
|
125 } |
113 } |
126 } |
114 return idx; |
127 return textlen; |
115 } |
128 } |
116 } |
129 } |
117 |
|