001: package gnu.lists;
002:
003: public class SubCharSeq extends SubSequence implements CharSeq {
004: public SubCharSeq(AbstractSequence base, int startPos, int endPos) {
005: super (base, startPos, endPos);
006: }
007:
008: /** Get length of string, in characters.
009: * Synonym for size(), for compatibility with String and StringBuffer. */
010: public int length() {
011: return size();
012: }
013:
014: public char charAt(int index) {
015: if (index < 0 || index >= size())
016: throw new IndexOutOfBoundsException();
017: int start = base.nextIndex(ipos0);
018: return ((CharSeq) base).charAt(start + index);
019: }
020:
021: /** Copy characters into a destination buffer.
022: * Same interface as java.lang.String's getChars. */
023: public void getChars(int srcBegin, int srcEnd, char[] dst,
024: int dstBegin) {
025: for (int i = srcBegin; i < srcEnd; i++)
026: dst[dstBegin++] = charAt(i);
027: }
028:
029: public void setCharAt(int index, char ch) {
030: if (index < 0 || index >= size())
031: throw new IndexOutOfBoundsException();
032: int start = base.nextIndex(ipos0);
033: ((CharSeq) base).setCharAt(start + index, ch);
034: }
035:
036: /** Set all the elements to a given character. */
037: public void fill(char value) {
038: int index0 = base.nextIndex(ipos0);
039: int index1 = base.nextIndex(ipos0);
040: ((CharSeq) base).fill(index0, index1, value);
041: }
042:
043: public void fill(int fromIndex, int toIndex, char value) {
044: int index0 = base.nextIndex(ipos0);
045: int index1 = base.nextIndex(ipos0);
046: if (fromIndex < 0 || toIndex < fromIndex
047: || index0 + toIndex > index1)
048: throw new IndexOutOfBoundsException();
049: ((CharSeq) base).fill(index0 + fromIndex, index0 + toIndex,
050: value);
051: }
052:
053: /* #ifdef JAVA5 */
054: // public void writeTo(int start, int count, Appendable dest)
055: // throws java.io.IOException
056: // {
057: // int index0 = base.nextIndex(ipos0);
058: // int index1 = base.nextIndex(ipos0);
059: // if (start < 0 || count < 0 || index0 + start + count > index1)
060: // throw new IndexOutOfBoundsException();
061: // ((CharSeq) base).writeTo(index0 + start, count, dest);
062: // }
063: // public void writeTo(Appendable dest)
064: // throws java.io.IOException
065: // {
066: // int index0 = base.nextIndex(ipos0);
067: // ((CharSeq) base).writeTo(index0, size(), dest);
068: // }
069: /* #else */
070: /**
071: * Write out (part of) this string.
072: * @param start index of initial character to write
073: * @param count number of characters to write
074: * @param dest where to write the characters
075: */
076: public void writeTo(int start, int count, java.io.Writer dest)
077: throws java.io.IOException {
078: int index0 = base.nextIndex(ipos0);
079: int index1 = base.nextIndex(ipos0);
080: if (start < 0 || count < 0 || index0 + start + count > index1)
081: throw new IndexOutOfBoundsException();
082: ((CharSeq) base).writeTo(index0 + start, count, dest);
083: }
084:
085: public void writeTo(java.io.Writer dest) throws java.io.IOException {
086: int index0 = base.nextIndex(ipos0);
087: ((CharSeq) base).writeTo(index0, size(), dest);
088: }
089:
090: /* #endif */
091:
092: public void consume(int start, int count, Consumer out) {
093: int index0 = base.nextIndex(ipos0);
094: int index1 = base.nextIndex(ipos0);
095: if (start < 0 || count < 0 || index0 + start + count > index1)
096: throw new IndexOutOfBoundsException();
097: ((CharSeq) base).consume(index0 + start, count, out);
098: }
099:
100: public String toString() {
101: int sz = size();
102: StringBuffer sbuf = new StringBuffer(sz);
103: for (int i = 0; i < sz; i++)
104: sbuf.append(charAt(i));
105: return sbuf.toString();
106: }
107:
108: private SubCharSeq subCharSeq(int start, int end) {
109: int sz = size();
110: if (start < 0 || end < start || end > sz)
111: throw new IndexOutOfBoundsException();
112: return new SubCharSeq(base, base.createRelativePos(ipos0,
113: start, false), base.createRelativePos(ipos0, end, true));
114: }
115:
116: /* #ifdef JAVA2 */
117: public java.util.List subList(int fromIx, int toIx) {
118: return (java.util.List) subCharSeq(fromIx, toIx);
119: }
120:
121: /* #endif */
122:
123: /* #ifdef use:java.lang.CharSequence */
124: public CharSequence subSequence(int start, int end) {
125: return subCharSeq(start, end);
126: }
127: /* #endif */
128: }
|