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

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

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

import java.util.Optional;

import static java.util.Objects.requireNonNull;

/**
 * Supports the "withFallback" method.
 */
public class FallbackParameterSource implements ParameterSource {
    private final ParameterSource primarySource;
    private final ParameterSource fallbackSource;

    public FallbackParameterSource(ParameterSource primarySource, ParameterSource fallbackSource) {
        requireNonNull(primarySource);
        requireNonNull(fallbackSource);
        this.primarySource = primarySource;
        this.fallbackSource = fallbackSource;
    }

    @Override
    public Optional getOptionalString(String key) {
        Optional value = primarySource.getOptionalString(key);
        if (value.isPresent()) {
            return value;
        }
        return fallbackSource.getOptionalString(key);
    }

    @Override
    public Optional getOptionalInteger(String key) {
        Optional value = primarySource.getOptionalInteger(key);
        if (value.isPresent()) {
            return value;
        }
        return fallbackSource.getOptionalInteger(key);
    }

    @Override
    public Optional getOptionalObject(String key) {
        Optional value = primarySource.getOptionalObject(key);
        if (value.isPresent()) {
            return value;
        }
        return fallbackSource.getOptionalObject(key);
    }

    @Override
    public String getPathSeparator() {
        return primarySource.getPathSeparator();
    }

    public ParameterSource getPrimarySource() {
        return primarySource;
    }

    public ParameterSource getFallbackSource() {
        return fallbackSource;
    }
}