* toplev.c (lang_independent_f_options): Remove
[official-gcc.git] / libjava / java / text / StringCharacterIterator.java
blob487b09c8220109ad74062dd1057684f956d8e36c
1 // StringCharacterIterator.java - Iterate over string of Unicode characters.
3 /* Copyright (C) 1999, 2000 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 package java.text;
13 /**
14 * @author Tom Tromey <tromey@cygnus.com>
15 * @date February 22, 1999
17 /* Written using "Java Class Libraries", 2nd edition, plus online
18 * API docs for JDK 1.2 beta from http://www.javasoft.com.
19 * Status: Believed complete and correct to 1.1.
22 public final class StringCharacterIterator implements CharacterIterator
24 public Object clone ()
26 return (Object) new StringCharacterIterator (text, begin, end, pos);
29 public char current ()
31 // This follows JDK 1.2 semantics and not 1.1 semantics.
32 // In 1.1 we would throw an exception if begin==end.
33 return (pos < end) ? text.charAt(pos) : CharacterIterator.DONE;
36 public boolean equals (Object obj)
38 if (! (obj instanceof StringCharacterIterator))
39 return false;
40 StringCharacterIterator sci = (StringCharacterIterator) obj;
41 // The spec says "the same text". We take this to mean equals,
42 // not ==.
43 return (pos == sci.pos
44 && begin == sci.begin
45 && end == sci.end
46 && text.equals(sci.text));
49 public char first ()
51 pos = begin;
52 return current ();
55 public int getBeginIndex ()
57 return begin;
60 public int getEndIndex ()
62 return end;
65 public int getIndex ()
67 return pos;
70 public int hashCode ()
72 // FIXME: this is a terrible hash code. Find a better one.
73 return text.hashCode() + pos + begin + end;
76 public char last ()
78 pos = end;
79 return current ();
82 public char next ()
84 if (pos == end)
85 return CharacterIterator.DONE;
86 ++pos;
87 return current ();
90 public char previous ()
92 if (pos == begin)
93 return CharacterIterator.DONE;
94 --pos;
95 return current ();
98 public char setIndex (int idx)
100 // In 1.1 we would throw an error if `idx == end'.
101 if (idx < begin || idx > end)
102 throw new IllegalArgumentException ();
103 pos = idx;
104 return current ();
107 public StringCharacterIterator (String text)
109 this (text, 0, text.length(), 0);
112 public StringCharacterIterator (String text, int pos)
114 this (text, 0, text.length(), pos);
117 public StringCharacterIterator (String text, int begin, int end, int pos)
119 if (begin < 0 || begin > end || end > text.length()
120 // In 1.1 we would also throw if `pos == end'.
121 || pos < begin || pos > end)
122 throw new IllegalArgumentException ();
124 this.text = text;
125 this.begin = begin;
126 this.end = end;
127 this.pos = pos;
130 // The online 1.2 docs say that this is "package visible" in the
131 // method description, but they also say it is public. We choose
132 // the latter for compatibility with the actual implementation.
133 public void setText (String text)
135 this.text = text;
136 this.begin = 0;
137 this.end = text.length ();
138 this.pos = 0;
141 // String to iterate over.
142 private String text;
143 // Current position.
144 private int pos;
145 // Start position in string.
146 private int begin;
147 // End position in string.
148 private int end;