Package org.apache.calcite.linq4j
Class Nullness
java.lang.Object
org.apache.calcite.linq4j.Nullness
The methods in this class allow to cast nullable reference to a non-nullable one.
This is an internal class, and it is not meant to be used as a public API.
The class enables to remove checker-qual runtime dependency, and helps IDEs to see
the resulting types of castNonNull
better.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends @Nullable Object>
@NonNull TcastNonNull
(@Nullable T ref) Allows you to treat a nullable type as non-nullable with no assertions.static <T> @NonNull T[]
castNonNullArray
(@Nullable T[] ts) Allows you to treat an array of nullable values as an array of non-nullable values.static <T> List<@NonNull T>
castNonNullList
(List<? extends @Nullable T> ts) Allows you to treat a list of nullable values as an list of non-nullable values.static <T> T
castToInitialized
(@UnderInitialization T ref) Allows you to treat an uninitialized or under-initialization object as initialized with no assertions.
-
Method Details
-
castNonNull
@Pure @EnsuresNonNull("#1") public static <T extends @Nullable Object> @NonNull T castNonNull(@Nullable T ref) Allows you to treat a nullable type as non-nullable with no assertions.It is useful in the case you have a nullable lately-initialized field like the following:
class Wrapper<T> { @Nullable T value; }
That signature allows you to use
Wrapper
with both nullable or non-nullable types:Wrapper<@Nullable Integer>
vsWrapper<Integer>
. Suppose you need to implementT get() { return value; }
The issue is checkerframework does not permit that because
T
has unknown nullability, so the following needs to be used:T get() { return sneakyNull(value); }
- Type Parameters:
T
- the type of the reference- Parameters:
ref
- a reference of @Nullable type, that is non-null at run time- Returns:
- the argument, cast to have the type qualifier @NonNull
-
castNonNullArray
@Pure public static <T> @NonNull T[] castNonNullArray(@Nullable T[] ts) Allows you to treat an array of nullable values as an array of non-nullable values.- Type Parameters:
T
- Type of the array elements- Parameters:
ts
- Array- Returns:
- the argument, cast so that elements are @NonNull
-
castNonNullList
Allows you to treat a list of nullable values as an list of non-nullable values.- Type Parameters:
T
- Type of the list elements- Parameters:
ts
- List- Returns:
- the argument, cast so that elements are @NonNull
-
castToInitialized
@Pure public static <T> T castToInitialized(@UnderInitialization T ref) Allows you to treat an uninitialized or under-initialization object as initialized with no assertions.- Type Parameters:
T
- The type of the reference- Parameters:
ref
- A reference that was @Uninitialized at some point but is now fully initialized- Returns:
- the argument, cast to have type qualifier @Initialized
-