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

com.github.phantomthief.util.IMultiDataAccess Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
package com.github.phantomthief.util;

import static java.util.Collections.emptyMap;

import java.util.Collection;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author w.vela
 */
public interface IMultiDataAccess {

    static  IMultiDataAccess getOnly(Function, Map> getFunction) {
        return new IMultiDataAccess() {

            @Override
            public Map get(Collection keys) {
                return getFunction.apply(keys);
            }
        };
    }

    static  IMultiDataAccess setOnly(Consumer> setFunction) {
        return new IMultiDataAccess() {

            @Override
            public void set(Map dataMap) {
                setFunction.accept(dataMap);
            }
        };
    }

    static  IMultiDataAccessBuilder
            get(Function, Map> getFunction) {
        return new IMultiDataAccessBuilder<>(getFunction);
    }

    /**
     * @param keys without null.
     * @return map without null values.
     */
    default Map get(Collection keys) {
        return emptyMap();
    }

    /**
     * @param dataMap without null values.
     */
    default void set(Map dataMap) {
    }

    class IMultiDataAccessBuilder {

        private final Function, Map> getFunction;

        private IMultiDataAccessBuilder(Function, Map> getFunction) {
            this.getFunction = getFunction;
        }

        public IMultiDataAccess set(Consumer> setFunction) {
            return new IMultiDataAccess() {

                @Override
                public Map get(Collection keys) {
                    return getFunction.apply(keys);
                }

                @Override
                public void set(Map dataMap) {
                    setFunction.accept(dataMap);
                }
            };
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy