001: /*
002: ******************************************************************
003: Copyright (c) 2001, Jeff Martin, Tim Bacon
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: * Redistributions of source code must retain the above copyright
011: notice, this list of conditions and the following disclaimer.
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the following
014: disclaimer in the documentation and/or other materials provided
015: with the distribution.
016: * Neither the name of the xmlunit.sourceforge.net nor the names
017: of its contributors may be used to endorse or promote products
018: derived from this software without specific prior written
019: permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
024: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
025: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
029: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
030: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
031: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: POSSIBILITY OF SUCH DAMAGE.
033:
034: ******************************************************************
035: */
036:
037: package org.custommonkey.xmlunit.util;
038:
039: /**
040: * Simplistic dynamically growing buffer of integers used by DoctypeSupport.
041: *
042: * <p>No attempt has been made to make this class thread-safe at all.
043: * The append methods and indexOf are not too efficient either, but
044: * work for what we need.</p>
045: */
046: public class IntegerBuffer {
047:
048: // should be big enough for the DoctypeSupport use case
049: private static final int INITIAL_SIZE = 512;
050:
051: private int[] buffer;
052: private int currentSize;
053:
054: /**
055: * Creates a new buffer.
056: */
057: public IntegerBuffer() {
058: this (INITIAL_SIZE);
059: }
060:
061: /**
062: * Creates a new buffer with the given initial capacity.
063: */
064: public IntegerBuffer(int capacity) {
065: buffer = new int[capacity];
066: }
067:
068: /**
069: * Returns the current size.
070: */
071: public int size() {
072: return currentSize;
073: }
074:
075: /**
076: * Returns the current capacity (the size the buffer can use
077: * before it needs to grow).
078: */
079: public int capacity() {
080: return buffer.length;
081: }
082:
083: /**
084: * Appends a single int.
085: */
086: public void append(int i) {
087: // could simply delegate to the other append methods, but this
088: // (avoiding arraycopy) is more efficient.
089: while (currentSize >= buffer.length) {
090: grow();
091: }
092: buffer[currentSize++] = i;
093: }
094:
095: /**
096: * Appends an array of ints.
097: */
098: public void append(int[] i) {
099: // could simply delegate to the other append methods, but this
100: // (avoiding repeated comparisions) is more efficient.
101: while (currentSize + i.length > buffer.length) {
102: grow();
103: }
104: System.arraycopy(i, 0, buffer, currentSize, i.length);
105: currentSize += i.length;
106: }
107:
108: /**
109: * Returns an arry view of this buffer's content.
110: */
111: public int[] toIntArray() {
112: int[] i = new int[currentSize];
113: System.arraycopy(buffer, 0, i, 0, currentSize);
114: return i;
115: }
116:
117: /**
118: * finds sequence in current buffer.
119: *
120: * @return index of sequence or -1 if not found
121: */
122: public int indexOf(int[] sequence) {
123: int index = -1;
124: for (int i = 0; index == -1
125: && i <= currentSize - sequence.length; i++) {
126: if (buffer[i] == sequence[0]) {
127: boolean matches = true;
128: for (int j = 1; matches && j < sequence.length; j++) {
129: if (buffer[i + j] != sequence[j]) {
130: matches = false;
131: }
132: }
133: if (matches) {
134: index = i;
135: }
136: }
137: }
138: return index;
139: }
140:
141: private void grow() {
142: int[] i = new int[buffer.length * 2 + 1];
143: System.arraycopy(buffer, 0, i, 0, buffer.length);
144: buffer = i;
145: }
146: }
|