001: package uk.org.ponder.stringutil;
002:
003: //import uk.org.ponder.util.Logger;
004:
005: import uk.org.ponder.arrayutil.ArrayUtil;
006:
007: /** This class allows chunk-wise manipulation of character data, in the form of
008: * a vector of CharWrap objects.
009: */
010:
011: public class CharWrapVector {
012: private CharWrap[] storage;
013: private int filled;
014:
015: /** Constructs a new CharWrapVector with the specified initial size.
016: * @param size The initial size of the new CharWrapVector.
017: */
018:
019: public CharWrapVector(int size) {
020: if (size < 10)
021: size = 10;
022: storage = new CharWrap[size];
023: filled = 0;
024: }
025:
026: /** Returns the contents of this CharWrapVector as an array of CharWrap. The represented
027: * data will start at index 0 of this array.
028: * @return The contents of this CharWrapVector as an array of CharWrap.
029: */
030:
031: public CharWrap[] getArray() {
032: return storage;
033: }
034:
035: private void expand(int newsize) {
036: // Logger.println("Expand from "+storage.length+" to "+newsize, Logger.DEBUG_INFORMATIONAL);
037: storage = (CharWrap[]) ArrayUtil.expand(storage, newsize);
038: }
039:
040: /** Appends the supplied CharWrap object onto the end of this CharWrapVector.
041: * @param toappend The CharWrap to be appended.
042: */
043:
044: public void append(CharWrap toappend) {
045: if (filled == storage.length)
046: expand(storage.length * 2);
047: storage[filled] = toappend;
048: filled++;
049: }
050:
051: /** Appends the specified portion of the supplied CharWrapVector onto the end of this
052: * CharWrapVector.
053: * @param other The CharWrapVector holding the CharWraps to be appended.
054: * @param offset The start index of the CharWraps to be appended.
055: * @param length The number of CharWraps to be appended.
056: */
057:
058: public void append(CharWrapVector other, int offset, int length) {
059: if (filled + length > storage.length)
060: expand(filled + length > storage.length * 2 ? filled
061: + length : storage.length * 2);
062: /*
063: Logger.println("About to call System.arraycopy: ol "+other.storage.length+" offset "
064: +offset+" l "+storage.length+" filled "+filled+" length "+length,
065: Logger.DEBUG_INFORMATIONAL);
066: */
067: System
068: .arraycopy(other.storage, offset, storage, filled,
069: length);
070: filled += length;
071: }
072:
073: /** This method compares the range of this vector with a range of the same length
074: * in another. If there is a mismatch, the index of the mismatch is returned - otherwise
075: * it returns -1.
076: * @param thisoffset The offset within this CharWrapVector to begin the comparison at.
077: * @param other The other CharWrapVector to compare this object with.
078: * @param otheroffset The offset within the other CharWrapVector to begin the comparison at.
079: * @param length The common length of the segments to be compared from each CharWrapVector.
080: * @return -1 if the specified ranges match exactly, otherwise the index of the mismatch
081: * point from the offset position.
082: */
083:
084: public int compareRange(int this offset, CharWrapVector other,
085: int otheroffset, int length) {
086: for (int i = 0; i < length; ++i) {
087: if (!(storage[this offset + i]
088: .equals(other.storage[otheroffset + i])))
089: return i;
090: }
091: return -1;
092: }
093:
094: /** Returns the number of elements in this CharWrapVector.
095: * @return The number of elements in this CharWrapVector.
096: */
097: public int size() {
098: return filled;
099: }
100:
101: /** Returns the CharWrap object at the specified index.
102: * @param i The index of the required CharWrap object.
103: * @return The CharWrap object at the specified index.
104: */
105: public CharWrap charWrapAt(int i) {
106: return storage[i];
107: }
108:
109: /** Converts this CharWrapVector to a String for debugging purposes. Word boundaries
110: * are represented with the character |.
111: * @return The data in this CharWrapVector as a String for debugging purposes.
112: */
113:
114: public String toDebugString() {
115: CharWrap togo = new CharWrap();
116: for (int i = 0; i < filled; ++i) {
117: togo.append('|').append(storage[i]).append('|');
118: }
119: return togo.toString();
120: }
121: }
|