1/*
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
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18package java.text;
19
20import libcore.icu.NativeBreakIterator;
21
22/*
23 * Default implementation of BreakIterator. Wraps libcore.icu.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).
27 */
28class RuleBasedBreakIterator extends BreakIterator {
29
30    RuleBasedBreakIterator(NativeBreakIterator iterator) {
31        super(iterator);
32    }
33
34    @Override public int current() {
35        return wrapped.current();
36    }
37
38    @Override public int first() {
39        return wrapped.first();
40    }
41
42    @Override public int following(int offset) {
43        validateOffset(offset);
44        return wrapped.following(offset);
45    }
46
47    /*
48     * check the offset, throw exception if it is invalid
49     */
50    private void validateOffset(int offset) {
51        CharacterIterator it = wrapped.getText();
52        if (offset < it.getBeginIndex() || offset > it.getEndIndex()) {
53            String message = "Valid range is [" + it.getBeginIndex() + " " + it.getEndIndex() + "]";
54            throw new IllegalArgumentException(message);
55        }
56    }
57
58    @Override public CharacterIterator getText() {
59        return wrapped.getText();
60    }
61
62    @Override public int last() {
63        return wrapped.last();
64    }
65
66    @Override public int next() {
67        return wrapped.next();
68    }
69
70    @Override public int next(int n) {
71        return wrapped.next(n);
72    }
73
74    @Override public int previous() {
75        return wrapped.previous();
76    }
77
78    @Override public void setText(CharacterIterator newText) {
79        // call a method to check if null pointer
80        newText.current();
81        wrapped.setText(newText);
82    }
83
84    @Override public boolean isBoundary(int offset) {
85        validateOffset(offset);
86        return wrapped.isBoundary(offset);
87    }
88
89    @Override public int preceding(int offset) {
90        validateOffset(offset);
91        return wrapped.preceding(offset);
92    }
93
94    @Override public boolean equals(Object o) {
95        if (!(o instanceof RuleBasedBreakIterator)) {
96            return false;
97        }
98        return wrapped.equals(((RuleBasedBreakIterator) o).wrapped);
99    }
100
101    @Override public String toString() {
102        return wrapped.toString();
103    }
104
105    @Override public int hashCode() {
106        return wrapped.hashCode();
107    }
108
109    @Override public Object clone() {
110        RuleBasedBreakIterator cloned = (RuleBasedBreakIterator) super.clone();
111        cloned.wrapped = (NativeBreakIterator) wrapped.clone();
112        return cloned;
113    }
114}
115