IMI-Application/src/main/java/de/vanitasvitae/imi/codes/input/InputValidator.java

77 lines
2.6 KiB
Java

package de.vanitasvitae.imi.codes.input;
import java.io.File;
import de.vanitasvitae.imi.codes.types.SampleType;
import de.vanitasvitae.imi.codes.types.StudyNumber;
public class InputValidator {
// Allowed characters
public static final String REGEX_ALPHABET = "[a-zA-Z0-9]";
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
/**
* Validate a given study number. Valid study numbers are 3-letter codes from the alphabet [a-zA-Z0-9].
*
* @param in input String
* @return unmodified input String if it matches.
*
* @throws InvalidOptionException if the input String doesn't match the regex.
*/
public static StudyNumber validateStudyNumber(String in) throws InvalidOptionException {
return new StudyNumber(in);
}
/**
* Validate a given sample type. Valid types are found in {@link SampleType}.#
*
* @param in input String
* @return parsed {@link SampleType}.
*
* @throws InvalidOptionException if the given String doesn't match a {@link SampleType}.
* @throws NullPointerException in case the given String is {@code null}.
*/
public static SampleType validateSampleType(String in) throws InvalidOptionException {
return new SampleType(in);
}
/**
* Validate, that the user provided number of codes to be generated is an integer greater than 0.
*
* @param in input String
* @return number of codes to be generated.
*
* @throws InvalidOptionException in case the input string is
*/
public static int validateNumberOfCodes(String in) throws InvalidOptionException {
int i;
try {
i = Integer.parseInt(in);
} catch (NumberFormatException e) {
throw new InvalidOptionException("Invalid input \"" + in + "\". Please enter a positive number.", e);
}
if (i <= 0) {
throw new InvalidOptionException("Number of generated codes must be greater than 0.");
}
return i;
}
/**
* Validate, that the user-provided path is writable and that any parent folders exist.
* Otherwise throw a {@link InvalidOptionException}.
*
* @param path file path (absolut or relative) as a string.
* @return file
*
* @throws InvalidOptionException in case any parent folder does not exist, the path points to a directory or the
* destination is not writable.
*/
public static File validateOutputPath(String path) throws InvalidOptionException {
// TODO
return null;
}
}