1 45 package org.openejb.util; 46 47 48 54 public class LinkedListStack implements Stack { 55 56 60 private LinkedEntry occupiedEntries; 61 62 66 private LinkedEntry vacantEntries; 67 68 71 private int size; 72 73 79 public LinkedListStack(int initialSize) { 80 for ( int i = 0; i < initialSize; i++ ) 81 vacantEntries = new LinkedEntry( null, vacantEntries ); 82 } 83 84 public synchronized Object push( Object object ) { 85 86 87 if ( vacantEntries == null ) 88 occupiedEntries = new LinkedEntry( object, occupiedEntries ); 89 else { 90 LinkedEntry entry = vacantEntries; 92 93 vacantEntries = vacantEntries.next; 95 96 occupiedEntries = entry.set(object, occupiedEntries); 99 } 100 ++size; 101 return object; 102 } 103 104 105 public synchronized Object pop() throws java.util.EmptyStackException { 106 107 108 LinkedEntry entry = occupiedEntries; 110 if ( entry == null ) return null; 111 112 occupiedEntries = occupiedEntries.next; 114 115 Object value = entry.value; 118 vacantEntries = entry.set(null ,vacantEntries); 119 --size; 120 return value; 121 } 122 123 public synchronized int size() { 124 return size; 125 } 126 130 static class LinkedEntry { 131 132 LinkedEntry next; 133 Object value; 134 135 LinkedEntry( Object value, LinkedEntry next ) { 136 set(value,next); 137 } 138 139 LinkedEntry set( Object value, LinkedEntry next ) { 140 this.next = next; 141 this.value = value; 142 return this; 143 } 144 } 145 146 } | Popular Tags |