Created
October 11, 2012 19:56
-
-
Save agentgt/3875102 to your computer and use it in GitHub Desktop.
Extendable Preconditions like Guava's with custom exceptions and SLF4J formatting
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
package com.snaphop.util; | |
import javax.annotation.Nullable; | |
import org.slf4j.helpers.MessageFormatter; | |
import java.lang.IllegalArgumentException; | |
import java.lang.IllegalStateException; | |
public abstract class Precondition<ARG extends IllegalArgumentException, STATE extends IllegalStateException> { | |
public static Precondition<IllegalArgumentException, IllegalStateException> check = | |
new Precondition<IllegalArgumentException, IllegalStateException>() { | |
protected IllegalArgumentException argumentException(String message) { | |
return new IllegalArgumentException(message); | |
} | |
protected IllegalStateException stateException(String message) { | |
return new IllegalStateException(message); | |
} | |
}; | |
public final void argument(boolean expression, @Nullable Object errorMessage) { | |
if (!expression) { | |
throw argumentException(String.valueOf(errorMessage)); | |
} | |
} | |
public final void argument(boolean expression, @Nullable String errorMessageTemplate, | |
@Nullable Object... errorMessageArgs) { | |
if (!expression) { | |
throw argumentException(format(errorMessageTemplate, errorMessageArgs)); | |
} | |
} | |
public final void state(boolean expression, @Nullable Object errorMessage) { | |
if (!expression) { | |
throw stateException(String.valueOf(errorMessage)); | |
} | |
} | |
public final void state(boolean expression, @Nullable String errorMessageTemplate, | |
@Nullable Object... errorMessageArgs) { | |
if (!expression) { | |
throw stateException(format(errorMessageTemplate, errorMessageArgs)); | |
} | |
} | |
public final STATE stateInvalid(@Nullable Object errorMessage) { | |
throw stateException(String.valueOf(errorMessage)); | |
} | |
public final STATE stateInvalid(@Nullable String errorMessageTemplate, | |
@Nullable Object... errorMessageArgs) { | |
throw stateException(format(errorMessageTemplate, errorMessageArgs)); | |
} | |
public String format(String message, Object... args) { | |
return MessageFormatter.arrayFormat(message, args).getMessage(); | |
} | |
protected abstract ARG argumentException(String message); | |
protected abstract STATE stateException(String message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// You can use the default which will throw IllegalStateException and IllegalArgumentException by: