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

com.laamella.parameter_source.SubParameterSource Maven / Gradle / Ivy

There is a newer version: 1.0
Show newest version
package com.laamella.parameter_source;

import java.util.Optional;
import java.util.function.BiFunction;

import static java.util.Objects.requireNonNull;

/**
 * Supports the "subSource" method.
 */
public class SubParameterSource implements ParameterSource {
    private final ParameterSource delegate;
    private final String keyPart;
    private final BiFunction keyCombiner;

    public SubParameterSource(ParameterSource delegate, String keyPart, BiFunction keyCombiner) {
        requireNonNull(delegate);
        requireNonNull(keyPart);
        requireNonNull(keyCombiner);

        this.delegate = delegate;
        this.keyPart = keyPart;
        this.keyCombiner = keyCombiner;
    }

    @Override
    public Optional getOptionalString(String key) {
        requireNonNull(key);
        return delegate.getOptionalString(combineKeys(keyPart, key));
    }

    @Override
    public Optional getOptionalInteger(String key) {
        requireNonNull(key);
        return delegate.getOptionalInteger(combineKeys(keyPart, key));
    }

    @Override
    public Optional getOptionalObject(String key) {
        requireNonNull(key);
        return delegate.getOptionalObject(combineKeys(keyPart, key));
    }

    @Override
    public SubParameterSource subSource(String keyPart) {
        requireNonNull(keyPart);
        return new SubParameterSource(this, keyPart, keyCombiner);
    }

    public String combineKeys(String first, String second) {
        requireNonNull(first);
        requireNonNull(second);

        return keyCombiner.apply(first, second);
    }
}