import static com.github.valid8j.pcond.core.functions.Predicates.isNotNull;
import static com.github.valid8j.pcond.classic.Assertions.*
public class TestClass {
public static void aMethod(Object value) {
assert that(value, isNotNull());
}
}
public enum Assertions extends Enum<Assertions>
Use methods in this class with the
statement.assert
import static com.github.valid8j.pcond.core.functions.Predicates.isNotNull;
import static com.github.valid8j.pcond.classic.Assertions.*
public class TestClass {
public static void aMethod(Object value) {
assert that(value, isNotNull());
}
}
This prints a much more readable error message than one you see for a usual assertion failure.
Exception in thread "main" java.lang.AssertionError: Value:null violated: isNotNull null -> isNotNull -> false at com.github.valid8j.pcond.provider.impls.DefaultAssertionProvider.checkValue(DefaultAssertionProvider.java:70) at com.github.valid8j.pcond.provider.AssertionProviderBase.checkInvariant(AssertionProviderBase.java:78) at com.github.valid8j.pcond.classic.Assertions.that(Assertions.java:51) ...
To enable this feature, do not forget giving
VM option to enable assertions.
Otherwise, the evaluation doesn’t happen completely and even printable predicates constructed the right side of the -ea
assert
statement.
This means you can enable the feature during the development and disable it in the production so that you will not see no performance impact.
Modifier and Type | Method and Description |
---|---|
static <T> boolean |
postcondition(T value,
Predicate<? super T> predicate)
A method to be used for checking a value satisfies a given post-condition.
|
static <T> boolean |
precondition(T value,
Predicate<? super T> predicate)
A method to be used for checking a value satisfies a given pre-condition.
|
static <T> boolean |
that(T value,
Predicate<? super T> predicate)
A method to be used for checking a value satisfies a given invariant condition.
|
static Assertions |
valueOf(String name)
Returns the enum constant of this type with the specified name.
|
static Assertions[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static Assertions[] values()
for (Assertions c : Assertions.values()) System.out.println(c);
public static Assertions valueOf(String name)
name
- the name of the enum constant to be returned.IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is nullpublic static <T> boolean that(T value, Predicate<? super T> predicate)
A method to be used for checking a value satisfies a given invariant condition.
This method is intended to be used in assert
statements.
public void aMethod(String var) {
assert that(var, isNotNull());
}
T
- The type of value
.value
- A value to be checked.predicate
- An invariant condition to check the value
.true
, if the condition given as predicate
is satisfied.public static <T> boolean precondition(T value, Predicate<? super T> predicate)
A method to be used for checking a value satisfies a given pre-condition.
This method is intended to be used in assert
statements.
public void aMethod(String var) {
assert precondition(var, isNotNull());
}
T
- The type of value
.value
- A value to be checked.predicate
- A pre-condition to check the value
.true
, if the condition given as predicate
is satisfied.public static <T> boolean postcondition(T value, Predicate<? super T> predicate)
A method to be used for checking a value satisfies a given post-condition.
This method is intended to be used in assert
statements.
public void aMethod(String var) {
assert postcondition(var, isNotNull());
}
T
- The type of value
.value
- A value to be checked.predicate
- A post-condition to check the value
.true
, if the condition given as predicate
is satisfied.Copyright © 2024. All rights reserved.