1 2 12 package com.versant.core.common; 13 14 import com.versant.core.common.Debug; 15 16 import com.versant.core.common.BindingSupportImpl; 17 18 21 public class Stack { 22 23 26 public Object [] m_baseArray; 27 private int size; 28 private int currentIndex; 29 30 33 public Stack() { 34 } 35 36 42 public Object 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 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 [] data, int amountToAdd) { 77 if (m_baseArray == null || size == 0) { 78 81 m_baseArray = data; 82 size = amountToAdd; 83 currentIndex = 0; 84 } else { 85 88 if ((m_baseArray.length - size) < amountToAdd) { 89 Object [] tmpArray = new Object [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 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 |