com.beust.jcommander.IStringConverter Java Examples

The following examples show how to use com.beust.jcommander.IStringConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CommandLine.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
private static JCommander prepareParser(CliOptions options) {
  JCommander.Builder builder = JCommander.newBuilder()
      .programName(SchedulerMain.class.getName());

  builder.addConverterFactory(new IStringConverterFactory() {
    private Map<Class<?>, Class<? extends IStringConverter<?>>> classConverters =
        ImmutableMap.<Class<?>, Class<? extends IStringConverter<?>>>builder()
            .put(Class.class, ClassConverter.class)
            .put(DataAmount.class, DataAmountConverter.class)
            .put(DockerParameter.class, DockerParameterConverter.class)
            .put(InetSocketAddress.class, InetSocketAddressConverter.class)
            .put(KerberosPrincipal.class, KerberosPrincipalConverter.class)
            .put(TimeAmount.class, TimeAmountConverter.class)
            .put(Volume.class, VolumeConverter.class)
            .build();

    @SuppressWarnings("unchecked")
    @Override
    public <T> Class<? extends IStringConverter<T>> getConverter(Class<T> forType) {
      return (Class<IStringConverter<T>>) classConverters.get(forType);
    }
  });

  builder.addObject(getOptionsObjects(options));
  return builder.build();
}
 
Example #2
Source File: RecoveryTool.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
private static JCommander configure(Options options, String... args) {
  JCommander.Builder builder = JCommander.newBuilder().programName(RecoveryTool.class.getName());
  builder.addConverterFactory(new IStringConverterFactory() {
    private Map<Class<?>, Class<? extends IStringConverter<?>>> classConverters =
        ImmutableMap.<Class<?>, Class<? extends IStringConverter<?>>>builder()
            .put(DataAmount.class, DataAmountConverter.class)
            .put(InetSocketAddress.class, InetSocketAddressConverter.class)
            .put(TimeAmount.class, TimeAmountConverter.class)
            .build();

    @SuppressWarnings("unchecked")
    @Override
    public <T> Class<? extends IStringConverter<T>> getConverter(Class<T> forType) {
      return (Class<IStringConverter<T>>) classConverters.get(forType);
    }
  });

  builder.addObject(options);
  for (Endpoint endpoint : Endpoint.values()) {
    endpoint.impl.getOptions().forEach(builder::addObject);
  }

  JCommander parser = builder.build();
  parser.parse(args);
  return parser;
}
 
Example #3
Source File: ConverterFactory.java    From openjavacard-tools with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Class<? extends IStringConverter<?>> getConverter(Class forType) {
    if(forType.equals(AID.class)) {
        return AIDConverter.class;
    }
    if(forType.equals(byte[].class)) {
        return BytesConverter.class;
    }
    return null;
}
 
Example #4
Source File: ParameterFactory.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/** Returns JCommander converter for a given type, or {@code null} if none exists. */
@Nullable
@Override
@SuppressWarnings("unchecked")
public <T> Class<? extends IStringConverter<T>> getConverter(@Nullable Class<T> type) {
  return (Class<? extends IStringConverter<T>>) CONVERTERS.get(type);
}
 
Example #5
Source File: PropertiesConfig.java    From jsflight with Apache License 2.0 5 votes vote down vote up
private <T> T getProperty(String name, T defaultValue, IParameterValidator validator, IStringConverter<T> converter)
{
    String value = properties.getProperty(name);
    if (value == null && defaultValue != null)
    {
        value = defaultValue.toString();
    }
    if (validator != null)
    {
        validator.validate(name, value);
    }
    return value == null ? null : converter.convert(value);
}
 
Example #6
Source File: IdConverterFactory.java    From digdag with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> Class<? extends IStringConverter<T>> getConverter(Class<T> forType)
{
    if (forType.equals(Id.class)) {
        return (Class<IStringConverter<T>>) (Class<?>) IdConverter.class;
    }
    return null;
}
 
Example #7
Source File: CustomParameterConverters.java    From morfologik-stemming with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Class<? extends IStringConverter<?>> getConverter(Class<?> forType) {
  if (forType.equals(Path.class)) {
    return PathConverter.class;
  }
  return null;
}
 
Example #8
Source File: DefaultConverterFactory.java    From muJava with Apache License 2.0 4 votes vote down vote up
public Class<? extends IStringConverter<?>> getConverter(Class forType) {
  return m_classConverters.get(forType);
}
 
Example #9
Source File: PropertiesConfig.java    From jsflight with Apache License 2.0 4 votes vote down vote up
private String getProperty(String name, IParameterValidator validator)
{
    return getProperty(name, validator, (IStringConverter<String>)String::toString);
}
 
