2 * This file is part of null-analysis. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of null-analysis,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
7 package de
.xn__ho_hia
.null_analysis
;
9 import java
.util
.Collection
;
10 import java
.util
.Collections
;
11 import java
.util
.List
;
14 import java
.util
.stream
.Stream
;
16 import org
.eclipse
.jdt
.annotation
.NonNull
;
17 import org
.eclipse
.jdt
.annotation
.Nullable
;
20 * Utility classes which helps working with legacy (nullable) APIs.
22 public final class Nullsafe
{
29 * Converts a {@link Nullable} reference into a {@link NonNull} reference. Performs a strict <code>null</code> check
30 * that fails in case a <code>null</code> reference is given.
33 * The type of the reference
35 * A possible <code>null</code> reference.
36 * @return Either the reference itself, or an {@link NullPointerException}, in case the reference was
39 public static <TYPE
> @NonNull TYPE
nonNull(@Nullable final TYPE reference
) {
40 return nonNull(reference
, "Got unexpected NULL reference"); //$NON-NLS-1$
44 * Converts a {@link Nullable} reference into a {@link NonNull} reference. Performs a strict <code>null</code> check
45 * that fails in case a <code>null</code> reference is given.
48 * The type of the reference
50 * A possible <code>null</code> reference.
52 * The exception message to throw.
53 * @return Either the reference itself, or an {@link NullPointerException}, in case the reference was
56 public static <T
> @NonNull T
nonNull(@Nullable final T reference
, final String message
) {
57 if (reference
!= null) {
61 throw new IllegalArgumentException(message
);
65 * Safely creates a {@link Stream stream} of a nullable {@link Collection collection}. Falls back to an empty stream
66 * in case the collection is <code>null</code>.
71 * A collection or <code>null</code>
72 * @return A stream of the given collection or empty stream
74 public static <TYPE
> @NonNull Stream
<TYPE
> safelyStream(@Nullable final Collection
<TYPE
> collection
) {
75 return collection
!= null ?
nonNull(collection
.stream()) : nonNull(Stream
.<TYPE
> empty());
79 * Ensures that callers of this method can always work with a {@link NonNull} {@link List list}. Falls back to an
80 * empty list in case the given list is <code>null</code>.
85 * A list or <code>null</code>
86 * @return The given list or an empty list
88 public static <TYPE
> @NonNull List
<TYPE
> ensureList(@Nullable final List
<TYPE
> list
) {
89 return list
!= null ? list
: nonNull(Collections
.<TYPE
> emptyList());
93 * Ensures that callers of this method can always work with a {@link NonNull} {@link Set set}. Falls back to an
94 * empty set in case the given set is <code>null</code>.
99 * A {@link Set} or <code>null</code>
100 * @return The given set or an empty set
102 public static <TYPE
> @NonNull Set
<TYPE
> ensureSet(@Nullable final Set
<TYPE
> set
) {
103 return set
!= null ? set
: nonNull(Collections
.<TYPE
> emptySet());
107 * Ensures that callers of this method can always work with a {@link NonNull} {@link Map map}. Falls back to an
108 * empty map in case the given map is <code>null</code>.
115 * A {@link Map} or <code>null</code>
116 * @return The given map or an empty map
118 public static <KEY
, VALUE
> @NonNull Map
<KEY
, VALUE
> ensureMap(@Nullable final Map
<KEY
, VALUE
> map
) {
119 return map
!= null ? map
: nonNull(Collections
.<KEY
, VALUE
> emptyMap());
123 * Safely checks whether a given {@link Collection collection} is <code>null</code> or empty.
126 * A collection or <code>null</code>
127 * @return <code>true</code> if the collection is null or empty, otherwise <code>false</code>.
129 public static boolean isNullOrEmpty(@Nullable final Collection
<?
> collection
) {
130 return collection
== null || collection
.isEmpty();
134 * Safely checks whether a given {@link Map map} is <code>null</code> or empty.
137 * A map or <code>null</code>
138 * @return <code>true</code> if the map is null or empty, otherwise <code>false</code>.
140 public static boolean isNullOrEmpty(@Nullable final Map
<?
, ?
> map
) {
141 return map
== null || map
.isEmpty();
145 * Safely checks whether a given {@link String string} is <code>null</code> or empty.
148 * A String or <code>null</code>
149 * @return <code>true</code> if the string is null or empty, otherwise <code>false</code>.
151 public static boolean isNullOrEmpty(@Nullable final String string
) {
152 return string
== null || string
.isEmpty();