1 package csdl.stackmvc.model; 2 3 import edu.hawaii.stack.Stack; 4 import edu.hawaii.stack.EmptyStackException; 5 6 import java.util.ArrayList ; 7 8 18 public class StackModel { 19 20 21 private static StackModel theInstance; 22 23 24 private Stack stack; 25 26 27 private StackModel() { 28 this.stack = new Stack(); 29 } 30 31 36 public static StackModel getInstance() { 37 if (StackModel.theInstance == null) { 38 StackModel.theInstance = new StackModel(); 39 } 40 return StackModel.theInstance; 41 } 42 43 44 49 public synchronized void push(String number) { 50 this.stack.push(number); 51 } 52 53 54 60 public synchronized String pop() throws EmptyStackException { 61 return (String ) this.stack.pop(); 62 } 63 64 65 71 public synchronized String top() throws EmptyStackException { 72 return (String ) this.stack.top(); 73 } 74 75 76 81 public synchronized void doubles() throws EmptyStackException { 82 ArrayList stackTrace = new ArrayList (); 83 84 try { 85 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 for (int i = 0; i < 2; i++) { 98 for (int j = stackTrace.size() - 1; j >= 0; j--) { 99 this.stack.push((String ) stackTrace.get(j)); 100 } 101 } 102 } 103 104 105 106 public synchronized void clearStack() { 107 this.stack = new Stack(); 108 } 109 110 111 117 public Object [] toArray() { 118 return this.stack.toArray(); 119 } 120 } 121 | Popular Tags |