01: /* CharSubSequence.java
02: *
03: * Created on Sep 30, 2003
04: *
05: * Copyright (C) 2003 Internet Archive.
06: *
07: * This file is part of the Heritrix web crawler (crawler.archive.org).
08: *
09: * Heritrix is free software; you can redistribute it and/or modify
10: * it under the terms of the GNU Lesser Public License as published by
11: * the Free Software Foundation; either version 2.1 of the License, or
12: * any later version.
13: *
14: * Heritrix is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: * GNU Lesser Public License for more details.
18: *
19: * You should have received a copy of the GNU Lesser Public License
20: * along with Heritrix; if not, write to the Free Software
21: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: */
23: package org.archive.io;
24:
25: /**
26: * Provides a subsequence view onto a CharSequence.
27: *
28: * @author gojomo
29: * @version $Revision: 3288 $, $Date: 2005-03-31 17:43:23 +0000 (Thu, 31 Mar 2005) $
30: */
31: public class CharSubSequence implements CharSequence {
32:
33: CharSequence inner;
34: int start;
35: int end;
36:
37: public CharSubSequence(CharSequence inner, int start, int end) {
38: if (end < start) {
39: throw new IllegalArgumentException("Start " + start
40: + " is > " + " than end " + end);
41: }
42:
43: if (end < 0 || start < 0) {
44: throw new IllegalArgumentException("Start " + start
45: + " or end " + end + " is < 0.");
46: }
47:
48: if (inner == null) {
49: throw new NullPointerException(
50: "Passed charsequence is null.");
51: }
52:
53: this .inner = inner;
54: this .start = start;
55: this .end = end;
56: }
57:
58: /*
59: * (non-Javadoc)
60: * @see java.lang.CharSequence#length()
61: */
62: public int length() {
63: return this .end - this .start;
64: }
65:
66: /*
67: * (non-Javadoc)
68: * @see java.lang.CharSequence#charAt(int)
69: */
70: public char charAt(int index) {
71: return this .inner.charAt(this .start + index);
72: }
73:
74: /*
75: * (non-Javadoc)
76: * @see java.lang.CharSequence#subSequence(int, int)
77: */
78: public CharSequence subSequence(int begin, int finish) {
79: return new CharSubSequence(this , begin, finish);
80: }
81:
82: /*
83: * (non-Javadoc)
84: * @see java.lang.CharSequence#toString()
85: */
86: public String toString() {
87: StringBuffer sb = new StringBuffer(length());
88: // could use StringBuffer.append(CharSequence) if willing to do 1.5 & up
89: for (int i = 0; i < length(); i++) {
90: sb.append(charAt(i));
91: }
92: return sb.toString();
93: }
94: }
|