KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > binding > util > SparseStack


1 /*
2 Copyright (c) 2005, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package org.jibx.binding.util;
30
31
32 /**
33  * Stack for values that depend on the level of nesting, where only some of the
34  * levels change the current value.
35  *
36  * @author Dennis M. Sosnoski
37  * @version 1.0
38  */

39
40 public class SparseStack
41 {
42     /** Current item. */
43     private Object JavaDoc m_current;
44     
45     /** Current nesting level. */
46     private int m_level;
47     
48     /** Levels with different items (paired with m_items stack). */
49     private IntStack m_levels;
50     
51     /** Stack of different items (paired with m_levels stack). */
52     private ObjectStack m_items;
53     
54     /**
55      * Constructor.
56      */

57     public SparseStack() {
58         
59         // initialize so peek always works
60
m_levels = new IntStack();
61         m_levels.push(-1);
62         m_items = new ObjectStack();
63         m_items.push(null);
64     }
65     
66     /**
67      * Get current object.
68      *
69      * @return
70      */

71     public Object JavaDoc getCurrent() {
72         return m_current;
73     }
74     
75     /**
76      * Set current object.
77      *
78      * @return
79      */

80     public void setCurrent(Object JavaDoc obj) {
81         m_current = obj;
82     }
83     
84     /**
85      * Enter a level of nesting.
86      */

87     public void enter() {
88         if (m_current != m_items.peek()) {
89             m_levels.push(m_level);
90             m_items.push(m_current);
91         }
92         m_level++;
93     }
94     
95     /**
96      * Exit a level of nesting with changed item returned.
97      *
98      * @return item that was active until this exit, or <code>null</code> if
99      * same item still active
100      */

101     public Object JavaDoc exit() {
102         m_level--;
103         if (m_level == m_levels.peek()) {
104             Object JavaDoc obj = m_current;
105             m_levels.pop();
106             m_current = m_items.pop();
107             return obj;
108         } else {
109             return null;
110         }
111     }
112 }
Popular Tags