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

org.jenetics.internal.util.Args Maven / Gradle / Ivy

/*
 * Java Genetic Algorithm Library (jenetics-3.4.0).
 * Copyright (c) 2007-2016 Franz Wilhelmstötter
 *
 * 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.
 *
 * Author:
 *    Franz Wilhelmstötter ([email protected])
 */
package org.jenetics.internal.util;

import static java.util.Objects.requireNonNull;

import java.util.Optional;
import java.util.function.Function;

import org.jenetics.util.ISeq;

/**
 * Helper class for parsing command line arguments.
 *
 * @author Franz Wilhelmstötter
 * @version 3.4
 * @since 3.4
 */
public class Args {

	private final ISeq _args;

	private Args(final String[] args) {
		_args = ISeq.of(requireNonNull(args));
	}

	/**
	 * Return the parameter with the given name.
	 *
	 * @param name the parameter name
	 * @return the parameter with the given name, if any
	 */
	public Optional arg(final String name) {
		int index = _args.indexOf("--" + name);
		if (index == -1) index = _args.indexOf("-" + name);

		return index >= 0 && index < _args.length() - 1
			? Optional.of(_args.get(index + 1))
			: Optional.empty();
	}

	/**
	 * Return the int-argument with the given name.
	 *
	 * @param name the argument name
	 * @return the int argument value, if any
	 */
	public Optional intArg(final String name) {
		return arg(name)
			.flatMap(s -> parse(s, Integer::new));
	}

	/**
	 * Return the long-argument with the given name.
	 *
	 * @param name the argument name
	 * @return the long argument value, if any
	 */
	public Optional longArg(final String name) {
		return arg(name)
			.flatMap(s -> parse(s, Long::new));
	}

	/**
	 * Return the double-argument with the given name.
	 *
	 * @param name the argument name
	 * @return the double argument value, if any
	 */
	public Optional doubleArg(final String name) {
		return arg(name)
			.flatMap(s -> parse(s, Double::new));
	}

	private static  Optional parse(
		final String string,
		final Function parser
	) {
		Optional value = Optional.empty();
		try {
			value = Optional.of(parser.apply(string));
		} catch (Exception ignore) {
		}

		return value;
	}

	@Override
	public String toString() {
		return _args.toString();
	}

	/**
	 * Wraps the given argument array into an {@code Args} object.
	 *
	 * @param args the underlying command line arguments
	 * @return the wrapped argument object
	 */
	public static Args of(final String[] args) {
		return new Args(args);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy