Created
July 22, 2015 13:48
-
-
Save HackerTheMonkey/b230c3510ab870638e85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple use case would be: