KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > io > CharSubSequence


1 /* CharSubSequence.java
2  *
3  * Created on Sep 30, 2003
4  *
5  * Copyright (C) 2003 Internet Archive.
6  *
7  * This file is part of the Heritrix web crawler (crawler.archive.org).
8  *
9  * 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 /**
27  * Provides a subsequence view onto a CharSequence.
28  *
29  * @author gojomo
30  * @version $Revision: 1.7 $, $Date: 2005/03/31 17:43:22 $
31  */

32 public class CharSubSequence implements CharSequence JavaDoc {
33
34     CharSequence JavaDoc inner;
35     int start;
36     int end;
37
38     public CharSubSequence(CharSequence JavaDoc inner, int start, int end) {
39         if (end < start) {
40             throw new IllegalArgumentException JavaDoc("Start " + start + " is > " +
41                 " than end " + end);
42         }
43
44         if (end < 0 || start < 0) {
45             throw new IllegalArgumentException JavaDoc("Start " + start + " or end " +
46                 end + " is < 0.");
47         }
48
49         if (inner == null) {
50             throw new NullPointerException JavaDoc("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 JavaDoc 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 JavaDoc toString() {
87         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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 }
95
Popular Tags