io.deepsense.neptune.clientlibrary.parsers.additionalargumentsparser.AdditionalArgumentsParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neptune-client-library Show documentation
Show all versions of neptune-client-library Show documentation
Enables integration with Neptune in your Java code
/**
* Copyright (c) 2016, CodiLime Inc.
*/
package io.deepsense.neptune.clientlibrary.parsers.additionalargumentsparser;
import io.deepsense.neptune.clientlibrary.models.JobParameters;
import io.deepsense.neptune.clientlibrary.models.Parameter;
import io.deepsense.neptune.clientlibrary.models.impl.parameters.OfflineJobParameters;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Stream;
import static io.deepsense.neptune.clientlibrary.models.impl.parameters.ParameterImpl.createBooleanParameter;
import static io.deepsense.neptune.clientlibrary.models.impl.parameters.ParameterImpl.createIntParameter;
import static io.deepsense.neptune.clientlibrary.models.impl.parameters.ParameterImpl.createDoubleParameter;
import static io.deepsense.neptune.clientlibrary.models.impl.parameters.ParameterImpl.createStringParameter;
import static io.deepsense.neptune.clientlibrary.utils.ArgumentsParserConsts.PARAMETER_PREFIX;
public class AdditionalArgumentsParser {
public JobParameters parse(List offlineJobArguments) {
Map groupedArguments = groupOfflineJobArguments(offlineJobArguments);
return parseToParameters(groupedArguments);
}
/**
* Custom boolean parse method.
* Necessary for not exhaustive boolean parse, i.e.
* method doesn't parse everything not matching @{code Boolean.TRUE} as @{code Boolean.FALSE}
*
* @param value String with value to parse
* @return parsed boolean value
*/
private Optional parseBoolean(String value) {
if (Boolean.TRUE.toString().equalsIgnoreCase(value)) {
return Optional.of(true);
} else if (Boolean.FALSE.toString().equalsIgnoreCase(value)) {
return Optional.of(false);
}
return Optional.empty();
}
private Optional parseInt(String value) {
try {
return Optional.of(Integer.parseInt(value));
} catch (NumberFormatException e) {
return Optional.empty();
}
}
private Optional parseDouble(String value) {
try {
return Optional.of(Double.parseDouble(value));
} catch (NumberFormatException e) {
return Optional.empty();
}
}
private Map groupOfflineJobArguments(List unknownArguments) {
Map parsedArguments = new HashMap<>();
for (Iterator it = unknownArguments.iterator(); it.hasNext();) {
String unknownArgument = it.next();
// arguments without proper prefix are ignored
if (unknownArgument.startsWith(PARAMETER_PREFIX)) {
String argumentName = unknownArgument.replace(PARAMETER_PREFIX, "");
String value;
if (it.hasNext()) {
value = it.next();
} else {
// if parameter doesn't have a value, we assume it's a flag
value = "true";
}
parsedArguments.put(argumentName, value);
}
}
return parsedArguments;
}
private Parameter parseParameter(String parameterName, String stringParameterValue) {
return Stream.>>of(
() -> parseBoolean(stringParameterValue).map(value -> createBooleanParameter(parameterName, value)),
() -> parseInt(stringParameterValue).map(value -> createIntParameter(parameterName, value)),
() -> parseDouble(stringParameterValue).map(value -> createDoubleParameter(parameterName, value)))
.map(Supplier::get)
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst()
.orElse(createStringParameter(parameterName, stringParameterValue));
}
private JobParameters parseToParameters(Map groupedArguments) {
JobParameters jobParameters = new OfflineJobParameters();
for (Map.Entry entry : groupedArguments.entrySet()) {
String parameterName = entry.getKey();
String rawParameterValue = entry.getValue();
Parameter parameter = parseParameter(parameterName, rawParameterValue);
jobParameters.put(parameterName, parameter);
}
return jobParameters;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy