1/*
2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/*
27 *******************************************************************************
28 * (C) Copyright IBM Corp. 1996-2005 - All Rights Reserved                     *
29 *                                                                             *
30 * The original version of this source code and documentation is copyrighted   *
31 * and owned by IBM, These materials are provided under terms of a License     *
32 * Agreement between IBM and Sun. This technology is protected by multiple     *
33 * US and International patents. This notice and attribution to IBM may not    *
34 * to removed.                                                                 *
35 *******************************************************************************
36 */
37
38package sun.text.normalizer;
39
40/**
41 * DLF docs must define behavior when Replaceable is mutated underneath
42 * the iterator.
43 *
44 * This and ICUCharacterIterator share some code, maybe they should share
45 * an implementation, or the common state and implementation should be
46 * moved up into UCharacterIterator.
47 *
48 * What are first, last, and getBeginIndex doing here?!?!?!
49 */
50class ReplaceableUCharacterIterator extends UCharacterIterator {
51
52    // public constructor ------------------------------------------------------
53
54    /**
55     * Public constructor
56     * @param str text which the iterator will be based on
57     */
58    public ReplaceableUCharacterIterator(String str){
59        if(str==null){
60            throw new IllegalArgumentException();
61        }
62        this.replaceable  = new ReplaceableString(str);
63        this.currentIndex = 0;
64    }
65
66    /**
67     * Public constructor
68     * @param buf buffer of text on which the iterator will be based
69     */
70    public ReplaceableUCharacterIterator(StringBuffer buf){
71        if(buf==null){
72            throw new IllegalArgumentException();
73        }
74        this.replaceable  = new ReplaceableString(buf);
75        this.currentIndex = 0;
76    }
77
78    // public methods ----------------------------------------------------------
79
80    /**
81     * Creates a copy of this iterator, does not clone the underlying
82     * <code>Replaceable</code>object
83     * @return copy of this iterator
84     */
85    public Object clone(){
86        try {
87          return super.clone();
88        } catch (CloneNotSupportedException e) {
89            return null; // never invoked
90        }
91    }
92
93    /**
94     * Returns the current UTF16 character.
95     * @return current UTF16 character
96     */
97    public int current(){
98        if (currentIndex < replaceable.length()) {
99            return replaceable.charAt(currentIndex);
100        }
101        return DONE;
102    }
103
104    /**
105     * Returns the length of the text
106     * @return length of the text
107     */
108    public int getLength(){
109        return replaceable.length();
110    }
111
112    /**
113     * Gets the current currentIndex in text.
114     * @return current currentIndex in text.
115     */
116    public int getIndex(){
117        return currentIndex;
118    }
119
120    /**
121     * Returns next UTF16 character and increments the iterator's currentIndex by 1.
122     * If the resulting currentIndex is greater or equal to the text length, the
123     * currentIndex is reset to the text length and a value of DONECODEPOINT is
124     * returned.
125     * @return next UTF16 character in text or DONE if the new currentIndex is off the
126     *         end of the text range.
127     */
128    public int next(){
129        if (currentIndex < replaceable.length()) {
130            return replaceable.charAt(currentIndex++);
131        }
132        return DONE;
133    }
134
135
136    /**
137     * Returns previous UTF16 character and decrements the iterator's currentIndex by
138     * 1.
139     * If the resulting currentIndex is less than 0, the currentIndex is reset to 0 and a
140     * value of DONECODEPOINT is returned.
141     * @return next UTF16 character in text or DONE if the new currentIndex is off the
142     *         start of the text range.
143     */
144    public int previous(){
145        if (currentIndex > 0) {
146            return replaceable.charAt(--currentIndex);
147        }
148        return DONE;
149    }
150
151    /**
152     * Sets the currentIndex to the specified currentIndex in the text and returns that
153     * single UTF16 character at currentIndex.
154     * This assumes the text is stored as 16-bit code units.
155     * @param currentIndex the currentIndex within the text.
156     * @exception IllegalArgumentException is thrown if an invalid currentIndex is
157     *            supplied. i.e. currentIndex is out of bounds.
158     */
159    public void setIndex(int currentIndex) {
160        if (currentIndex < 0 || currentIndex > replaceable.length()) {
161            throw new IllegalArgumentException();
162        }
163        this.currentIndex = currentIndex;
164    }
165
166    public int getText(char[] fillIn, int offset){
167        int length = replaceable.length();
168        if(offset < 0 || offset + length > fillIn.length){
169            throw new IndexOutOfBoundsException(Integer.toString(length));
170        }
171        replaceable.getChars(0,length,fillIn,offset);
172        return length;
173    }
174
175    // private data members ----------------------------------------------------
176
177    /**
178     * Replaceable object
179     */
180    private Replaceable replaceable;
181    /**
182     * Current currentIndex
183     */
184    private int currentIndex;
185
186}
187