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 com.ibm.icu4jni.text.NativeBreakIterator;
21
22/*
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).
27 */
28class RuleBasedBreakIterator extends BreakIterator {
29
30    /*
31     * Wrapping constructor.
32     */
33    RuleBasedBreakIterator(NativeBreakIterator iterator) {
34        super(iterator);
35    }
36
37    /*
38     * (non-Javadoc)
39     *
40     * @see java.text.BreakIterator#current()
41     */
42    @Override
43    public int current() {
44        return wrapped.current();
45    }
46
47    /*
48     * (non-Javadoc)
49     *
50     * @see java.text.BreakIterator#first()
51     */
52    @Override
53    public int first() {
54        return wrapped.first();
55    }
56
57    /*
58     * (non-Javadoc)
59     *
60     * @see java.text.BreakIterator#following(int)
61     */
62    @Override
63    public int following(int offset) {
64        validateOffset(offset);
65        return wrapped.following(offset);
66    }
67
68    /*
69     * check the offset, throw exception if it is invalid
70     */
71    private void validateOffset(int offset) {
72        CharacterIterator it = wrapped.getText();
73        if (offset < it.getBeginIndex() || offset >= it.getEndIndex()) {
74            throw new IllegalArgumentException();
75        }
76    }
77
78    /*
79     * (non-Javadoc)
80     *
81     * @see java.text.BreakIterator#getText()
82     */
83    @Override
84    public CharacterIterator getText() {
85        return wrapped.getText();
86    }
87
88    /*
89     * (non-Javadoc)
90     *
91     * @see java.text.BreakIterator#last()
92     */
93    @Override
94    public int last() {
95        return wrapped.last();
96    }
97
98    /*
99     * (non-Javadoc)
100     *
101     * @see java.text.BreakIterator#next()
102     */
103    @Override
104    public int next() {
105        return wrapped.next();
106    }
107
108    /*
109     * (non-Javadoc)
110     *
111     * @see java.text.BreakIterator#next(int)
112     */
113    @Override
114    public int next(int n) {
115        return wrapped.next(n);
116    }
117
118    /*
119     * (non-Javadoc)
120     *
121     * @see java.text.BreakIterator#previous()
122     */
123    @Override
124    public int previous() {
125        return wrapped.previous();
126    }
127
128    /*
129     * (non-Javadoc)
130     *
131     * @see java.text.BreakIterator#setText(java.text.CharacterIterator)
132     */
133    @Override
134    public void setText(CharacterIterator newText) {
135        // call a method to check if null pointer
136        newText.current();
137        wrapped.setText(newText);
138    }
139
140    /*
141     * (non-Javadoc)
142     *
143     * @see java.text.BreakIterator#isBoundary(int)
144     */
145    @Override
146    public boolean isBoundary(int offset) {
147        validateOffset(offset);
148        return wrapped.isBoundary(offset);
149    }
150
151    /*
152     * (non-Javadoc)
153     *
154     * @see java.text.BreakIterator#preceding(int)
155     */
156    @Override
157    public int preceding(int offset) {
158        validateOffset(offset);
159        return wrapped.preceding(offset);
160    }
161
162    /*
163     * (non-Javadoc)
164     *
165     * @see java.lang.Object#equals(java.lang.Object)
166     */
167    @Override
168    public boolean equals(Object o) {
169        if (!(o instanceof RuleBasedBreakIterator)) {
170            return false;
171        }
172        return wrapped.equals(((RuleBasedBreakIterator) o).wrapped);
173    }
174
175    /*
176     * (non-Javadoc)
177     *
178     * @see java.lang.Object#toString()
179     */
180    @Override
181    public String toString() {
182        return wrapped.toString();
183    }
184
185    /*
186     * (non-Javadoc)
187     *
188     * @see java.lang.Object#hashCode()
189     */
190    @Override
191    public int hashCode() {
192        return wrapped.hashCode();
193    }
194
195    /*
196     * (non-Javadoc)
197     *
198     * @see java.lang.Object#clone()
199     */
200    @Override
201    public Object clone() {
202        RuleBasedBreakIterator cloned = (RuleBasedBreakIterator) super.clone();
203        cloned.wrapped = (NativeBreakIterator) wrapped.clone();
204        return cloned;
205    }
206}
207