KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > common > Stack


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.common;
13
14 import com.versant.core.common.Debug;
15
16 import com.versant.core.common.BindingSupportImpl;
17
18 /**
19  * A Stack implementation used by QueryResultIterator to hold values returned for query.
20  */

21 public class Stack {
22
23     /**
24      * The underlying array used for storing the data.
25      */

26     public Object JavaDoc[] m_baseArray;
27     private int size;
28     private int currentIndex;
29
30     /**
31      * Default constructor.
32      */

33     public Stack() {
34     }
35
36     /**
37      * Pop a value from the stack.
38      *
39      * @return value from top of stack
40      * @throws ArrayIndexOutOfBoundsException on attempt to pop empty stack
41      */

42     public Object JavaDoc pop() {
43         if (Debug.DEBUG) {
44             if (size <= 0) {
45                 throw BindingSupportImpl.getInstance().arrayIndexOutOfBounds
46                         ("Attempt to pop empty stack");
47             }
48         }
49         size--;
50         return m_baseArray[currentIndex++];
51     }
52
53     /**
54      * Pop multiple values from the stack.
55      *
56      * @param count number of values to pop from stack (must be strictly
57      * positive)
58      * @throws ArrayIndexOutOfBoundsException on attempt to pop past end of
59      * stack
60      */

61     public void pop(int count) {
62         if (Debug.DEBUG) {
63             if (count > size) {
64                 throw BindingSupportImpl.getInstance().arrayIndexOutOfBounds(
65                         "Attempt to pop past end of stack");
66             }
67             if (count <= 0) {
68                 throw BindingSupportImpl.getInstance().illegalArgument(
69                         "Count must be greater than 0");
70             }
71         }
72         currentIndex += count;
73         size = size - count;
74     }
75
76     public void add(Object JavaDoc[] data, int amountToAdd) {
77         if (m_baseArray == null || size == 0) {
78             /**
79              * No data so just replace with supplied array
80              */

81             m_baseArray = data;
82             size = amountToAdd;
83             currentIndex = 0;
84         } else {
85             /**
86              * There is current data so copy in.
87              */

88             if ((m_baseArray.length - size) < amountToAdd) {
89                 Object JavaDoc[] tmpArray = new Object JavaDoc[m_baseArray.length + amountToAdd];
90                 System.arraycopy(m_baseArray, 0, tmpArray, 0, size);
91                 System.arraycopy(data, 0, tmpArray, size - 1, amountToAdd);
92                 m_baseArray = tmpArray;
93                 size += amountToAdd;
94             }
95             //array copy the new data and update the entry count.
96
System.arraycopy(data, 0, m_baseArray, size - 1, amountToAdd);
97             size += amountToAdd;
98         }
99     }
100
101     public int size() {
102         return size;
103     }
104
105     public boolean isEmpty() {
106         return size == 0;
107     }
108
109     public void clear() {
110         size = 0;
111         currentIndex = 0;
112     }
113
114     public void close() {
115         m_baseArray = null;
116         size = 0;
117         currentIndex = 0;
118     }
119 }
120
Popular Tags