Package org.linkki.util
Class Objects
java.lang.Object
org.linkki.util.Objects
Provides
static utility methods that delegate to Objects and are
additionally annotated with SpotBugs null annotations.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TrequireNonNull(T obj, String message) Checks that the specified object reference is notnulland throws a customizedNullPointerExceptionif it is.static <T> TrequireNonNull(T obj, Supplier<String> messageSupplier) Checks that the specified object reference is notnulland throws a customizedNullPointerExceptionif it is.
-
Method Details
-
requireNonNull
Checks that the specified object reference is notnulland throws a customizedNullPointerExceptionif it is. This method is designed primarily for doing parameter validation in methods and constructors with multiple parameters, as demonstrated below:public Foo(Bar bar, Baz baz) { this.bar = Objects.requireNonNull(bar, "bar must not be null"); this.baz = Objects.requireNonNull(baz, "baz must not be null"); }Wrapper forObjects.requireNonNull(Object, String)that is annotated with SpotBugs annotations to correctly handle the null case.Will be removed when issue https://github.com/spotbugs/spotbugs/issues/456 is fixed
- Type Parameters:
T- the type of the reference- Parameters:
obj- the object reference to check for nullitymessage- detail message to be used in the event that aNullPointerExceptionis thrown- Returns:
objif notnull- Throws:
NullPointerException- ifobjisnull
-
requireNonNull
@NonNull public static <T> T requireNonNull(@CheckForNull T obj, @NonNull Supplier<String> messageSupplier) Checks that the specified object reference is notnulland throws a customizedNullPointerExceptionif it is.Unlike the method
requireNonNull(Object, String), this method allows creation of the message to be deferred until after the null check is made. While this may confer a performance advantage in the non-null case, when deciding to call this method care should be taken that the costs of creating the message supplier are less than the cost of just creating the string message directly. Wrapper forObjects.requireNonNull(Object, Supplier)that is annotated with SpotBugs annotations to correctly handle the null case.Will be removed when issue https://github.com/spotbugs/spotbugs/issues/456 is fixed
- Type Parameters:
T- the type of the reference- Parameters:
obj- the object reference to check for nullitymessageSupplier- detail message supplier to be used in the event that aNullPointerExceptionis thrown- Returns:
objif notnull- Throws:
NullPointerException- ifobjisnull
-