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

48 lines
1.6 KiB
Java

package de.vanitasvitae.imi.codes;
import java.util.regex.Pattern;
public class InputValidator {
// Allowed characters
private static final String ALPHABET = "[a-zA-Z0-9]";
/*
REGEX pattern for study numbers. Allowed are all 3-letter numbers from the alphabet [a-zA-Z0-9].
*/
private static final Pattern STUDY_NUMBER_MATCHER = Pattern.compile(ALPHABET+"{3}");
/*
REGEX pattern for sample numbers. Allowed are four-letter numbers from the alphabet [a-zA-Z0-9].
*/
private static final Pattern SAMPLE_NUMBER_MATCHER = Pattern.compile(ALPHABET+"{4}");
/**
* 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 IllegalArgumentException if the input String doesn't match the regex.
*/
public static String validateStudyNumber(String in) {
if (!STUDY_NUMBER_MATCHER.matcher(in).matches()) {
throw new IllegalArgumentException("Study number does not match REGEX.");
}
return in;
}
/**
* Validate a given sample type. Valid types are found in {@link SampleType}.#
*
* @param in input String
* @return parsed {@link SampleType}.
*
* @throws IllegalArgumentException 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) {
return SampleType.valueOf(in);
}
}