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

org.apache.maven.cling.invoker.LayeredOptions Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */
package org.apache.maven.cling.invoker;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;

import org.apache.maven.api.cli.Options;
import org.apache.maven.api.cli.ParserRequest;

/**
 * Options that are "layered" by precedence order.
 *
 * @param  The type of options.
 */
public abstract class LayeredOptions implements Options {
    protected final List options;

    protected LayeredOptions(List options) {
        this.options = new ArrayList<>(options);
    }

    @Override
    public Optional> userProperties() {
        return collectMapIfPresentOrEmpty(Options::userProperties);
    }

    @Override
    public String source() {
        return String.format(
                "layered(%s)", options.stream().map(Options::source).toList());
    }

    @Override
    public Optional showVersionAndExit() {
        return returnFirstPresentOrEmpty(Options::showVersionAndExit);
    }

    @Override
    public Optional showVersion() {
        return returnFirstPresentOrEmpty(Options::showVersion);
    }

    @Override
    public Optional quiet() {
        return returnFirstPresentOrEmpty(Options::quiet);
    }

    @Override
    public Optional verbose() {
        return returnFirstPresentOrEmpty(Options::verbose);
    }

    @Override
    public Optional showErrors() {
        return returnFirstPresentOrEmpty(Options::showErrors);
    }

    @Override
    public Optional failOnSeverity() {
        return returnFirstPresentOrEmpty(Options::failOnSeverity);
    }

    @Override
    public Optional nonInteractive() {
        return returnFirstPresentOrEmpty(Options::nonInteractive);
    }

    @Override
    public Optional forceInteractive() {
        return returnFirstPresentOrEmpty(Options::forceInteractive);
    }

    @Override
    public Optional altUserSettings() {
        return returnFirstPresentOrEmpty(Options::altUserSettings);
    }

    @Override
    public Optional altProjectSettings() {
        return returnFirstPresentOrEmpty(Options::altProjectSettings);
    }

    @Override
    public Optional altInstallationSettings() {
        return returnFirstPresentOrEmpty(Options::altInstallationSettings);
    }

    @Override
    public Optional altUserToolchains() {
        return returnFirstPresentOrEmpty(Options::altUserToolchains);
    }

    @Override
    public Optional altInstallationToolchains() {
        return returnFirstPresentOrEmpty(Options::altInstallationToolchains);
    }

    @Override
    public Optional logFile() {
        return returnFirstPresentOrEmpty(Options::logFile);
    }

    @Override
    public Optional rawStreams() {
        return returnFirstPresentOrEmpty(Options::rawStreams);
    }

    @Override
    public Optional color() {
        return returnFirstPresentOrEmpty(Options::color);
    }

    @Override
    public Optional help() {
        return returnFirstPresentOrEmpty(Options::help);
    }

    @Override
    public void warnAboutDeprecatedOptions(ParserRequest request, Consumer printWriter) {}

    @Override
    public void displayHelp(ParserRequest request, Consumer printWriter) {
        options.get(0).displayHelp(request, printWriter);
    }

    protected  Optional returnFirstPresentOrEmpty(Function> getter) {
        for (O option : options) {
            Optional o = getter.apply(option);
            if (o.isPresent()) {
                return o;
            }
        }
        return Optional.empty();
    }

    protected Optional> collectListIfPresentOrEmpty(Function>> getter) {
        int had = 0;
        ArrayList items = new ArrayList<>();
        for (O option : options) {
            Optional> o = getter.apply(option);
            if (o.isPresent()) {
                had++;
                items.addAll(o.get());
            }
        }
        return had == 0 ? Optional.empty() : Optional.of(List.copyOf(items));
    }

    protected Optional> collectMapIfPresentOrEmpty(
            Function>> getter) {
        int had = 0;
        HashMap items = new HashMap<>();
        for (O option : options) {
            Optional> up = getter.apply(option);
            if (up.isPresent()) {
                had++;
                items.putAll(up.get());
            }
        }
        return had == 0 ? Optional.empty() : Optional.of(Map.copyOf(items));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy