KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > lists > SubCharSeq


1 package gnu.lists;
2
3 public class SubCharSeq extends SubSequence
4 implements CharSeq
5 {
6   public SubCharSeq (AbstractSequence base, int startPos, int endPos)
7   {
8     super(base, startPos, endPos);
9   }
10
11   /** Get length of string, in characters.
12    * Synonym for size(), for compatibility with String and StringBuffer. */

13   public int length()
14   {
15     return size();
16   }
17
18   public char charAt(int index)
19   {
20     if (index < 0 || index >=size())
21       throw new IndexOutOfBoundsException JavaDoc();
22     int start = base.nextIndex(ipos0);
23     return ((CharSeq) base).charAt(start + index);
24   }
25
26   /** Copy characters into a destination buffer.
27    * Same interface as java.lang.String's getChars. */

28   public void getChars (int srcBegin, int srcEnd, char[] dst, int dstBegin)
29   {
30     for (int i = srcBegin; i < srcEnd; i++)
31       dst[dstBegin++] = charAt(i);
32   }
33
34   public void setCharAt(int index, char ch)
35   {
36     if (index < 0 || index >=size())
37       throw new IndexOutOfBoundsException JavaDoc();
38     int start = base.nextIndex(ipos0);
39     ((CharSeq) base).setCharAt(start + index, ch);
40   }
41
42   /** Set all the elements to a given character. */
43   public void fill(char value)
44   {
45     int index0 = base.nextIndex(ipos0);
46     int index1 = base.nextIndex(ipos0);
47     ((CharSeq) base).fill(index0, index1, value);
48   }
49
50   public void fill(int fromIndex, int toIndex, char value)
51   {
52     int index0 = base.nextIndex(ipos0);
53     int index1 = base.nextIndex(ipos0);
54     if (fromIndex < 0 || toIndex < fromIndex || index0 + toIndex > index1)
55       throw new IndexOutOfBoundsException JavaDoc();
56     ((CharSeq) base).fill(index0 + fromIndex, index0 + toIndex, value);
57   }
58
59   /* #ifdef JAVA5 */
60   // public void writeTo(int start, int count, Appendable dest)
61
// throws java.io.IOException
62
// {
63
// int index0 = base.nextIndex(ipos0);
64
// int index1 = base.nextIndex(ipos0);
65
// if (start < 0 || count < 0 || index0 + start + count > index1)
66
// throw new IndexOutOfBoundsException();
67
// ((CharSeq) base).writeTo(index0 + start, count, dest);
68
// }
69

70   // public void writeTo(Appendable dest)
71
// throws java.io.IOException
72
// {
73
// int index0 = base.nextIndex(ipos0);
74
// ((CharSeq) base).writeTo(index0, size(), dest);
75
// }
76
/* #else */
77   /**
78    * Write out (part of) this string.
79    * @param start index of initial character to write
80    * @param count number of characters to write
81    * @param dest where to write the characters
82    */

83   public void writeTo(int start, int count, java.io.Writer JavaDoc dest)
84     throws java.io.IOException JavaDoc
85   {
86     int index0 = base.nextIndex(ipos0);
87     int index1 = base.nextIndex(ipos0);
88     if (start < 0 || count < 0 || index0 + start + count > index1)
89       throw new IndexOutOfBoundsException JavaDoc();
90     ((CharSeq) base).writeTo(index0 + start, count, dest);
91   }
92
93   public void writeTo(java.io.Writer JavaDoc dest) throws java.io.IOException JavaDoc
94   {
95     int index0 = base.nextIndex(ipos0);
96     ((CharSeq) base).writeTo(index0, size(), dest);
97   }
98   /* #endif */
99
100   public void consume(int start, int count, Consumer out)
101   {
102     int index0 = base.nextIndex(ipos0);
103     int index1 = base.nextIndex(ipos0);
104     if (start < 0 || count < 0 || index0 + start + count > index1)
105       throw new IndexOutOfBoundsException JavaDoc();
106     ((CharSeq) base).consume(index0 + start, count, out);
107   }
108
109   public String JavaDoc toString()
110   {
111     int sz = size();
112     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(sz);
113     for (int i = 0; i < sz; i++)
114       sbuf.append(charAt(i));
115     return sbuf.toString();
116   }
117
118   private SubCharSeq subCharSeq(int start, int end)
119   {
120     int sz = size();
121     if (start < 0 || end < start || end > sz)
122       throw new IndexOutOfBoundsException JavaDoc();
123     return new SubCharSeq(base,
124               base.createRelativePos(ipos0, start, false),
125               base.createRelativePos(ipos0, end, true));
126   }
127
128   /* #ifdef JAVA2 */
129   public java.util.List JavaDoc subList(int fromIx, int toIx)
130   {
131     return (java.util.List JavaDoc) subCharSeq(fromIx, toIx);
132   }
133   /* #endif */
134
135   /* #ifdef use:java.lang.CharSequence */
136   public CharSequence JavaDoc subSequence(int start, int end)
137   {
138     return subCharSeq(start, end);
139   }
140   /* #endif */
141 }
142
Popular Tags