jdk/src/share/classes/javax/swing/text/GapVector.java
author darcy
Sun, 23 Mar 2014 13:49:48 -0700
changeset 23697 e556a715949f
parent 23010 6dadb192ad81
permissions -rw-r--r--
8034169: Fix serial lint warnings in javax.swing Reviewed-by: alanb, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
     2
 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.undo.UndoableEdit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * An implementation of a gapped buffer similar to that used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * emacs.  The underlying storage is a java array of some type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * which is known only by the subclass of this class.  The array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * has a gap somewhere.  The gap is moved to the location of changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * to take advantage of common behavior where most changes occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * in the same location.  Changes that occur at a gap boundary are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * generally cheap and moving the gap is generally cheaper than
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
    39
 * moving the array contents directly to accommodate the change.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @see GapContent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
    44
@SuppressWarnings("serial") // Data in fields not necessarily serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
abstract class GapVector implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Creates a new GapVector object.  Initial size defaults to 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public GapVector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Creates a new GapVector object, with the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * size specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param initialLength the initial size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public GapVector(int initialLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        array = allocateArray(initialLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        g0 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        g1 = initialLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Allocate an array to store items of the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * appropriate (which is determined by the subclass).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected abstract Object allocateArray(int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Get the length of the allocated array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected abstract int getArrayLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Access to the array.  The actual type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * of the array is known only by the subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected final Object getArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Access to the start of the gap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    protected final int getGapStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return g0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Access to the end of the gap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    protected final int getGapEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return g1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // ---- variables -----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * The array of items.  The type is determined by the subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private Object array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * start of gap in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private int g0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * end of gap in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private int g1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // --- gap management -------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Replace the given logical position in the storage with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * the given new items.  This will move the gap to the area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * being changed if the gap is not currently located at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * change location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param position the location to make the replacement.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *  is not the location in the underlying storage array, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *  the location in the contiguous space being modeled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param rmSize the number of items to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param addItems the new items to place in storage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    protected void replace(int position, int rmSize, Object addItems, int addSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        int addOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (addSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            close(position, rmSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } else if (rmSize > addSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            /* Shrink the end. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            close(position+addSize, rmSize-addSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            /* Grow the end, do two chunks. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            int endSize = addSize - rmSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            int end = open(position + rmSize, endSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            System.arraycopy(addItems, rmSize, array, end, endSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            addSize = rmSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        System.arraycopy(addItems, addOffset, array, position, addSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Delete nItems at position.  Squeezes any marks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * within the deleted area to position.  This moves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * the gap to the best place by minimizing it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * overall movement.  The gap must intersect the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * target block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    void close(int position, int nItems) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (nItems == 0)  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int end = position + nItems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        int new_gs = (g1 - g0) + nItems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (end <= g0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            // Move gap to end of block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (g0 != end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                shiftGap(end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // Adjust g0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            shiftGapStartDown(g0 - nItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } else if (position >= g0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // Move gap to beginning of block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (g0 != position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                shiftGap(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            // Adjust g1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            shiftGapEndUp(g0 + new_gs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            // The gap is properly inside the target block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            // No data movement necessary, simply move both gap pointers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            shiftGapStartDown(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            shiftGapEndUp(g0 + new_gs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Make space for the given number of items at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @return the location that the caller should fill in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    int open(int position, int nItems) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        int gapSize = g1 - g0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (nItems == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (position > g0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                position += gapSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // Expand the array if the gap is too small.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        shiftGap(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (nItems >= gapSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // Pre-shift the gap, to reduce total movement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            shiftEnd(getArrayLength() - gapSize + nItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            gapSize = g1 - g0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        g0 = g0 + nItems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * resize the underlying storage array to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * given new size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    void resize(int nsize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        Object narray = allocateArray(nsize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        System.arraycopy(array, 0, narray, 0, Math.min(nsize, getArrayLength()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        array = narray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Make the gap bigger, moving any necessary data and updating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * the appropriate marks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    protected void shiftEnd(int newSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        int oldSize = getArrayLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        int oldGapEnd = g1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        int upperSize = oldSize - oldGapEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int arrayLength = getNewArraySize(newSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int newGapEnd = arrayLength - upperSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        resize(arrayLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        g1 = newGapEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (upperSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            // Copy array items to new end of array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            System.arraycopy(array, oldGapEnd, array, newGapEnd, upperSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Calculates a new size of the storage array depending on required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param reqSize the size which is necessary for new content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @return the new size of the storage array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    int getNewArraySize(int reqSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return (reqSize + 1) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Move the start of the gap to a new location,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * without changing the size of the gap.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * moves the data in the array and updates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * marks accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    protected void shiftGap(int newGapStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (newGapStart == g0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        int oldGapStart = g0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        int dg = newGapStart - oldGapStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        int oldGapEnd = g1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        int newGapEnd = oldGapEnd + dg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        int gapSize = oldGapEnd - oldGapStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        g0 = newGapStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        g1 = newGapEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (dg > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            // Move gap up, move data down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            System.arraycopy(array, oldGapEnd, array, oldGapStart, dg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        } else if (dg < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // Move gap down, move data up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            System.arraycopy(array, newGapStart, array, newGapEnd, -dg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Adjust the gap end downward.  This doesn't move
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * any data, but it does update any marks affected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * by the boundary change.  All marks from the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * gap start down to the new gap start are squeezed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * to the end of the gap (their location has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * removed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    protected void shiftGapStartDown(int newGapStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        g0 = newGapStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Adjust the gap end upward.  This doesn't move
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * any data, but it does update any marks affected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * by the boundary change. All marks from the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * gap end up to the new gap end are squeezed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * to the end of the gap (their location has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * removed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    protected void shiftGapEndUp(int newGapEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        g1 = newGapEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
}