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

com.github.bingoohuang.utils.proxy.ReadOnlyMap Maven / Gradle / Ivy

The newest version!
package com.github.bingoohuang.utils.proxy;

import java.lang.reflect.Proxy;
import java.util.Map;

public interface ReadOnlyMap {
    default boolean containsKey(K key) {
        return true;
    }

    V get(K key);

    @SuppressWarnings("unchecked")
    static  Map proxy(ReadOnlyMap readOnlyMap) {
        return (Map) Proxy.newProxyInstance(ReadOnlyMap.class.getClassLoader(),
                new Class[]{Map.class},
                (proxy, method, args) -> {
                    switch (method.getName()) {
                        case "containsKey":
                            return readOnlyMap.containsKey((K) args[0]);
                        case "get":
                            return readOnlyMap.get((K) args[0]);
                    }

                    throw new RuntimeException("unsupported method " + method.getName());
                });
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy