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

com.github.triceo.robozonky.installer.panels.CommandLinePart Maven / Gradle / Ivy

/*
 * Copyright 2017 The RoboZonky Project
 *
 * 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.
 */

package com.github.triceo.robozonky.installer.panels;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.github.triceo.robozonky.internal.api.Defaults;

public class CommandLinePart {

    private final Map> options = new LinkedHashMap<>();
    private final Map properties = new LinkedHashMap<>();
    private final Map environmentVariables = new LinkedHashMap<>();
    private final Map> jvmArguments = new LinkedHashMap<>();

    public CommandLinePart setOption(final String key, final String... value) {
        this.options.put(key, Collections.unmodifiableCollection(Arrays.asList(value)));
        return this;
    }

    public CommandLinePart setProperty(final String key, final String value) {
        this.properties.put(key, value);
        return this;
    }

    public CommandLinePart setJvmArgument(final String argument) {
        this.jvmArguments.put(argument, Optional.empty());
        return this;
    }

    public CommandLinePart setJvmArgument(final String argument, final String value) {
        this.jvmArguments.put(argument, Optional.of(value));
        return this;
    }

    public CommandLinePart setEnvironmentVariable(final String key, final String value) {
        this.environmentVariables.put(key, value);
        return this;
    }

    public Map> getOptions() {
        return Collections.unmodifiableMap(options);
    }

    public Map getProperties() {
        return Collections.unmodifiableMap(properties);
    }

    public Map getEnvironmentVariables() {
        return Collections.unmodifiableMap(environmentVariables);
    }

    private Collection getOptionItems() {
        return options.entrySet().stream()
                .flatMap((e) -> {
                    final Stream key = Stream.of(e.getKey());
                    final Stream values = e.getValue().stream().map(v -> "\"" + v + "\"");
                    return Stream.concat(key, values);
                }).collect(Collectors.toList());
    }

    public String convertOptions() {
        return this.getOptionItems().stream().collect(Collectors.joining(" "));
    }

    public void storeOptions(final File cliFile) throws IOException {
        Files.write(cliFile.toPath(), this.getOptionItems(), Defaults.CHARSET);
    }

    public Map> getJvmArguments() {
        return Collections.unmodifiableMap(jvmArguments);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy