[DISCUSSION] Views of immutable objects.

Paulo Levi i30817 at gmail.com
Tue Jul 31 22:26:28 UTC 2007


You're right, that method does what i suggested. Unfortunately it doesn't
seem to help as i thought it would. I wanted to both eliminate the
allocation and any copying done before inserting the String on the Document.
I can eliminate the allocation keeping a buffer for a String bellow a
certain size (a leak prone technique this where many documents are
instantiated), but not the copying since the internal gapvector does not
allow subclasses to access the internal char [] a string insertion and use
getChars internally. It can only use a char array and then THAT means i must
copy, since there is no way to put the string char [] into the gapVector, or
to take the gapVector char[] and fill it with the string char[].

WARNING : shameless modification of jdk gpl'd code

An alternative, add one method to the package protected abstract class
GapVector:

protected void replace(int position, int rmSize, String addItems, int
addSize) {
    int addOffset = 0;
    if (addSize == 0) {
        close(position, rmSize);
        return;
    } else if (rmSize > addSize) {
        /* Shrink the end. */
        close(position+addSize, rmSize-addSize);
    } else {
        /* Grow the end, do two chunks. */
        //int endSize = addSize - rmSize;
        int end = open(position + rmSize, addSize - rmSize);
        //System.arraycopy(addItems, rmSize, array, end, endSize);
        addItems.getChars(rmSize, addSize, array, end);
        addSize = rmSize;
    }
    //System.arraycopy(addItems, addOffset, array, position, addSize);
    addItems.getChars(addOffset, addSize, array, position);
}

and change the GapContent subclass of gapVector insertString method to this:
public UndoableEdit insertString(int where, String str) throws
BadLocationException {
    if (where > length() || where < 0) {
        throw new BadLocationException("Invalid insert", length());
    }
    //char[] chars = str.toCharArray();
    //replace(where, 0, chars, chars.length);
    replace(where, 0, str, str.length() );
    return new InsertUndo(where, str.length());
}

Voila. If nothing is wrong in the first function, no copy needed in the
Document Hierarchy.
Only this doesn't help those of us using 1.5 even if it is accepted as a
contribution.

I think package protected is evil.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20070731/630aed78/attachment.html>


More information about the core-libs-dev mailing list