2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 import com.ibm.icu4jni.text.NativeBreakIterator;
23 * Default implementation of BreakIterator. Wraps com.ibm.icu4jni.text.NativeBreakIterator.
24 * We need this because BreakIterator.isBoundary and BreakIterator.preceding are non-abstract,
25 * and we don't have Java implementations of those methods (other than the current ones, which
26 * forward to the wrapped NativeBreakIterator).
28 class RuleBasedBreakIterator extends BreakIterator {
31 * Wrapping constructor.
33 RuleBasedBreakIterator(NativeBreakIterator iterator) {
40 * @see java.text.BreakIterator#current()
43 public int current() {
44 return wrapped.current();
50 * @see java.text.BreakIterator#first()
54 return wrapped.first();
60 * @see java.text.BreakIterator#following(int)
63 public int following(int offset) {
64 validateOffset(offset);
65 return wrapped.following(offset);
69 * check the offset, throw exception if it is invalid
71 private void validateOffset(int offset) {
72 CharacterIterator it = wrapped.getText();
73 if (offset < it.getBeginIndex() || offset >= it.getEndIndex()) {
74 throw new IllegalArgumentException();
81 * @see java.text.BreakIterator#getText()
84 public CharacterIterator getText() {
85 return wrapped.getText();
91 * @see java.text.BreakIterator#last()
95 return wrapped.last();
101 * @see java.text.BreakIterator#next()
105 return wrapped.next();
111 * @see java.text.BreakIterator#next(int)
114 public int next(int n) {
115 return wrapped.next(n);
121 * @see java.text.BreakIterator#previous()
124 public int previous() {
125 return wrapped.previous();
131 * @see java.text.BreakIterator#setText(java.text.CharacterIterator)
134 public void setText(CharacterIterator newText) {
135 // call a method to check if null pointer
137 wrapped.setText(newText);
143 * @see java.text.BreakIterator#isBoundary(int)
146 public boolean isBoundary(int offset) {
147 validateOffset(offset);
148 return wrapped.isBoundary(offset);
154 * @see java.text.BreakIterator#preceding(int)
157 public int preceding(int offset) {
158 validateOffset(offset);
159 return wrapped.preceding(offset);
165 * @see java.lang.Object#equals(java.lang.Object)
168 public boolean equals(Object o) {
169 if (!(o instanceof RuleBasedBreakIterator)) {
172 return wrapped.equals(((RuleBasedBreakIterator) o).wrapped);
178 * @see java.lang.Object#toString()
181 public String toString() {
182 return wrapped.toString();
188 * @see java.lang.Object#hashCode()
191 public int hashCode() {
192 return wrapped.hashCode();
198 * @see java.lang.Object#clone()
201 public Object clone() {
202 RuleBasedBreakIterator cloned = (RuleBasedBreakIterator) super.clone();
203 cloned.wrapped = (NativeBreakIterator) wrapped.clone();