01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.openejb.util;
17:
18: public class LinkedListStack implements Stack {
19:
20: private LinkedEntry occupiedEntries;
21:
22: private LinkedEntry vacantEntries;
23:
24: private int size;
25:
26: public LinkedListStack(int initialSize) {
27: for (int i = 0; i < initialSize; i++)
28: vacantEntries = new LinkedEntry(null, vacantEntries);
29: }
30:
31: public synchronized Object push(Object object) {
32: /* Take an entry from the vacant list and move it to the occupied list. */
33:
34: if (vacantEntries == null)
35: occupiedEntries = new LinkedEntry(object, occupiedEntries);
36: else {
37:
38: LinkedEntry entry = vacantEntries;
39:
40: vacantEntries = vacantEntries.next;
41:
42: occupiedEntries = entry.set(object, occupiedEntries);
43: }
44: ++size;
45: return object;
46: }
47:
48: public synchronized Object pop()
49: throws java.util.EmptyStackException {
50: /* Take an entry from the occupied list and move it to the vacant list. */
51:
52: LinkedEntry entry = occupiedEntries;
53: if (entry == null)
54: return null;
55:
56: occupiedEntries = occupiedEntries.next;
57:
58: Object value = entry.value;
59: vacantEntries = entry.set(null, vacantEntries);
60: --size;
61: return value;
62: }
63:
64: public synchronized int size() {
65: return size;
66: }
67:
68: static class LinkedEntry {
69:
70: LinkedEntry next;
71: Object value;
72:
73: LinkedEntry(Object value, LinkedEntry next) {
74: set(value, next);
75: }
76:
77: LinkedEntry set(Object value, LinkedEntry next) {
78: this.next = next;
79: this.value = value;
80: return this;
81: }
82: }
83:
84: }
|