KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > text > ReplaceableContextIterator


1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 2004-2005, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 * file name: ReplaceableContextIterator.java
9 * encoding: US-ASCII
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 2005feb04
14 * created by: Markus W. Scherer
15 *
16 * Implementation of UCaseProps.ContextIterator, iterates over a Replaceable.
17 * Java port of casetrn.cpp/utrans_rep_caseContextIterator().
18 */

19
20 package com.ibm.icu.text;
21
22 import com.ibm.icu.impl.UCaseProps;
23 import com.ibm.icu.impl.UCharacterProperty;
24
25 import com.ibm.icu.util.ULocale;
26
27 /**
28  * Implementation of UCaseProps.ContextIterator, iterates over a Replaceable.
29  * See casetrn.cpp/utrans_rep_caseContextIterator().
30  * See also UCharacter.StringContextIterator.
31  * @internal
32  */

33 class ReplaceableContextIterator implements UCaseProps.ContextIterator {
34     /**
35      * Constructor.
36      * @param rep Replaceable to iterate over.
37      */

38     ReplaceableContextIterator() {
39         this.rep=null;
40         limit=cpStart=cpLimit=index=contextStart=contextLimit=0;
41         dir=0;
42         reachedLimit=false;
43     }
44
45     /**
46      * Set the text for iteration.
47      * @param rep Iteration text.
48      */

49     public void setText(Replaceable rep) {
50         this.rep=rep;
51         limit=contextLimit=rep.length();
52         cpStart=cpLimit=index=contextStart=0;
53         dir=0;
54         reachedLimit=false;
55     }
56
57     /**
58      * Set the index where nextCaseMapCP() is to start iterating.
59      * @param index Iteration start index for nextCaseMapCP().
60      */

61     public void setIndex(int index) {
62         cpStart=cpLimit=index;
63         this.index=0;
64         dir=0;
65         reachedLimit=false;
66     }
67
68     /**
69      * Get the index of where the code point currently being case-mapped starts.
70      * @return The start index of the current code point.
71      */

72     public int getCaseMapCPStart() {
73         return cpStart;
74     }
75
76     /**
77      * Set the iteration limit for nextCaseMapCP() to an index within the string.
78      * If the limit parameter is negative or past the string, then the
79      * string length is restored as the iteration limit.
80      *
81      * @param lim The iteration limit.
82      */

83     public void setLimit(int lim) {
84         if(0<=lim && lim<=rep.length()) {
85             limit=lim;
86         } else {
87             limit=rep.length();
88         }
89         reachedLimit=false;
90     }
91
92     /**
93      * Set the start and limit indexes for context iteration with next().
94      * @param contextStart Start of context for next().
95      * @param contextLimit Limit of context for next().
96      */

97     public void setContextLimits(int contextStart, int contextLimit) {
98         if(contextStart<0) {
99             this.contextStart=0;
100         } else if(contextStart<=rep.length()) {
101             this.contextStart=contextStart;
102         } else {
103             this.contextStart=rep.length();
104         }
105         if(contextLimit<this.contextStart) {
106             this.contextLimit=this.contextStart;
107         } else if(contextLimit<=rep.length()) {
108             this.contextLimit=contextLimit;
109         } else {
110             this.contextLimit=rep.length();
111         }
112         reachedLimit=false;
113     }
114
115     /**
116      * Iterate forward through the string to fetch the next code point
117      * to be case-mapped, and set the context indexes for it.
118      *
119      * @return The next code point to be case-mapped, or <0 when the iteration is done.
120      */

121     public int nextCaseMapCP() {
122         int c;
123         if(cpLimit<limit) {
124             cpStart=cpLimit;
125             c=rep.char32At(cpLimit);
126             cpLimit+=UTF16.getCharCount(c);
127             return c;
128         } else {
129             return -1;
130         }
131     }
132
133     /**
134      * Replace the current code point by its case mapping,
135      * and update the indexes.
136      *
137      * @param text Replacement text.
138      * @return The delta for the change of the text length.
139      */

140     public int replace(String JavaDoc text) {
141         int delta=text.length()-(cpLimit-cpStart);
142         rep.replace(cpStart, cpLimit, text);
143         cpLimit+=delta;
144         limit+=delta;
145         contextLimit+=delta;
146         return delta;
147     }
148
149     /**
150      * Did forward context iteration with next() reach the iteration limit?
151      * @return Boolean value.
152      */

153     public boolean didReachLimit() {
154         return reachedLimit;
155     }
156
157     // implement UCaseProps.ContextIterator
158
public void reset(int dir) {
159         if(dir>0) {
160             /* reset for forward iteration */
161             this.dir=1;
162             index=cpLimit;
163         } else if(dir<0) {
164             /* reset for backward iteration */
165             this.dir=-1;
166             index=cpStart;
167         } else {
168             // not a valid direction
169
this.dir=0;
170             index=0;
171         }
172         reachedLimit=false;
173     }
174
175     public int next() {
176         int c;
177
178         if(dir>0) {
179             if(index<contextLimit) {
180                 c=rep.char32At(index);
181                 index+=UTF16.getCharCount(c);
182                 return c;
183             } else {
184                 // forward context iteration reached the limit
185
reachedLimit=true;
186             }
187         } else if(dir<0 && index>contextStart) {
188             c=rep.char32At(index-1);
189             index-=UTF16.getCharCount(c);
190             return c;
191         }
192         return -1;
193     }
194
195     // variables
196
protected Replaceable rep;
197     protected int index, limit, cpStart, cpLimit, contextStart, contextLimit;
198     protected int dir; // 0=initial state >0=forward <0=backward
199
protected boolean reachedLimit;
200 }
201
Popular Tags