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

net.pincette.util.ArgsBuilder Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
package net.pincette.util;

import static java.util.Optional.empty;
import static java.util.Optional.of;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * An simple args utility for main methods.
 *
 * @author Werner Donn\u00e9
 */
public class ArgsBuilder {

  private Map args = new HashMap<>();
  private String pending;

  /**
   * Adds arg as a key, with the empty string as the value, if there is no pending arg
   * and as a value for the pending key otherwise.
   *
   * @param arg the key or the value.
   * @return The builder.
   */
  public ArgsBuilder add(final String arg) {
    if (pending != null) {
      args.put(pending, arg);
      pending = null;
    } else {
      args.put(arg, "");
    }

    return this;
  }

  /**
   * Expects another arg, which will be the value for arg.
   *
   * @param arg the key.
   * @return The builder.
   */
  public ArgsBuilder addPending(final String arg) {
    pending = arg;

    return this;
  }

  /**
   * Return an empty optional is there is a pending arg, a map otherwise.
   *
   * @return The map.
   */
  public Optional> build() {
    return pending != null ? empty() : of(args);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy