1 57 58 package org.enhydra.apache.xerces.utils; 59 60 67 public final class IntStack { 68 69 73 74 private int fDepth; 75 76 77 private int[] fData; 78 79 83 84 public int size() { 85 return fDepth; 86 } 87 88 89 public void push(int value) { 90 ensureCapacity(fDepth + 1); 91 fData[fDepth++] = value; 92 } 93 94 95 public int peek() { 96 return fData[fDepth - 1]; 97 } 98 99 100 public int pop() { 101 return fData[--fDepth]; 102 } 103 104 105 public void clear() { 106 fDepth = 0; 107 } 108 109 111 112 public void print() { 113 System.out.print('('); 114 System.out.print(fDepth); 115 System.out.print(") {"); 116 for (int i = 0; i < fDepth; i++) { 117 if (i == 3) { 118 System.out.print(" ..."); 119 break; 120 } 121 System.out.print(' '); 122 System.out.print(fData[i]); 123 if (i < fDepth - 1) { 124 System.out.print(','); 125 } 126 } 127 System.out.print(" }"); 128 System.out.println(); 129 } 130 131 135 136 private boolean ensureCapacity(int size) { 137 try { 138 return fData[size] != 0; 139 } 140 catch (NullPointerException e) { 141 fData = new int[32]; 142 } 143 catch (ArrayIndexOutOfBoundsException e) { 144 int[] newdata = new int[fData.length * 2]; 145 System.arraycopy(fData, 0, newdata, 0, fData.length); 146 fData = newdata; 147 } 148 return true; 149 } 150 151 } | Popular Tags |