001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.common;
012:
013: import com.versant.core.common.Debug;
014:
015: import com.versant.core.common.BindingSupportImpl;
016:
017: /**
018: * A Stack implementation used by QueryResultIterator to hold values returned for query.
019: */
020: public class Stack {
021:
022: /**
023: * The underlying array used for storing the data.
024: */
025: public Object[] m_baseArray;
026: private int size;
027: private int currentIndex;
028:
029: /**
030: * Default constructor.
031: */
032: public Stack() {
033: }
034:
035: /**
036: * Pop a value from the stack.
037: *
038: * @return value from top of stack
039: * @throws ArrayIndexOutOfBoundsException on attempt to pop empty stack
040: */
041: public Object pop() {
042: if (Debug.DEBUG) {
043: if (size <= 0) {
044: throw BindingSupportImpl.getInstance()
045: .arrayIndexOutOfBounds(
046: "Attempt to pop empty stack");
047: }
048: }
049: size--;
050: return m_baseArray[currentIndex++];
051: }
052:
053: /**
054: * Pop multiple values from the stack.
055: *
056: * @param count number of values to pop from stack (must be strictly
057: * positive)
058: * @throws ArrayIndexOutOfBoundsException on attempt to pop past end of
059: * stack
060: */
061: public void pop(int count) {
062: if (Debug.DEBUG) {
063: if (count > size) {
064: throw BindingSupportImpl.getInstance()
065: .arrayIndexOutOfBounds(
066: "Attempt to pop past end of stack");
067: }
068: if (count <= 0) {
069: throw BindingSupportImpl.getInstance().illegalArgument(
070: "Count must be greater than 0");
071: }
072: }
073: currentIndex += count;
074: size = size - count;
075: }
076:
077: public void add(Object[] data, int amountToAdd) {
078: if (m_baseArray == null || size == 0) {
079: /**
080: * No data so just replace with supplied array
081: */
082: m_baseArray = data;
083: size = amountToAdd;
084: currentIndex = 0;
085: } else {
086: /**
087: * There is current data so copy in.
088: */
089: if ((m_baseArray.length - size) < amountToAdd) {
090: Object[] tmpArray = new Object[m_baseArray.length
091: + amountToAdd];
092: System.arraycopy(m_baseArray, 0, tmpArray, 0, size);
093: System.arraycopy(data, 0, tmpArray, size - 1,
094: amountToAdd);
095: m_baseArray = tmpArray;
096: size += amountToAdd;
097: }
098: //array copy the new data and update the entry count.
099: System.arraycopy(data, 0, m_baseArray, size - 1,
100: amountToAdd);
101: size += amountToAdd;
102: }
103: }
104:
105: public int size() {
106: return size;
107: }
108:
109: public boolean isEmpty() {
110: return size == 0;
111: }
112:
113: public void clear() {
114: size = 0;
115: currentIndex = 0;
116: }
117:
118: public void close() {
119: m_baseArray = null;
120: size = 0;
121: currentIndex = 0;
122: }
123: }
|