1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.archive.io;
24
25
26 /***
27 * Provides a subsequence view onto a CharSequence.
28 *
29 * @author gojomo
30 * @version $Revision: 3288 $, $Date: 2005-03-31 17:43:23 +0000 (Thu, 31 Mar 2005) $
31 */
32 public class CharSubSequence implements CharSequence {
33
34 CharSequence inner;
35 int start;
36 int end;
37
38 public CharSubSequence(CharSequence inner, int start, int end) {
39 if (end < start) {
40 throw new IllegalArgumentException("Start " + start + " is > " +
41 " than end " + end);
42 }
43
44 if (end < 0 || start < 0) {
45 throw new IllegalArgumentException("Start " + start + " or end " +
46 end + " is < 0.");
47 }
48
49 if (inner == null) {
50 throw new NullPointerException("Passed charsequence is null.");
51 }
52
53 this.inner = inner;
54 this.start = start;
55 this.end = end;
56 }
57
58
59
60
61
62 public int length() {
63 return this.end - this.start;
64 }
65
66
67
68
69
70 public char charAt(int index) {
71 return this.inner.charAt(this.start + index);
72 }
73
74
75
76
77
78 public CharSequence subSequence(int begin, int finish) {
79 return new CharSubSequence(this, begin, finish);
80 }
81
82
83
84
85
86 public String toString() {
87 StringBuffer sb = new StringBuffer(length());
88
89 for (int i = 0;i<length();i++) {
90 sb.append(charAt(i));
91 }
92 return sb.toString();
93 }
94 }