/**
 * Copyright 2010 CosmoCode GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.cosmocode.palava.scope;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.google.common.base.Preconditions;


/**
 * Abstract skeleton implementation of the {@link ScopeContext} interface.
 *
 * @author Willi Schoenborn
 */
public abstract class AbstractScopeContext implements ScopeContext {

    /**
     * Provide the underlying context map. Allows sub classes to 
     * plug-in different map implementations.
     * 
     * @return a context map
     */
    protected abstract Map<Object, Object> context();

    @Override
    public <K, V> void set(K key, V value) {
        Preconditions.checkNotNull(key, "Key");
        context().put(key, value);
    };
    
    @Override
    @SuppressWarnings("unchecked")
    public <K, V> V get(K key) {
        Preconditions.checkNotNull(key, "Key");
        return (V) context().get(key);
    };

    @Override
    public <K> boolean contains(K key) {
        Preconditions.checkNotNull(key, "Key");
        return context().containsKey(key);
    };
    
    @Override
    @SuppressWarnings("unchecked")
    public <K, V> V remove(K key) {
        Preconditions.checkNotNull(key, "Key");
        return (V) context().remove(key);
    };
    
    @Override
    public <K, V> void putAll(Map<? extends K, ? extends V> map) {
        Preconditions.checkNotNull(map, "Map");
        context().putAll(map);
    }
    
    @Override
    public Iterator<Entry<Object, Object>> iterator() {
        return context().entrySet().iterator();
    }
    
    @Override
    public void clear() {
        final Iterator<Entry<Object, Object>> iterator = iterator();
        
        while (iterator.hasNext()) {
            final Entry<Object, Object> entry = iterator.next();
            
            Destroyables.destroySilently(entry.getKey());
            Destroyables.destroySilently(entry.getValue());
            
            iterator.remove();
        }
    }
    
}

@@ -1297,34 +1297,33 @@
 y, value);%0A    %7D
-;
 %0A
+
     %0A    @Overri
@@ -1485,17 +1485,16 @@
 );%0A    %7D
-;
 %0A%0A    @O
@@ -1630,33 +1630,32 @@
 sKey(key);%0A    %7D
-;
 %0A    %0A    @Overr
@@ -1823,14 +1823,13 @@
 y);%0A
+
     %7D
-;
 %0A   

removed empty statements