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

Last change on this file since 5663 was 5663, checked in by kjdon, 21 years ago

fixed up some bad javadoc

  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 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: 5663 $
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 * @see Increment
50 */
51 IncrementRange(char first, char last){
52 this.first = first;
53 this.last = last;
54 }
55 /**
56 * Create a default IncrementRange
57 *
58 * @see Increment
59 */
60 IncrementRange(){
61 }
62
63 /**
64 * Create a IncrementRange over all Unicode characters
65 *
66 * @see Increment
67 */
68 IncrementRange(boolean b){
69 if (true) {
70 this.first = Character.MIN_VALUE;
71 this.last = Character.MAX_VALUE;
72 }
73 }
74 /** the last character in out range */
75 private char first = ' ';
76 /** the last character in out range */
77 private char last = 'z';
78
79 /**
80 * Increment a stream of characters, reading from the reader and writing the incremented characters to the writer
81 *
82 * @exception java.io.Error if any of the characters in the string are outside the range of first-last
83 * @param str the string to be incremented
84 * @return the incremented string
85 * @see Increment
86 */
87 long incrementStream(Reader rdr, Writer wtr) {
88 try {
89 long count = 0; // the return value
90
91 int i = rdr.read();
92 boolean carry = true;
93
94 while (i != -1) {
95 char c = (char) i;
96
97 // check the validity of the character we're about to increment
98 if (c < first || c > last)
99 throw new Error("Character '" + c + "' is ouside range");
100
101 if (c == last) {
102 if (carry == true) {
103 wtr.write(first);
104 carry = true;
105 } else {
106 wtr.write(c);
107 carry = false;
108 }
109 } else {
110 if (carry == true) {
111 wtr.write(++c);
112 carry = false;
113 } else {
114 wtr.write(c);
115 carry = false;
116 }
117 }
118 count++;
119
120 // read the next character
121 i = rdr.read();
122 }
123 if (carry == true) {
124 wtr.write(first);
125 count++;
126 }
127 return count;
128 } catch (IOException e) {
129 System.err.println("IOException in incrementStream()");
130 throw new Error(e.toString());
131 }
132 }
133
134 /**
135 * Increment a string of characters
136 *
137 * @exception java.lang.Error if any of the characters in the string are outside the range of first-last
138 * @param str the string to be incremented
139 * @return the incremented string
140 * @see Increment
141 */
142 public final String incrementString(String str) {
143 StringBuffer buffer = new StringBuffer();
144 if (str.equals("")) {
145 buffer.append(first);
146 return buffer.toString();
147 } else {
148 boolean carry = true;
149 for (int i=0;i<str.length();i++) {
150 char c = str.charAt(i);
151 if (c < first || c > last)
152 throw new Error("Character '" + c + "' is ouside range");
153
154 if (c == last) {
155 if (carry == true) {
156 buffer.append(first);
157 carry = true;
158 } else {
159 buffer.append(c);
160 carry = false;
161 }
162 } else {
163 if (carry == true) {
164 buffer.append(++c);
165 carry = false;
166 } else {
167 buffer.append(c);
168 carry = false;
169 }
170 }
171 }
172 if (carry == true)
173 buffer.append(first);
174 }
175 return buffer.toString();
176 }
177
178
179 /**
180 * Test the incrementing of strings (never returns)
181 */
182 public static void testStringIncrement() throws IOException
183 {
184 IncrementRange n = new IncrementRange ();
185 try {
186 String tmp = "";
187 System.out.println(stringToHex(tmp));
188 while (true) {
189 //checkStr(tmp);
190 tmp = n.incrementString(tmp);
191 n.checkStr(tmp);
192 }
193 } catch (Exception e) {
194 System.out.println("Caught exception: " + e);
195 }
196
197 }
198
199 /**
200 * Test the incrementing of streams (never returns)
201 *
202 */
203 public static void testStreamIncrement()
204 {
205 IncrementRange n = new IncrementRange ();
206 try {
207 long counter = 0;
208 String tmp = "";
209 System.out.println(stringToHex(tmp));
210 while (true) {
211 //checkStr(tmp);
212 String tmp2 = n.incrementString(tmp);
213 StringWriter wtr = new StringWriter();
214 n.incrementStream(new StringReader(tmp),wtr);
215 if (!tmp2.equals(wtr.toString()))
216 throw new Exception("Error: \"" + tmp2 +
217 "\" and \"" + tmp2 +
218 "\"");
219 counter++;
220 if (counter % 1000000 == 0)
221 System.out.println(counter + " " + tmp);
222
223 tmp = tmp2;
224 }
225 } catch (Exception e) {
226 System.out.println("Caught exception: " + e);
227 }
228 }
229
230 /**
231 * Tests stream incrementing
232 *
233 *
234 */
235 public static void main(String args[])
236 {
237 testStreamIncrement();
238 }
239}
240
Note: See TracBrowser for help on using the repository browser.