io.bitsquare.util.spring.JOptCommandLinePropertySource.java Source code

Java tutorial

Introduction

Here is the source code for io.bitsquare.util.spring.JOptCommandLinePropertySource.java

Source

/*
 * This file is part of Bitsquare.
 *
 * Bitsquare is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bitsquare is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
 */

package io.bitsquare.util.spring;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import joptsimple.OptionSet;

/**
 * Customizes {@link io.bitsquare.util.spring.JOptCommandLinePropertySource#getOptionValues(String)}
 * to allow for proper use of {@link joptsimple.ArgumentAcceptingOptionSpec#ofType(Class)}.
 * To be removed once https://github.com/spring-projects/spring-framework/pull/693 has
 * been merged and made available in a release.
 */
public class JOptCommandLinePropertySource extends org.springframework.core.env.JOptCommandLinePropertySource {

    public JOptCommandLinePropertySource(String name, OptionSet options) {
        super(name, options);
    }

    @Override
    public List<String> getOptionValues(String name) {
        List<?> argValues = this.source.valuesOf(name);
        List<String> stringArgValues = new ArrayList<>();
        for (Object argValue : argValues) {
            stringArgValues.add(argValue instanceof String ? (String) argValue : argValue.toString());
        }
        if (stringArgValues.isEmpty()) {
            return (this.source.has(name) ? Collections.<String>emptyList() : null);
        }
        return Collections.unmodifiableList(stringArgValues);
    }
}