MavenOptionCollection.java

/*
 * 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
 *
 *   https://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.rat.documentation.options;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

import org.apache.commons.cli.Option;
import org.apache.commons.lang3.StringUtils;
import org.apache.rat.commandline.Arg;
import org.apache.rat.ui.UIOptionCollection;

/**
 * The collection of MavenOptions equivalent to the CLI options
 * with any unsupported options removed.
 */
public final class MavenOptionCollection extends UIOptionCollection<MavenOption> {
    /**
     * Mapping of standard name to non-conflicting name.
     */
    private static final Map<String, String> RENAME_MAP;

    static {
        Map<String, String> map = new HashMap<>();
        map.put("addLicense", "add-license");
        RENAME_MAP = map;
    }

    /**
     * Gets the map of renamed Options indexed by original option name.
     * @return the map of renamed Options indexed by original option name.
     */
    public static Map<String, String> getRenameMap() {
        return new TreeMap<>(RENAME_MAP);
    }

    /**
     * Create an instance.
     */
    public MavenOptionCollection() {
        super(new Builder());
    }

    @Override
    public String rename(final Option option) {
        String key = super.rename(option);
        return StringUtils.defaultIfEmpty(RENAME_MAP.get(key), key);
    }

    /**
     * The Builder for the MavenOptionCollection.
     */
    public static final class Builder extends UIOptionCollection.Builder<MavenOption, Builder> {
        private Builder() {
            super(MavenOption.MavenOptionBuilder::new);
            Arg.getOptions().getOptions()
                    .stream().filter(o -> Objects.isNull(o.getLongOpt()))
                    .forEach(this::unsupported);
            unsupported(Arg.DIR).unsupported(Arg.LOG_LEVEL);
        }
    }
}