ScopedMap implements ScopeSubscriber, Map<K, V> { public V put(final K key, final V value) { Stack<ScopedMapItem<V>> stack = map.get(key); ScopedMapItem<V> previousItem; if (stack == null) { stack = new Stack<ScopedMapItem<V>>(); map.put(key, stack); previousItem = null; } else { if (stack.empty()) { previousItem = null; } else { previousItem = stack.peek(); } } final ScopedMapItem<V> item = new ScopedMapItem<V>(scope, value); stack.push(item); if (previousItem == null) { return null; } return previousItem.getValue(); } ScopedMap(final ScopeObserver scopeObserver); void close(); void enterScope(final Scope previous, final Scope next); void exitScope(final Scope previous, final Scope next); int size(); boolean isEmpty(); boolean containsKey(final Object key); boolean containsValue(final Object value); V get(final Object key); V put(final K key, final V value); V remove(final Object key); void putAll(final Map<? extends K, ? extends V> t); void clear(); Set<K> keySet(); Collection<V> values(); Set<Map.Entry<K, V>> entrySet(); @Override boolean equals(final Object o); @Override int hashCode();  }
@Test public void testPut() { final ScopedMap<String, String> map = new ScopedMap<String, String>(observer); Assert.assertNull(map.put("key1", "value1")); Assert.assertEquals("value1", map.get("key1")); Assert.assertNull(map.put("key2", "value2")); Assert.assertEquals("value1", map.get("key1")); Assert.assertEquals("value2", map.get("key2")); map.enterScope(Scope.SESSION, Scope.DOCUMENT); Assert.assertEquals("value1", map.get("key1")); Assert.assertEquals("value2", map.get("key2")); Assert.assertEquals("value2", map.put("key2", "value3")); Assert.assertEquals("value1", map.get("key1")); Assert.assertEquals("value3", map.get("key2")); Assert.assertNull(map.put("key3", "value3")); Assert.assertEquals("value3", map.get("key3")); map.exitScope(Scope.DOCUMENT, Scope.SESSION); Assert.assertEquals("value1", map.get("key1")); Assert.assertTrue(map.containsKey("key1")); Assert.assertEquals("value2", map.get("key2")); Assert.assertNull(map.get("key3")); Assert.assertFalse(map.containsKey("key3")); }