io.vertx.core.cli.TypedOption Maven / Gradle / Ivy
/*
* Copyright (c) 2011-2015 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.vertx.core.cli;
import io.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());
}
}
}