Skip to content

Instantly share code, notes, and snippets.

@HackerTheMonkey
Created July 22, 2015 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HackerTheMonkey/b230c3510ab870638e85 to your computer and use it in GitHub Desktop.
Save HackerTheMonkey/b230c3510ab870638e85 to your computer and use it in GitHub Desktop.
import org.apache.commons.lang.StringUtils;
public class Validatable {
private boolean isValid;
public static <T> Validatable ifNull(T field) {
if (field == null) {
return new Validatable().invalid();
} else {
return new Validatable().valid();
}
}
public static <T extends String> Validatable ifNullOrEmptyString(T field) {
if (StringUtils.isEmpty(field)) {
return new Validatable().invalid();
} else {
return new Validatable().valid();
}
}
public static <T extends Object> Validatable areEqual(T o, T oo) {
if (!o.equals(oo)) {
return new Validatable().invalid();
} else {
return new Validatable().valid();
}
}
private Validatable invalid() {
isValid = false;
return this;
}
private Validatable valid() {
isValid = true;
return this;
}
public ExceptionThrower thenThrow(Class<? extends RuntimeException> exceptionClass) {
try {
return new ExceptionThrower(exceptionClass, isValid);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
@HackerTheMonkey
Copy link
Author

Simple use case would be:

Validatable. ifNullOrEmptyString(someString).thenThrow(IllegalArgumentException.class).withMessage("You got to be kidding me! try again with valid data");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment