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        checkOffset(offset);
44        return wrapped.following(offset);
45    }
46
47    private void checkOffset(int offset) {
48        if (!wrapped.hasText()) {
49            throw new IllegalArgumentException("BreakIterator has no text");
50        }
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        if (newText == null) {
80            throw new NullPointerException("newText == null");
81        }
82        newText.current();
83        wrapped.setText(newText);
84    }
85
86    @Override public boolean isBoundary(int offset) {
87        checkOffset(offset);
88        return wrapped.isBoundary(offset);
89    }
90
91    @Override public int preceding(int offset) {
92        checkOffset(offset);
93        return wrapped.preceding(offset);
94    }
95
96    @Override public boolean equals(Object o) {
97        if (!(o instanceof RuleBasedBreakIterator)) {
98            return false;
99        }
100        return wrapped.equals(((RuleBasedBreakIterator) o).wrapped);
101    }
102
103    @Override public String toString() {
104        return wrapped.toString();
105    }
106
107    @Override public int hashCode() {
108        return wrapped.hashCode();
109    }
110
111    @Override public Object clone() {
112        RuleBasedBreakIterator cloned = (RuleBasedBreakIterator) super.clone();
113        cloned.wrapped = (NativeBreakIterator) wrapped.clone();
114        return cloned;
115    }
116}
117