]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/classes/core/src/com/ibm/icu/impl/ReplaceableUCharacterIterator.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / core / src / com / ibm / icu / impl / ReplaceableUCharacterIterator.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.impl;
8
9 import com.ibm.icu.text.Replaceable;
10 import com.ibm.icu.text.ReplaceableString;
11 import com.ibm.icu.text.UCharacterIterator;
12 import com.ibm.icu.text.UTF16;
13
14 /**
15  * DLF docs must define behavior when Replaceable is mutated underneath
16  * the iterator.
17  *
18  * This and ICUCharacterIterator share some code, maybe they should share
19  * an implementation, or the common state and implementation should be
20  * moved up into UCharacterIterator.
21  *
22  * What are first, last, and getBeginIndex doing here?!?!?!
23  */
24 public class ReplaceableUCharacterIterator extends UCharacterIterator {
25
26     // public constructor ------------------------------------------------------
27     
28     /**
29      * Public constructor
30      * @param replaceable text which the iterator will be based on
31      */
32     public ReplaceableUCharacterIterator(Replaceable replaceable){
33         if(replaceable==null){
34             throw new IllegalArgumentException();
35         }
36         this.replaceable  = replaceable;
37         this.currentIndex = 0;
38     }
39     
40     /**
41      * Public constructor
42      * @param str text which the iterator will be based on
43      */
44     public ReplaceableUCharacterIterator(String str){
45         if(str==null){
46             throw new IllegalArgumentException();
47         }
48         this.replaceable  = new ReplaceableString(str);
49         this.currentIndex = 0;
50     }
51     
52     /**
53      * Public constructor
54      * @param buf buffer of text on which the iterator will be based
55      */
56     public ReplaceableUCharacterIterator(StringBuffer buf){
57         if(buf==null){
58             throw new IllegalArgumentException();
59         }
60         this.replaceable  = new ReplaceableString(buf);
61         this.currentIndex = 0;
62     }
63     
64     // public methods ----------------------------------------------------------
65     
66     /**
67      * Creates a copy of this iterator, does not clone the underlying 
68      * <code>Replaceable</code>object
69      * @return copy of this iterator
70      */
71     public Object clone(){
72         try {
73           return super.clone();
74         } catch (CloneNotSupportedException e) {
75             return null; // never invoked
76         }
77     }
78     
79     /**
80      * Returns the current UTF16 character.
81      * @return current UTF16 character
82      */
83     public int current(){
84         if (currentIndex < replaceable.length()) {
85             return replaceable.charAt(currentIndex);
86         }
87         return DONE;
88     }
89     
90     /**
91      * Returns the current codepoint
92      * @return current codepoint
93      */
94     public int currentCodePoint(){
95         // cannot use charAt due to it different 
96         // behaviour when index is pointing at a
97         // trail surrogate, check for surrogates
98          
99         int ch = current();
100         if(UTF16.isLeadSurrogate((char)ch)){
101             // advance the index to get the next code point
102             next();
103             // due to post increment semantics current() after next() 
104             // actually returns the next char which is what we want
105             int ch2 = current();
106             // current should never change the current index so back off
107             previous();
108             
109             if(UTF16.isTrailSurrogate((char)ch2)){
110                 // we found a surrogate pair
111                 return UCharacterProperty.getRawSupplementary(
112                                                          (char)ch,(char)ch2
113                                                              );
114             }
115         }
116         return ch;
117     }
118     
119     /**
120      * Returns the length of the text
121      * @return length of the text
122      */
123     public int getLength(){
124         return replaceable.length();
125     }
126     
127     /**
128      * Gets the current currentIndex in text.
129      * @return current currentIndex in text.
130      */
131     public int getIndex(){
132         return currentIndex;
133     }
134         
135     /**
136      * Returns next UTF16 character and increments the iterator's currentIndex by 1. 
137      * If the resulting currentIndex is greater or equal to the text length, the 
138      * currentIndex is reset to the text length and a value of DONECODEPOINT is 
139      * returned. 
140      * @return next UTF16 character in text or DONE if the new currentIndex is off the 
141      *         end of the text range.
142      */
143     public int next(){
144         if (currentIndex < replaceable.length()) {
145             return replaceable.charAt(currentIndex++);
146         }
147         return DONE;
148     }
149     
150                 
151     /**
152      * Returns previous UTF16 character and decrements the iterator's currentIndex by 
153      * 1. 
154      * If the resulting currentIndex is less than 0, the currentIndex is reset to 0 and a 
155      * value of DONECODEPOINT is returned. 
156      * @return next UTF16 character in text or DONE if the new currentIndex is off the 
157      *         start of the text range.
158      */
159     public int previous(){
160         if (currentIndex > 0) {
161             return replaceable.charAt(--currentIndex);
162         }
163         return DONE;
164     }
165
166     /**
167      * <p>Sets the currentIndex to the specified currentIndex in the text and returns that 
168      * single UTF16 character at currentIndex. 
169      * This assumes the text is stored as 16-bit code units.</p>
170      * @param currentIndex the currentIndex within the text. 
171      * @exception IllegalArgumentException is thrown if an invalid currentIndex is 
172      *            supplied. i.e. currentIndex is out of bounds.
173      * @returns the character at the specified currentIndex or DONE if the specified 
174      *         currentIndex is equal to the end of the text.
175      */
176     public void setIndex(int currentIndex) throws IndexOutOfBoundsException{
177         if (currentIndex < 0 || currentIndex > replaceable.length()) {
178             throw new IndexOutOfBoundsException();
179         }
180         this.currentIndex = currentIndex;
181     }
182     
183     public int getText(char[] fillIn, int offset){
184         int length = replaceable.length();
185         if(offset < 0 || offset + length > fillIn.length){
186             throw new IndexOutOfBoundsException(Integer.toString(length));
187         }
188         replaceable.getChars(0,length,fillIn,offset);
189         return length;
190     }       
191         
192     // private data members ----------------------------------------------------
193     
194     /**
195      * Replacable object
196      */
197     private Replaceable replaceable;
198     /**
199      * Current currentIndex
200      */
201     private int currentIndex;
202
203 }