public final class EidPreconditions extends Object
boolean expression which is expected to be true (or in the
case of checkNotNull, an object reference which is expected to be non-null). When false (or null) is passed instead,
the EidPreconditions method throws an unchecked exception, which helps the calling method communicate to its
caller that
that caller has made a mistake.
Each method accepts a EID String or Eid object, which is designed to ease of use and provide strict ID for given
Exception usage. This approach speed up development of large application and helps support teams to by giving the both static
and random ID for each possible unpredicted bug.
This is best to use with tools and plugins like
EidGenerator for Netbeans IDE
Example:
In this example,/** * Returns the positive square root of the given value. * * @throws EidIllegalArgumentException if the value is negative */ public static double sqrt(double value) { EidPreconditions.checkArgument(value >= 0.0, "20150718:012333"); // calculate the square root } void exampleBadCaller() { double d = sqrt(-1.0); }
checkArgument throws an EidIllegalArgumentException to indicate that exampleBadCaller
made an error in its call to sqrt. Exception, when it will be printed will contain user given Eid and also
Randomly generated ID. Those fields can be displayed to user on error page on posted directly to issue tracker.
Example:
// Main application class for ex.: http servlet
try {
performRequest(request, response);
} catch (EidRuntimeException ex) {
issuesTracker.put(ex);
throw ex;
}
catch blocks. It's easy and gives developers nice way of
dealing with countless operations that suppose to work as intended.
Example:
InputStream is = EidPreconditions.tryToExecute(new UnsafeSupplier<InputStream>() {
@Override
public InputStream get() throws IOException {
return this.getClass().getClassLoader()
.getResourceAsStream("project.properties");
}
}, "20150718:121521");
| Modifier and Type | Class and Description |
|---|---|
static interface |
EidPreconditions.RiskyCode<R>
Deprecated.
Use instead
EidPreconditions.UnsafeSupplier or EidPreconditions.UnsafeProcedure. To be removed in next release. |
static interface |
EidPreconditions.UnsafeProcedure
This unsafe procedure can be used to execute a code block that can throw some checked Exceptions, that you would
like not to process, because they are unrecoverable bugs.
|
static interface |
EidPreconditions.UnsafeSupplier<T>
This unsafe supplier can be used to execute a code block that needs to return some value and can throw some checked
Exceptions, that you would like not to process, because they are unrecoverable bugs.
|
| Modifier | Constructor and Description |
|---|---|
protected |
EidPreconditions() |
| Modifier and Type | Method and Description |
|---|---|
static void |
checkArgument(Boolean expression,
Eid eid)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static void |
checkArgument(Boolean expression,
Eid eid,
String messageFormat,
Object... parameters)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static void |
checkArgument(Boolean expression,
String eid)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static void |
checkArgument(Boolean expression,
String eid,
String messageFormat,
Object... parameters)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static int |
checkElementIndex(int index,
int size,
Eid eid)
Ensures that
index specifies a valid element in an array, list or string of size size. |
static int |
checkElementIndex(int index,
int size,
Eid eid,
String messageFormat,
Object... parameters)
Ensures that
index specifies a valid element in an array, list or string of size size. |
static int |
checkElementIndex(int index,
int size,
String eid)
Ensures that
index specifies a valid element in an array, list or string of size size. |
static int |
checkElementIndex(int index,
int size,
String eid,
String messageFormat,
Object... parameters)
Ensures that
index specifies a valid element in an array, list or string of size size. |
static <T> T |
checkNotNull(T reference,
Eid eid)
Ensures that an object reference passed as a parameter to the calling method is not null.
|
static <T> T |
checkNotNull(T reference,
Eid eid,
String messageFormat,
Object... parameters)
Ensures that an object reference passed as a parameter to the calling method is not null.
|
static <T> T |
checkNotNull(T reference,
String eid)
Ensures that an object reference passed as a parameter to the calling method is not null.
|
static <T> T |
checkNotNull(T reference,
String eid,
String messageFormat,
Object... parameters)
Ensures that an object reference passed as a parameter to the calling method is not null.
|
static void |
checkState(Boolean expression,
Eid eid)
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the
calling method.
|
static void |
checkState(Boolean expression,
Eid eid,
String messageFormat,
Object... parameters)
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the
calling method.
|
static void |
checkState(Boolean expression,
String eid)
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the
calling method.
|
static void |
checkState(Boolean expression,
String eid,
String messageFormat,
Object... parameters)
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the
calling method.
|
static <R> R |
tryToExecute(EidPreconditions.RiskyCode<R> code,
Eid eid)
Deprecated.
Use instead
tryToExecute(UnsafeSupplier, Eid). To be removed in next release. |
static <R> R |
tryToExecute(EidPreconditions.RiskyCode<R> code,
String eid)
Deprecated.
Use instead
tryToExecute(UnsafeSupplier, String). To be removed in next release. |
static void |
tryToExecute(EidPreconditions.UnsafeProcedure procedure,
Eid eid)
Tries to execute code in given unsafe procedure code block, and if exception is thrown, it will gets rethrown as a
EidRuntimeException with eid given as a argument. |
static void |
tryToExecute(EidPreconditions.UnsafeProcedure procedure,
String eid)
For more info in JavaDoc see
tryToExecute(UnsafeProcedure, Eid) |
static <R> R |
tryToExecute(EidPreconditions.UnsafeSupplier<R> supplier,
Eid eid)
Tries to execute code in given unsafe supplier code block, and if exception is thrown, it will gets rethrown as a
EidRuntimeException with eid given as a argument. |
static <R> R |
tryToExecute(EidPreconditions.UnsafeSupplier<R> supplier,
String eid)
For more info in JavaDoc see
tryToExecute(UnsafeSupplier, Eid) |
public static void checkArgument(@Nullable Boolean expression, @Nonnull String eid)
expression - (Nullable) a boolean expressioneid - (Nonnull) the exception ID to use if the check fails; will be converted to
EidEidIllegalArgumentException - if expression is falseEidNullPointerException - if expression is nullpublic static void checkArgument(@Nullable Boolean expression, @Nonnull String eid, @Nonnull String messageFormat, Object... parameters)
expression - (Nullable) a boolean expressioneid - (Nonnull) the exception ID to use if the check fails; will be converted to
EidmessageFormat - message format in form of String.format(String, Object...)parameters - parameters fo message format in for of String.format(String, Object...)EidIllegalArgumentException - if expression is falseEidNullPointerException - if expression or eid are nullpublic static void checkArgument(@Nullable Boolean expression, @Nonnull Eid eid)
expression - (Nullable) a boolean expressioneid - (Nonnull) the exception ID to use if the check fails; will be converted to
EidEidIllegalArgumentException - if expression is falseEidNullPointerException - if expression is nullpublic static void checkArgument(@Nullable Boolean expression, @Nonnull Eid eid, @Nonnull String messageFormat, Object... parameters)
expression - (Nullable) a boolean expressioneid - (Nonnull) the exception ID to use if the check fails; will be converted to
EidmessageFormat - message format in form of String.format(String, Object...)parameters - parameters fo message format in for of String.format(String, Object...)EidIllegalArgumentException - if expression is falseEidNullPointerException - if expression is nullpublic static void checkState(@Nullable Boolean expression, @Nonnull String eid)
expression - (Nullable) a boolean expressioneid - (Nonnull) the exception message to use if the check fails; will be converted to a string using
String.valueOf(Object)EidIllegalStateException - if expression is falseEidNullPointerException - if expression is nullpublic static void checkState(@Nullable Boolean expression, @Nonnull String eid, @Nonnull String messageFormat, Object... parameters)
expression - (Nullable) a boolean expressioneid - (Nonnull) the exception message to use if the check fails; will be converted to a string using
String.valueOf(Object)messageFormat - message format in form of String.format(String, Object...)parameters - parameters fo message format in for of String.format(String, Object...)EidIllegalStateException - if expression is falseEidNullPointerException - if expression is nullpublic static void checkState(@Nullable Boolean expression, @Nonnull Eid eid)
expression - (Nullable) a boolean expressioneid - (Nonnull) the exception message to use if the check fails; will be converted to a string using
String.valueOf(Object)EidIllegalStateException - if expression is falsepublic static void checkState(@Nullable Boolean expression, @Nonnull Eid eid, @Nonnull String messageFormat, Object... parameters)
expression - (Nullable) a boolean expressioneid - (Nonnull) the exception message to use if the check fails; will be converted to a string using
String.valueOf(Object)messageFormat - message format in form of String.format(String, Object...)parameters - parameters fo message format in for of String.format(String, Object...)EidIllegalStateException - if expression is false@Nonnull public static <T> T checkNotNull(@Nullable T reference, @Nonnull String eid)
T - type of object reference being checkedreference - an object referenceeid - the exception message to use if the check fails; will be converted to a string using
String.valueOf(Object)EidNullPointerException - if reference is null@Nonnull public static <T> T checkNotNull(@Nullable T reference, @Nonnull String eid, @Nonnull String messageFormat, Object... parameters)
T - type of object reference being checkedreference - an object referenceeid - the exception message to use if the check fails; will be converted to a string using
String.valueOf(Object)messageFormat - message format in form of String.format(String, Object...)parameters - parameters fo message format in for of String.format(String, Object...)EidNullPointerException - if reference is null@Nonnull public static <T> T checkNotNull(@Nullable T reference, @Nonnull Eid eid)
T - type of object reference being checkedreference - an object referenceeid - the exception message to use if the check fails; will be converted to a string using
String.valueOf(Object)EidNullPointerException - if reference is null@Nonnull public static <T> T checkNotNull(@Nullable T reference, @Nonnull Eid eid, @Nonnull String messageFormat, Object... parameters)
T - type of object reference being checkedreference - an object referenceeid - the exception message to use if the check fails; will be converted to a string using
String.valueOf(Object)messageFormat - message format in form of String.format(String, Object...)parameters - parameters fo message format in for of String.format(String, Object...)EidNullPointerException - if reference is nullpublic static int checkElementIndex(int index,
int size,
@Nonnull
String eid)
index specifies a valid element in an array, list or string of size size. An element
index may range from zero, inclusive, to size, exclusive.index - a user-supplied index identifying an element of an array, list or stringsize - the size of that array, list or stringeid - the text to use to describe this index in an error messageindexEidIndexOutOfBoundsException - if index is negative or is not less than sizeEidIllegalArgumentException - if size is negativepublic static int checkElementIndex(int index,
int size,
@Nonnull
String eid,
@Nonnull
String messageFormat,
Object... parameters)
index specifies a valid element in an array, list or string of size size. An element
index may range from zero, inclusive, to size, exclusive.index - a user-supplied index identifying an element of an array, list or stringsize - the size of that array, list or stringeid - the text to use to describe this index in an error messagemessageFormat - message format in form of String.format(String, Object...)parameters - parameters fo message format in for of String.format(String, Object...)indexEidIndexOutOfBoundsException - if index is negative or is not less than sizeEidIllegalArgumentException - if size is negativepublic static int checkElementIndex(int index,
int size,
@Nonnull
Eid eid)
index specifies a valid element in an array, list or string of size size. An element
index may range from zero, inclusive, to size, exclusive.index - a user-supplied index identifying an element of an array, list or stringsize - the size of that array, list or stringeid - the text to use to describe this index in an error messageindexEidIndexOutOfBoundsException - if index is negative or is not less than sizeEidIllegalArgumentException - if size is negativepublic static int checkElementIndex(int index,
int size,
@Nonnull
Eid eid,
@Nonnull
String messageFormat,
Object... parameters)
index specifies a valid element in an array, list or string of size size. An element
index may range from zero, inclusive, to size, exclusive.index - a user-supplied index identifying an element of an array, list or stringsize - the size of that array, list or stringeid - the text to use to describe this index in an error messagemessageFormat - message format in form of String.format(String, Object...)parameters - parameters fo message format in for of String.format(String, Object...)indexEidIndexOutOfBoundsException - if index is negative or is not less than sizeEidIllegalArgumentException - if size is negative@Nullable @Deprecated public static <R> R tryToExecute(@Nonnull EidPreconditions.RiskyCode<R> code, @Nonnull String eid)
tryToExecute(UnsafeSupplier, String). To be removed in next release.@Deprecated @Nullable public static <R> R tryToExecute(@Nonnull EidPreconditions.RiskyCode<R> code, @Nonnull Eid eid)
tryToExecute(UnsafeSupplier, Eid). To be removed in next release.@Nullable public static <R> R tryToExecute(@Nonnull EidPreconditions.UnsafeSupplier<R> supplier, @Nonnull String eid)
tryToExecute(UnsafeSupplier, Eid)R - return typesupplier - unsafe supplier code to be executed within a try-catch blockeid - unique developer identifier from date for ex.: "20150716:123200"EidRuntimeException - if code block thrown any exception, which in that case is wrapped in EidRuntimeExceptiontryToExecute(UnsafeSupplier, Eid)public static void tryToExecute(@Nonnull EidPreconditions.UnsafeProcedure procedure, @Nonnull String eid)
tryToExecute(UnsafeProcedure, Eid)procedure - unsafe procedure code to be executed within a try-catch blockeid - unique developer identifier from date for ex.: "20150716:123200"EidRuntimeException - if code block thrown any exception, which in that case is wrapped in EidRuntimeExceptiontryToExecute(UnsafeProcedure, Eid)@Nullable public static <R> R tryToExecute(@Nonnull EidPreconditions.UnsafeSupplier<R> supplier, @Nonnull Eid eid)
EidRuntimeException with eid given as a argument. This is because this exception is threaded as a software bug!
Example:
Document doc = EidPreconditions.tryToExecute(new UnsafeSupplier<Document>() {
@Override
public Document get() throws SAXException, IOException {
DocumentBuilder docBuilder = ...
return docBuilder.parse(new InputSource(reader));
}
}, new Eid("20150718:121521"));
R - return typesupplier - unsafe supplier code to be executed within a try-catch blockeid - unique developer identifier from date for ex.: "20150716:123200"EidRuntimeException - if code block thrown any exception, which in that case is wrapped in EidRuntimeExceptionpublic static void tryToExecute(@Nonnull EidPreconditions.UnsafeProcedure procedure, @Nonnull Eid eid)
EidRuntimeException with eid given as a argument. This is because this exception is threaded as a software bug!
Example:
EidPreconditions.tryToExecute(new UnsafeProcedure() {
@Override
public void execute() throws EJBException {
em.persist(user);
}
}, new Eid("20151117:184627"));
procedure - unsafe procedure code to be executed within a try-catch blockeid - unique developer identifier from date for ex.: "20150716:123200"EidRuntimeException - if code block thrown any exception, which in that case is wrapped in EidRuntimeExceptionCopyright © 2015. All Rights Reserved.