View Javadoc

1   /*
2    * Copyright (c) 2001, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   */
18  package net.sf.zel.vm;
19  
20  import java.util.Collection;
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.Set;
24  
25  /**
26   * Stack of Maps implementation.
27   * @author zoly
28   */
29  public final class StackedMap<K,V>
30          implements Map<K,V>
31  {
32  
33      private final SimpleStack<Map<K, V>> stack;
34  
35      public StackedMap()
36      {
37          stack = new SimpleStack<Map<K, V>>();
38          stack.push(new HashMap<K, V>());
39      }
40  
41  
42      public void push()
43      {
44          stack.push(new HashMap<K, V>());
45      }
46  
47      public Map<K,V> pop()
48      {
49          return stack.pop();
50      }
51  
52  
53  
54      @Override
55      public void putAll(Map m)
56      {
57          stack.peek().putAll(m);
58      }
59  
60      @Override
61      public void clear()
62      {
63          stack.clear();
64          stack.push(new HashMap());
65      }
66  
67      @Override
68      public Set keySet()
69      {
70          throw new UnsupportedOperationException("Not supported yet.");
71      }
72  
73      @Override
74      public Collection values()
75      {
76          throw new UnsupportedOperationException("Not supported yet.");
77      }
78  
79      @Override
80      public Set entrySet()
81      {
82          throw new UnsupportedOperationException("Not supported yet.");
83      }
84  
85      @Override
86      public V get(final Object key)
87      {
88          int i = stack.getPtr() - 1;
89          while (i >= 0)
90          {
91              Map<K, V> m = stack.getFromPtr(i);
92              @SuppressWarnings("element-type-mismatch")
93              V o = m.get(key);
94              if (o != null)
95              {
96                  return o;
97              }
98              i--;
99          }
100         return null;
101     }
102 
103     @Override
104     public int size()
105     {
106         throw new UnsupportedOperationException("Not supported yet.");
107     }
108 
109     @Override
110     public boolean isEmpty()
111     {
112         throw new UnsupportedOperationException("Not supported yet.");
113     }
114 
115 
116     @Override
117     public V put(final K key, final V value)
118     {
119         return stack.peek().put(key, value);
120     }
121 
122     @Override
123     public boolean containsKey(final Object key)
124     {
125         throw new UnsupportedOperationException("Not supported yet.");
126     }
127 
128     @Override
129     public boolean containsValue(final Object value)
130     {
131         throw new UnsupportedOperationException("Not supported yet.");
132     }
133 
134     @Override
135     @SuppressWarnings("element-type-mismatch")
136     public V remove(final Object key)
137     {
138         return stack.peek().remove(key);
139     }
140 
141 }
142