All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jetbrick.web.servlet.map.StringEnumeratedMap Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
/**
 * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved.
 *
 * Email: [email protected]
 * URL: http://subchen.github.io/
 *
 * 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 jetbrick.web.servlet.map;

import java.util.*;

public abstract class StringEnumeratedMap implements Map {
    private Map map;

    @Override
    public boolean containsKey(Object key) {
        return get(key) != null;
    }

    @Override
    public boolean containsValue(Object value) {
        return getAsMap().containsValue(value);
    }

    @Override
    public V get(Object key) {
        return getAttribute(key.toString());
    }

    @Override
    public boolean isEmpty() {
        return getAsMap().isEmpty();
    }

    @Override
    public int size() {
        return getAsMap().size();
    }

    @Override
    public Set> entrySet() {
        return getAsMap().entrySet();
    }

    @Override
    public Set keySet() {
        return getAsMap().keySet();
    }

    @Override
    public Collection values() {
        return getAsMap().values();
    }

    @Override
    public synchronized V put(String key, V value) {
        map = null;
        V previous = get(key);
        setAttribute(key, value);
        return previous;
    }

    @Override
    public synchronized void putAll(Map m) {
        map = null;
        for (Map.Entry e : m.entrySet()) {
            setAttribute(e.getKey(), e.getValue());
        }
    }

    @Override
    public synchronized V remove(Object key) {
        map = null;
        V value = get(key);
        removeAttribute(key.toString());
        return value;
    }

    @Override
    public synchronized void clear() {
        map = null;
        Enumeration keys = getAttributeNames();
        while (keys.hasMoreElements()) {
            removeAttribute(keys.nextElement());
        }
    }

    protected abstract Enumeration getAttributeNames();

    protected abstract V getAttribute(String name);

    protected abstract void setAttribute(String name, V value);

    protected abstract void removeAttribute(String name);

    protected Map getAsMap() {
        if (map == null) {
            synchronized (this) {
                if (map == null) {
                    Map result = new HashMap();
                    for (Enumeration e = getAttributeNames(); e.hasMoreElements();) {
                        String key = e.nextElement();
                        V value = getAttribute(key);
                        result.put(key, value);
                    }
                    map = result;
                }
            }
        }
        return map;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy