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

com.moon.core.lang.ref.DefaultLocation Maven / Gradle / Ivy

package com.moon.core.lang.ref;

import com.moon.core.util.Table;
import com.moon.core.util.TableImpl;

import java.util.Map;
import java.util.function.Supplier;

/**
 * 推荐使用{@link Table}、{@link TableImpl}
 *
 * @author moonsky
 */
@Deprecated
public class DefaultLocation implements Location {

    private final Supplier creator;

    private Map value;

    public DefaultLocation(Supplier creator) { this.creator = creator; }

    public static final  DefaultLocation of(Supplier creator) {
        return new DefaultLocation<>(creator);
    }

    private Map onlyGetX(X x) { return value == null ? null : (Map) value.get(x); }

    private Map ensureGetX(X x) {
        Map map = this.value, res;
        if (map == null) {
            this.value = map = createSub();
            map.put(x, res = createSub());
        } else if ((res = (Map) map.get(x)) == null) {
            map.put(x, res = createSub());
        }
        return res;
    }

    /**
     * 设置一个值
     *
     * @param x first key
     * @param y second key
     * @param z value
     *
     * @return this
     */
    @Override
    public DefaultLocation put(X x, Y y, Z z) {
        ensureGetX(x).put(y, z);
        return this;
    }

    /**
     * 放置所有
     *
     * @param x   namespace
     * @param map values entry
     *
     * @return this
     */
    @Override
    public DefaultLocation putAll(X x, Map map) {
        ensureGetX(x).putAll(map);
        return this;
    }

    /**
     * 获取一个值
     *
     * @param x namespace
     * @param y key
     *
     * @return value
     */
    @Override
    public Z get(X x, Y y) {
        Map onlyX = onlyGetX(x);
        return onlyX == null ? null : (Z) onlyX.get(y);
    }

    /**
     * 清空
     *
     * @return this
     */
    @Override
    public DefaultLocation clear() {
        this.value = null;
        return this;
    }

    /**
     * 清空
     *
     * @param x namespace
     *
     * @return this
     */
    @Override
    public DefaultLocation clear(X x) {
        Map onlyX = onlyGetX(x);
        if (onlyX != null) { value.remove(x); }
        return this;
    }

    private Map createSub() { return creator.get(); }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy