Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/globalretailtech/pos/context/PosStack.java


1   /*
2    * Copyright (C) 2001 Global Retail Technology, LLC
3    * <http://www.globalretailtech.com>
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  
20  package com.globalretailtech.pos.context;
21  
22  import java.util.Stack;
23  
24  import com.globalretailtech.util.Log;
25  
26  /**
27   * Manages event stacks for a context, pending and processed.
28   * The pending stack is for events yet to be executed, processed
29   * are events that have been processed. In error conditions where
30   * the user wants to correct a dialog, the clear key will restore
31   * the processed to pending.
32   *
33   * @author  Quentin Olson
34   */
35  public class PosStack
36  {
37  
38      private Stack pending;
39      private Stack processed;
40  
41      /**
42       * Simple constructor creates a pending and processed stack.
43       */
44      public PosStack ()
45      {
46  
47          pending = new Stack ();
48          processed = new Stack ();
49      }
50  
51      /**
52       * Number of elements in the pending stack.
53       */
54      protected int pendingSize ()
55      {
56          return pending.size ();
57      }
58      /**
59       * Number of elements in the processed.
60       */
61      protected int processedSize ()
62      {
63          return processed.size ();
64      }
65  
66      /**
67       * Clear the pending and push an object on.
68       */
69      protected void set (Object o)
70      {
71  
72          pending.clear ();
73          pending.push (o);
74      }
75  
76      /**
77       * Insert an object at a location in the stack.
78       */
79      protected void pendingInsertElementAt (Object o, int pos)
80      {
81  
82          pending.insertElementAt (o, pos);
83      }
84  
85      /**
86       * Look at the top of the stack.
87       */
88      protected Object peek ()
89      {
90          return (Object) pending.peek ();
91      }
92  
93      /**
94       * Clear both stacks.
95       */
96      protected void clear ()
97      {
98  
99          pending.removeAllElements ();
100         processed.removeAllElements ();
101     }
102 
103     /**
104      * Pop the pending, push it on the processed and return it.
105      */
106     protected Object popPending ()
107     {
108 
109         //     Object o;
110 
111         //     if (pending.size () > 1) {
112         //       o =  pending.pop ();
113         //       processed.push (o);
114         //     }
115         //     else {
116         //       o = pending.peek ();
117         //     }
118 
119         if (pending.size () == 0)
120             return null;
121         return pending.pop ();
122     }
123 
124     /**
125      * Push an object on the pending stack.
126      */
127     protected void pushPending (Object o)
128     {
129         pending.push (o);
130     }
131 
132     /**
133      * Restore all of the processed to the pending.
134      */
135     protected void restore ()
136     {
137 
138         while (processed.size () > 0)
139         {
140             Object o = processed.pop ();
141             pending.push (o);
142         }
143     }
144 
145     /**
146      * Clear the pending stack.
147      */
148     public void clearPending ()
149     {
150         while (pending.size () > 1)
151         {
152             Object o = pending.pop ();
153         }
154     }
155 
156     /**
157      * Clear the processed stack.
158      */
159     public void clearProcessed ()
160     {
161         while (processed.size () > 1)
162         {
163             Object o = processed.pop ();
164         }
165     }
166 
167     /**
168      * Useful method to dump the contents of both stacks to the
169      * debug Log.
170      */
171     public void dump ()
172     {
173 
174         if (this instanceof PosStateStack)
175         {
176 
177             Log.info ("Dumping states");
178             for (int i=0;i<pending.size ();i++)
179             {
180                 Integer state = (Integer) pending.elementAt (i);
181                 Log.info ("dump pending   " + state.intValue ());
182             }
183             for (int i=0;i<processed.size ();i++)
184             {
185                 Integer state = (Integer) processed.elementAt (i);
186                 Log.info ("dump processed " + state.intValue ());
187             }
188 
189         }
190         else
191         {
192 
193             Log.info ("Dumping events");
194             for (int i=0;i<pending.size ();i++)
195             {
196                 Object o = (Object) pending.elementAt (i);
197                 Log.info ("dump pending   " + o.getClass ());
198             }
199             for (int i=0;i<processed.size ();i++)
200             {
201                 Object o = (Object) processed.elementAt (i);
202                 Log.info ("dump processed " + o.getClass ());
203             }
204 
205         }
206     }
207 }
208 
209 /**
210  * $Log: PosStack.java,v $
211  * Revision 1.1.1.1  2001/08/13 22:18:10  qolson
212  * Initial Checkin 0.2-2
213  *
214  *
215  */