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

net.binis.codegen.collection.CodeMapImpl Maven / Gradle / Ivy

There is a newer version: 1.2.22
Show newest version
package net.binis.codegen.collection;

/*-
 * #%L
 * code-generator-core
 * %%
 * Copyright (C) 2021 - 2024 Binis Belev
 * %%
 * 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.
 * #L%
 */

import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

import static java.util.Objects.nonNull;

public class CodeMapImpl implements CodeMap {

    protected final R parent;
    protected final Map map;
    protected final Consumer keyValidator;
    protected final Consumer valueValidator;

    public CodeMapImpl(R parent, Map map) {
        this.parent = parent;
        this.map = map;
        this.keyValidator = null;
        this.valueValidator = null;
    }

    public CodeMapImpl(R parent, Map map, Consumer keyValidator, Consumer valueValidator) {
        this.parent = parent;
        this.map = map;
        this.keyValidator = keyValidator;
        this.valueValidator = valueValidator;
    }

    @Override
    public CodeMap put(K key, V value) {
        validate(key, value);
        map.put(key, value);
        return this;
    }

    @Override
    public CodeMap putAll(Map map) {
        map.forEach(this::put);
        return this;
    }

    @Override
    public CodeMap remove(Object key) {
        map.remove(key);
        return this;
    }

    @Override
    public CodeMap clear() {
        map.clear();
        return this;
    }

    @Override
    public CodeMap replaceAll(BiFunction function) {
        map.replaceAll((k, v) -> {
            validate(k, v);
            return function.apply(k, v);
        });
        return this;
    }

    @Override
    public CodeMap putIfAbsent(K key, V value) {
        validate(key, value);
        map.putIfAbsent(key, value);
        return this;
    }

    @Override
    public CodeMap remove(Object key, Object value) {
        map.remove(key, value);
        return this;
    }

    @Override
    public CodeMap replace(K key, V oldValue, V newValue) {
        validate(key, newValue);
        map.replace(key, oldValue, newValue);
        return this;
    }

    @Override
    public CodeMap replace(K key, V value) {
        validate(key, value);
        map.replace(key, value);
        return this;
    }

    @Override
    public CodeMap computeIfAbsent(K key, Function mappingFunction) {
        validateKey(key);
        map.computeIfAbsent(key, k -> {
            var value = mappingFunction.apply(k);
            validateValue(value);
            return value;
        });
        return this;
    }

    @Override
    public CodeMap computeIfPresent(K key, BiFunction remappingFunction) {
        validateKey(key);
        map.computeIfPresent(key, (k, v) -> {
            var value = remappingFunction.apply(k, v);
            validateValue(value);
            return value;
        });
        return this;
    }

    @Override
    public CodeMap compute(K key, BiFunction remappingFunction) {
        validateKey(key);
        map.compute(key, (k, v) -> {
            var value = remappingFunction.apply(k, v);
            validateValue(value);
            return value;
        });
        return this;
    }

    @Override
    public CodeMap merge(K key, V value, BiFunction remappingFunction) {
        validateKey(key);
        map.merge(key, value, (old, v) -> {
            var val = remappingFunction.apply(old, v);
            validateValue(val);
            return val;
        });
        return this;
    }

    @Override
    public R done() {
        return parent;
    }

    protected void validate(K key, V value) {
        validateKey(key);
        validateValue(value);
    }

    protected void validateKey(K key) {
        if (nonNull(keyValidator)) {
            keyValidator.accept(key);
        }
    }

    protected void validateValue(V value) {
        if (nonNull(valueValidator)) {
            valueValidator.accept(value);
        }
    }

    @Override
    public CodeMap _if(boolean condition, Consumer> consumer) {
        if (condition) {
            consumer.accept(this);
        }
        return this;
    }

    @Override
    public CodeMap _if(boolean condition, BiConsumer, R> consumer) {
        if (condition) {
            consumer.accept(this, parent);
        }
        return this;
    }

    @Override
    public CodeMap _if(boolean condition, Consumer> consumer, Consumer> elseConsumer) {
        if (condition) {
            consumer.accept(this);
        } else {
            elseConsumer.accept(this);
        }
        return this;
    }

    @Override
    public CodeMap _if(boolean condition, BiConsumer, R> consumer, BiConsumer, R> elseConsumer) {
        if (condition) {
            consumer.accept(this, parent);
        } else {
            elseConsumer.accept(this, parent);
        }
        return this;
    }

    @Override
    public CodeMap _self(BiConsumer, R> consumer) {
        consumer.accept(this, parent);
        return this;
    }

    @Override
    public CodeMap _map(Object source) {
        throw new UnsupportedOperationException("Not implemented yet!");
        //TODO: Implement handling for collections.
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy