Commit df8fb89a authored by Boris Sazonov's avatar Boris Sazonov Committed by Commit Bot

[Android] Add ApiCompatibilityUtils.requireNonNull

This CL adds ApiCompatibilityUtils.requireNonNull method that behaves
identically to Objects.requireNonNull added in API level 19 (see
https://developer.android.com/reference/java/util/Objects). This method
does what the name says - it ensures that passed object is not null.

Bug: None
Change-Id: Ia9a1b32f34569208f2eea67fc998fe1d3fca613d
Reviewed-on: https://chromium-review.googlesource.com/1097404
Commit-Queue: Boris Sazonov <bsazonov@chromium.org>
Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Cr-Commit-Position: refs/heads/master@{#567842}
parent 71845dc1
...@@ -31,6 +31,7 @@ import android.os.StatFs; ...@@ -31,6 +31,7 @@ import android.os.StatFs;
import android.os.StrictMode; import android.os.StrictMode;
import android.os.UserManager; import android.os.UserManager;
import android.provider.Settings; import android.provider.Settings;
import android.support.annotation.NonNull;
import android.text.Html; import android.text.Html;
import android.text.Spanned; import android.text.Spanned;
import android.view.View; import android.view.View;
...@@ -68,6 +69,29 @@ public class ApiCompatibilityUtils { ...@@ -68,6 +69,29 @@ public class ApiCompatibilityUtils {
return lhs == rhs ? 0 : lhs ? 1 : -1; return lhs == rhs ? 0 : lhs ? 1 : -1;
} }
/**
* Checks that the object reference is not null and throws NullPointerException if it is.
* See {@link Objects#requireNonNull} which is available since API level 19.
* @param obj The object to check
*/
@NonNull
public static <T> T requireNonNull(T obj) {
if (obj == null) throw new NullPointerException();
return obj;
}
/**
* Checks that the object reference is not null and throws NullPointerException if it is.
* See {@link Objects#requireNonNull} which is available since API level 19.
* @param obj The object to check
* @param message The message to put into NullPointerException
*/
@NonNull
public static <T> T requireNonNull(T obj, String message) {
if (obj == null) throw new NullPointerException(message);
return obj;
}
/** /**
* {@link String#getBytes()} but specifying UTF-8 as the encoding and capturing the resulting * {@link String#getBytes()} but specifying UTF-8 as the encoding and capturing the resulting
* UnsupportedEncodingException. * UnsupportedEncodingException.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment