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

com.arangodb.shaded.vertx.core.cli.TypedOption Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
/*
 * Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
 * which is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
 */

package com.arangodb.shaded.vertx.core.cli;

import com.arangodb.shaded.vertx.core.cli.converters.Converter;

import java.util.Arrays;
import java.util.Objects;
import java.util.Set;

/**
 * An implementation of {@link Option} for java specifying the type of
 * object received by the option. This allows converting the given raw value into the specified type.
 *
 * @author Clement Escoffier 
 */
public class TypedOption extends Option {

  /**
   * The type of the option.
   */
  protected Class type;

  /**
   * whether or not the raw value should be parsed as a list. The list if computed by splitting the value.
   */
  protected boolean parsedAsList;

  /**
   * the split character used if the raw value needs to be parsed as a list. {@code ','} is used by default.
   */
  protected String listSeparator = ",";

  /**
   * the converter to create the value.
   */
  protected Converter converter;

  /**
   * Creates an empty instance of {@link TypedOption}.
   */
  public TypedOption() {
    super();
  }

  /**
   * Creates an instance of {@link TypedOption} by copying the state of another {@link TypedOption}
   *
   * @param option the copied option
   */
  public TypedOption(TypedOption option) {
    super(option);
    this.type = option.getType();
    this.converter = option.getConverter();
    this.parsedAsList = option.isParsedAsList();
    this.listSeparator = option.getListSeparator();
  }

  @Override
  public TypedOption setMultiValued(boolean acceptMultipleValues) {
    super.setMultiValued(acceptMultipleValues);
    return this;
  }

  @Override
  public TypedOption setSingleValued(boolean acceptSingleValue) {
    super.setSingleValued(acceptSingleValue);
    return this;
  }

  @Override
  public TypedOption setArgName(String argName) {
    super.setArgName(argName);
    return this;
  }

  @Override
  public TypedOption setDefaultValue(String defaultValue) {
    super.setDefaultValue(defaultValue);
    return this;
  }

  @Override
  public TypedOption setDescription(String description) {
    super.setDescription(description);
    return this;
  }

  @Override
  public TypedOption setFlag(boolean flag) {
    super.setFlag(flag);
    return this;
  }

  @Override
  public TypedOption setHidden(boolean hidden) {
    super.setHidden(hidden);
    return this;
  }

  @Override
  public TypedOption setLongName(String longName) {
    super.setLongName(longName);
    return this;
  }

  @Override
  public TypedOption setRequired(boolean required) {
    super.setRequired(required);
    return this;
  }

  @Override
  public TypedOption setShortName(String shortName) {
    super.setShortName(shortName);
    return this;
  }

  public Class getType() {
    return type;
  }

  public TypedOption setType(Class type) {
    this.type = type;
    if (type != null  && getChoices().isEmpty() && type.isEnum()) {
      setChoicesFromEnumType();
    }
    return this;
  }

  public boolean isParsedAsList() {
    return parsedAsList;
  }

  public TypedOption setParsedAsList(boolean isList) {
    this.parsedAsList = isList;
    return this;
  }

  public String getListSeparator() {
    return listSeparator;
  }

  public TypedOption setListSeparator(String listSeparator) {
    Objects.requireNonNull(listSeparator);
    this.parsedAsList = true;
    this.listSeparator = listSeparator;
    return this;
  }

  public Converter getConverter() {
    return converter;
  }

  public TypedOption setConverter(Converter converter) {
    this.converter = converter;
    return this;
  }

  @Override
  public void ensureValidity() {
    super.ensureValidity();
    if (type == null) {
      throw new IllegalArgumentException("Type must not be null");
    }
  }

  @Override
  public TypedOption setChoices(Set choices) {
    super.setChoices(choices);
    return this;
  }

  @Override
  public TypedOption addChoice(String choice) {
    super.addChoice(choice);
    return this;
  }

  /**
   * Sets the list of values accepted by this option from the option's type.
   */
  private void setChoicesFromEnumType() {
    Object[] constants = type.getEnumConstants();
    for (Object c : constants) {
      addChoice(c.toString());
    }
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy