2004-09-22 Kelley Cook <kcook@gcc.gnu.org>
[official-gcc.git] / libjava / java / text / AttributedStringIterator.java
blobca20801141c76bbe9277b69d8f1c7fb33d39c5b4
1 /* AttributedStringIterator.java -- Class to iterate over AttributedString
2 Copyright (C) 1998, 1999, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.text;
41 import java.util.HashMap;
42 import java.util.HashSet;
43 import java.util.Iterator;
44 import java.util.Map;
45 import java.util.Set;
47 /**
48 * This class implements the AttributedCharacterIterator interface. It
49 * is used by AttributedString.getIterator().
51 * @version 0.0
53 * @author Aaron M. Renn (arenn@urbanophile.com)
55 class AttributedStringIterator implements AttributedCharacterIterator
58 /*************************************************************************/
60 /**
61 * Instance Variables
64 /**
65 * The character iterator containing the text
67 private CharacterIterator ci;
69 /**
70 * The list of attributes and ranges
72 private AttributedString.AttributeRange[] attribs;
74 /**
75 * The list of attributes that the user is interested in. We may,
76 * at our option, not return any other attributes.
78 private AttributedCharacterIterator.Attribute[] restricts;
80 /*************************************************************************/
83 * Constructors
86 AttributedStringIterator(StringCharacterIterator sci,
87 AttributedString.AttributeRange[] attribs,
88 int begin_index, int end_index,
89 AttributedCharacterIterator.Attribute[] restricts)
91 this.ci = new StringCharacterIterator(sci, begin_index, end_index);
92 this.attribs = attribs;
93 this.restricts = restricts;
96 /*************************************************************************/
99 * Instance Methods
102 // First we have a bunch of stupid redirects. If StringCharacterIterator
103 // weren't final, I just would have extended that for this class. Alas, no.
105 public Object
106 clone()
108 return(ci.clone());
111 public char
112 current()
114 return(ci.current());
117 public char
118 next()
120 return(ci.next());
123 public char
124 previous()
126 return(ci.previous());
129 public char
130 first()
132 return(ci.first());
135 public char
136 last()
138 return(ci.last());
141 public int
142 getIndex()
144 return(ci.getIndex());
147 public char
148 setIndex(int index)
150 return(ci.setIndex(index));
153 public int
154 getBeginIndex()
156 return(ci.getBeginIndex());
159 public int
160 getEndIndex()
162 return(ci.getEndIndex());
166 * Here is where the AttributedCharacterIterator methods start.
169 /*************************************************************************/
172 * Returns a list of all the attribute keys that are defined anywhere
173 * on this string.
175 public Set
176 getAllAttributeKeys()
178 HashSet s = new HashSet();
179 if (attribs == null)
180 return(s);
182 for (int i = 0; i < attribs.length; i++)
184 if (attribs[i].begin_index > getEndIndex()
185 || attribs[i].end_index <= getBeginIndex())
186 continue;
188 Set key_set = attribs[i].attribs.keySet();
189 Iterator iter = key_set.iterator();
190 while (iter.hasNext())
192 s.add(iter.next());
196 return(s);
199 /*************************************************************************/
202 * Various methods that determine how far the run extends for various
203 * attribute combinations.
206 public int
207 getRunLimit()
209 return(getRunLimit(getAttributes().keySet()));
212 public int
213 getRunLimit(AttributedCharacterIterator.Attribute attrib)
215 HashSet s = new HashSet();
216 s.add(attrib);
218 return(getRunLimit(s));
221 public synchronized int
222 getRunLimit(Set attribute_set)
224 boolean hit = false;
225 int runLimit = ci.getEndIndex ();
226 int pos = ci.getIndex ();
228 for (int i = 0; i < attribs.length; ++i)
230 if (pos >= attribs[i].begin_index &&
231 pos <= attribs[i].end_index)
233 Iterator iter = attribute_set.iterator();
234 while(iter.hasNext())
235 if (attribs[i].attribs.containsKey(iter.next()))
237 hit = true;
238 runLimit = Math.min(runLimit, attribs[i].end_index);
242 if (hit)
243 return runLimit;
244 else
245 return -1;
248 /*************************************************************************/
251 * Various methods that determine where the run begins for various
252 * attribute combinations.
255 public int
256 getRunStart()
258 return(getRunStart(getAttributes().keySet()));
261 public int
262 getRunStart(AttributedCharacterIterator.Attribute attrib)
264 HashSet s = new HashSet();
265 s.add(attrib);
267 return(getRunStart(s));
270 public int
271 getRunStart(Set attribute_set)
273 boolean hit = false;
274 int runBegin = 0;
275 int pos = ci.getIndex ();
277 for (int i = 0; i < attribs.length; ++i)
279 if (pos >= attribs[i].begin_index &&
280 pos <= attribs[i].end_index)
282 Iterator iter = attribute_set.iterator();
283 while(iter.hasNext())
284 if (attribs[i].attribs.containsKey(iter.next()))
286 hit = true;
287 runBegin = Math.max(runBegin, attribs[i].begin_index);
291 if (hit)
292 return runBegin;
293 else
294 return -1;
297 /*************************************************************************/
299 public Object
300 getAttribute(AttributedCharacterIterator.Attribute attrib)
302 if (attribs == null)
303 return(null);
305 for (int i = 0; i < attribs.length; i++)
307 Set key_set = attribs[i].attribs.keySet();
308 Iterator iter = key_set.iterator();
309 while (iter.hasNext())
311 Object obj = iter.next();
313 // Check for attribute match and range match
314 if (obj.equals(attrib))
315 if ((ci.getIndex() >= attribs[i].begin_index) &&
316 (ci.getIndex() < attribs[i].end_index))
317 return(attribs[i].attribs.get(obj));
321 return(null);
324 /*************************************************************************/
327 * Return a list of all the attributes and values defined for this
328 * character
330 public Map
331 getAttributes()
333 HashMap m = new HashMap();
334 if (attribs == null)
335 return(m);
337 for (int i = 0; i < attribs.length; i++)
339 if ((ci.getIndex() >= attribs[i].begin_index) &&
340 (ci.getIndex() < attribs[i].end_index))
341 m.putAll(attribs[i].attribs);
344 return(m);
347 } // class AttributedStringIterator