KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > csdl > stackmvc > model > StackModel


1 package csdl.stackmvc.model;
2
3 import edu.hawaii.stack.Stack;
4 import edu.hawaii.stack.EmptyStackException;
5
6 import java.util.ArrayList JavaDoc;
7
8 /**
9  * Implements a singleton Stack instance.
10  * The command classes manipulate this singleton in response to user input.
11  * The StackModel manipulates string objects only.
12  * To prevent multi-threaded access from corrupting the stack, all operations on the
13  * singleton StackModel instance are synchronized.
14  *
15  * @author Jitender Miglani
16  * @author Philip Johnson
17  */

18 public class StackModel {
19
20   /** The singleton StackModel instance. */
21   private static StackModel theInstance;
22
23   /** The internal stack implementation. */
24   private Stack stack;
25
26   /** Private constructor used to create a single instance of stack */
27   private StackModel() {
28     this.stack = new Stack();
29   }
30
31   /**
32    * Get the single instance of StackModel object.
33    *
34    * @return A StackModel.
35    */

36   public static StackModel getInstance() {
37     if (StackModel.theInstance == null) {
38       StackModel.theInstance = new StackModel();
39     }
40     return StackModel.theInstance;
41   }
42
43
44   /**
45    * Pushes a string object onto the stack.
46    *
47    * @param number The number to be pushed.
48    */

49   public synchronized void push(String JavaDoc number) {
50     this.stack.push(number);
51   }
52
53
54   /**
55    * Pops a string number out of the stack.
56    *
57    * @return The number popped out of the stack.
58    * @throws EmptyStackException If an empty stack was popped.
59    */

60   public synchronized String JavaDoc pop() throws EmptyStackException {
61     return (String JavaDoc) this.stack.pop();
62   }
63
64
65   /**
66    * Gets top element of the stack.
67    *
68    * @return The number on the top of the stack.
69    * @throws EmptyStackException If an empty stack was popped.
70    */

71   public synchronized String JavaDoc top() throws EmptyStackException {
72     return (String JavaDoc) this.stack.top();
73   }
74
75
76   /**
77    * Duplicates the elements in the stack.
78    *
79    * @throws EmptyStackException If an empty stack was popped.
80    */

81   public synchronized void doubles() throws EmptyStackException {
82     ArrayList JavaDoc stackTrace = new ArrayList JavaDoc();
83
84     try {
85       // get all elements in original stack
86
while (true) {
87         stackTrace.add(this.stack.pop());
88       }
89     }
90     catch (EmptyStackException e) {
91       if (stackTrace.size() == 0) {
92         throw new EmptyStackException(e);
93       }
94     }
95
96     // double contents of original stack
97
for (int i = 0; i < 2; i++) {
98       for (int j = stackTrace.size() - 1; j >= 0; j--) {
99         this.stack.push((String JavaDoc) stackTrace.get(j));
100       }
101     }
102   }
103
104
105   /** Clears the stack. */
106   public synchronized void clearStack() {
107     this.stack = new Stack();
108   }
109
110
111   /**
112    * Return a copy of the stack as an array.
113    * Used for presentation purposes.
114    *
115    * @return An array containing the stack data.
116    */

117   public Object JavaDoc[] toArray() {
118     return this.stack.toArray();
119   }
120 }
121
Popular Tags