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

com.github.zhangxd1989.basetool.map.MapWrapper Maven / Gradle / Ivy

There is a newer version: 1.0.16
Show newest version
package com.github.zhangxd1989.basetool.map;

import com.github.zhangxd1989.basetool.util.ObjectUtil;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Map包装类,通过包装一个已有Map实现特定功能。例如自定义Key的规则或Value规则
 *
 * @param  键类型
 * @param  值类型
 * @author sheldon
 */
public class MapWrapper implements Map, Iterable>, Serializable, Cloneable {

    private static final long serialVersionUID = -3528215190773677632L;
    /**
     * 默认增长因子
     */
    protected static final float DEFAULT_LOAD_FACTOR = 0.75f;
    /**
     * 默认初始大小
     */
    protected static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;

    private Map raw;

    /**
     * 构造
     *
     * @param raw 被包装的Map
     */
    public MapWrapper(Map raw) {
        this.raw = raw;
    }

    /**
     * 获取原始的Map
     *
     * @return Map
     */
    public Map getRaw() {
        return this.raw;
    }

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

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

    @Override
    public boolean containsKey(Object key) {
        return raw.containsKey(key);
    }

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

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

    @Override
    public V put(K key, V value) {
        return raw.put(key, value);
    }

    @Override
    public V remove(Object key) {
        return raw.remove(key);
    }

    @Override
    public void putAll(Map m) {
        raw.putAll(m);
    }

    @Override
    public void clear() {
        raw.clear();
    }

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

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

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

    @Override
    public Iterator> iterator() {
        return this.entrySet().iterator();
    }

    @SuppressWarnings("unchecked")
    @Override
    protected Object clone() throws CloneNotSupportedException {
        MapWrapper result;
        try {
            result = (MapWrapper) super.clone();
        } catch (CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw new CloneNotSupportedException();
        }
        result.raw = ObjectUtil.clone(this.raw);
        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy