source: trunk/gsdl3/src/java/org/greenstone/gsdl3/selfContained/IncrementRange.java@ 4012

Last change on this file since 4012 was 4012, checked in by mdewsnip, 21 years ago

Changed string comparison to use .equals().

  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
1/*
2 * IncrementRange.java
3 * Copyright (C) 2000-2001 Stuart Yeates
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20// the package we're in
21package org.greenstone.gsdl3.selfContained;
22
23// java standard library classes used
24import java.io.IOException;
25import java.io.Reader;
26import java.io.Serializable;
27import java.io.StringReader;
28import java.io.StringWriter;
29import java.io.Writer;
30
31/**
32 * Class IncrementRange increments strings and streams over a limited range of Characters.
33 *
34 *
35 * @author <a href="http://www.cs.waikato.ac.nz/~say1/">stuart yeates</a> (<a href="mailto:[email protected]">[email protected]</a>) at the <a href="http://www.nzdl.org">New Zealand Digital Library</a>
36 * @version $Revision: 4012 $
37 * @see Increment
38 *
39 */
40final public class IncrementRange
41 extends Increment
42 implements Cloneable, Serializable
43{
44 /**
45 * Create an IncrementRange which counts between first and last inclusive
46 *
47 * @param first the first character in the range
48 * @param last the last character in the range
49 * @return the new IncrementRange
50 * @see Increment
51 */
52 IncrementRange(char first, char last){
53 this.first = first;
54 this.last = last;
55 }
56 /**
57 * Create a default IncrementRange
58 *
59 * @return the new IncrementRange
60 * @see Increment
61 */
62 IncrementRange(){
63 }
64
65 /**
66 * Create a IncrementRange over all Unicode characters
67 *
68 * @return the new IncrementRange
69 * @see Increment
70 */
71 IncrementRange(boolean b){
72 if (true) {
73 this.first = Character.MIN_VALUE;
74 this.last = Character.MAX_VALUE;
75 }
76 }
77 /** the last character in out range */
78 private char first = ' ';
79 /** the last character in out range */
80 private char last = 'z';
81
82 /**
83 * Increment a stream of characters, reading from the reader and writing the incremented characters to the writer
84 *
85 * @exception java.io.Error if any of the characters in the string are outside the range of first-last
86 * @param str the string to be incremented
87 * @return the incremented string
88 * @see Increment
89 */
90 long incrementStream(Reader rdr, Writer wtr) {
91 try {
92 long count = 0; // the return value
93
94 int i = rdr.read();
95 boolean carry = true;
96
97 while (i != -1) {
98 char c = (char) i;
99
100 // check the validity of the character we're about to increment
101 if (c < first || c > last)
102 throw new Error("Character '" + c + "' is ouside range");
103
104 if (c == last) {
105 if (carry == true) {
106 wtr.write(first);
107 carry = true;
108 } else {
109 wtr.write(c);
110 carry = false;
111 }
112 } else {
113 if (carry == true) {
114 wtr.write(++c);
115 carry = false;
116 } else {
117 wtr.write(c);
118 carry = false;
119 }
120 }
121 count++;
122
123 // read the next character
124 i = rdr.read();
125 }
126 if (carry == true) {
127 wtr.write(first);
128 count++;
129 }
130 return count;
131 } catch (IOException e) {
132 System.err.println("IOException in incrementStream()");
133 throw new Error(e.toString());
134 }
135 }
136
137 /**
138 * Increment a string of characters
139 *
140 * @exception java.lang.Error if any of the characters in the string are outside the range of first-last
141 * @param str the string to be incremented
142 * @return the incremented string
143 * @see Increment
144 */
145 public final String incrementString(String str) {
146 StringBuffer buffer = new StringBuffer();
147 if (str.equals("")) {
148 buffer.append(first);
149 return buffer.toString();
150 } else {
151 boolean carry = true;
152 for (int i=0;i<str.length();i++) {
153 char c = str.charAt(i);
154 if (c < first || c > last)
155 throw new Error("Character '" + c + "' is ouside range");
156
157 if (c == last) {
158 if (carry == true) {
159 buffer.append(first);
160 carry = true;
161 } else {
162 buffer.append(c);
163 carry = false;
164 }
165 } else {
166 if (carry == true) {
167 buffer.append(++c);
168 carry = false;
169 } else {
170 buffer.append(c);
171 carry = false;
172 }
173 }
174 }
175 if (carry == true)
176 buffer.append(first);
177 }
178 return buffer.toString();
179 }
180
181
182 /**
183 * Test the incrementing of strings (never returns)
184 */
185 public static void testStringIncrement() throws IOException
186 {
187 IncrementRange n = new IncrementRange ();
188 try {
189 String tmp = "";
190 System.out.println(stringToHex(tmp));
191 while (true) {
192 //checkStr(tmp);
193 tmp = n.incrementString(tmp);
194 n.checkStr(tmp);
195 }
196 } catch (Exception e) {
197 System.out.println("Caught exception: " + e);
198 }
199
200 }
201
202 /**
203 * Test the incrementing of streams (never returns)
204 *
205 */
206 public static void testStreamIncrement()
207 {
208 IncrementRange n = new IncrementRange ();
209 try {
210 long counter = 0;
211 String tmp = "";
212 System.out.println(stringToHex(tmp));
213 while (true) {
214 //checkStr(tmp);
215 String tmp2 = n.incrementString(tmp);
216 StringWriter wtr = new StringWriter();
217 n.incrementStream(new StringReader(tmp),wtr);
218 if (!tmp2.equals(wtr.toString()))
219 throw new Exception("Error: \"" + tmp2 +
220 "\" and \"" + tmp2 +
221 "\"");
222 counter++;
223 if (counter % 1000000 == 0)
224 System.out.println(counter + " " + tmp);
225
226 tmp = tmp2;
227 }
228 } catch (Exception e) {
229 System.out.println("Caught exception: " + e);
230 }
231 }
232
233 /**
234 * Tests stream incrementing
235 *
236 *
237 */
238 public static void main(String args[])
239 {
240 testStreamIncrement();
241 }
242}
243
Note: See TracBrowser for help on using the repository browser.