Example #10
Source File: PropertiesConfig.java    From jsflight with Apache License 2.0 4 votes vote down vote up
private <T> T getProperty(String name, IParameterValidator validator, IStringConverter<T> converter)
{
    return getProperty(name, null, validator, converter);
}
 
Example #11
Source File: PropertiesConfig.java    From jsflight with Apache License 2.0 4 votes vote down vote up
private <T> T getProperty(String name, T defaultValue, IStringConverter<T> converter)
{
    return getProperty(name, defaultValue, null, converter);
}
 
Example #12
Source File: PropertiesConfig.java    From jsflight with Apache License 2.0 4 votes vote down vote up
@Override
public Integer getProxyPort()
{
    return getProperty(PropertiesConstants.PROXY_PORT, POSITIVE_INTEGER, (IStringConverter<Integer>)Integer::new);
}
 
Example #13
Source File: GeoWaveOperationServiceWrapper.java    From geowave with Apache License 2.0 4 votes vote down vote up
/**
 * Reads Parameter fields of the current instance, and populates them with values from the
 * request.
 *
 * <p> This uses an analogous approach to JCommander. Ideally, it could reuse the same
 * implementation, but ParametersDelegate makes this a bit trickier, since those aren't
 * initialized right away. Follow the behavior as best as possible, and perform validation.
 *
 * @param form The form to fetch parameters from, or the query if form is null.
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
private void injectParameters(final RequestParameters requestParameters, final Object instance)
    throws MissingArgumentException, InstantiationException, IllegalAccessException {
  final List<RestFieldValue<?>> fields = RestFieldFactory.createRestFieldValues(instance);
  for (final RestFieldValue f : fields) {

    Object objValue = null;
    final Class<?> type = f.getType();
    final Field field = f.getField();
    final String strValue = requestParameters.getString(f.getName());

    if (field.isAnnotationPresent(Parameter.class)) {
      final Class<? extends IStringConverter<?>> converter =
          field.getAnnotation(Parameter.class).converter();
      if (converter != null) {
        if ((converter != NoConverter.class) && (strValue != null)) {
          try {
            objValue = converter.newInstance().convert(strValue);
          } catch (final InstantiationException e) {
            LOGGER.warn(
                "Cannot convert parameter since converter does not have zero argument constructor");
          }
        }
      }
    }

    if (objValue == null) {
      if (List.class.isAssignableFrom(type)) {
        objValue = requestParameters.getList(f.getName());
      } else if (type.isArray()) {
        objValue = requestParameters.getArray(f.getName());
        if (objValue != null) {
          objValue =
              Arrays.copyOf((Object[]) objValue, ((Object[]) objValue).length, f.getType());
        }
      } else {
        if (strValue != null) {
          if (Long.class.isAssignableFrom(type) || long.class.isAssignableFrom(type)) {
            objValue = Long.valueOf(strValue);
          } else if (Integer.class.isAssignableFrom(type) || int.class.isAssignableFrom(type)) {
            objValue = Integer.valueOf(strValue);
          } else if (Short.class.isAssignableFrom(type) || short.class.isAssignableFrom(type)) {
            objValue = Short.valueOf(strValue);
          } else if (Byte.class.isAssignableFrom(type) || byte.class.isAssignableFrom(type)) {
            objValue = Byte.valueOf(strValue);
          } else if (Double.class.isAssignableFrom(type) || double.class.isAssignableFrom(type)) {
            objValue = Double.valueOf(strValue);
          } else if (Float.class.isAssignableFrom(type) || float.class.isAssignableFrom(type)) {
            objValue = Float.valueOf(strValue);
          } else if (Boolean.class.isAssignableFrom(type)
              || boolean.class.isAssignableFrom(type)) {
            objValue = Boolean.valueOf(strValue);
          } else if (String.class.isAssignableFrom(type)) {
            objValue = strValue;
          } else if (Enum.class.isAssignableFrom(type)) {
            objValue = Enum.valueOf((Class<Enum>) type, strValue.toUpperCase());
          } else {
            throw new RuntimeException("Unsupported format on field " + f.getType());
          }
        }
      }
    }
    if (objValue != null) {
      f.setValue(objValue);
    } else if (f.isRequired()) {
      throw new MissingArgumentException(f.getName());
    }
  }
}
 
Example #14
Source File: CustomConvertFactory.java    From Scribengin with GNU Affero General Public License v3.0 4 votes vote down vote up
public Class<? extends IStringConverter<?>> getConverter(Class forType) {
  if (forType.equals(HostPort.class)) {
    return HostPortConverter.class;
  } else
    return null;
}