From 5501fcae57188560b0fea61a51b3efc5b0313be5 Mon Sep 17 00:00:00 2001 From: Benedict Wong Date: Mon, 28 Oct 2019 19:03:05 -0700 Subject: [PATCH] Add message template support for checkStringNotEmpty This change adds the ability for the Preconditions#checkStringNotEmpty() to take a message template instead of just an error message, allowing for template reuse. Test: FrameworksNetTests passing Change-Id: I6df1c3bff77b227126c35f066ee9112e959d6b1c --- .../android/internal/util/Preconditions.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/core/java/com/android/internal/util/Preconditions.java b/core/java/com/android/internal/util/Preconditions.java index 731b93c18b093..3fff5c233890f 100644 --- a/core/java/com/android/internal/util/Preconditions.java +++ b/core/java/com/android/internal/util/Preconditions.java @@ -101,6 +101,24 @@ public class Preconditions { return string; } + /** + * Ensures that an string reference passed as a parameter to the calling method is not empty. + * + * @param string an string reference + * @param messageTemplate a printf-style message template to use if the check fails; will be + * converted to a string using {@link String#format(String, Object...)} + * @param messageArgs arguments for {@code messageTemplate} + * @return the string reference that was validated + * @throws IllegalArgumentException if {@code string} is empty + */ + public static @NonNull T checkStringNotEmpty( + final T string, final String messageTemplate, final Object... messageArgs) { + if (TextUtils.isEmpty(string)) { + throw new IllegalArgumentException(String.format(messageTemplate, messageArgs)); + } + return string; + } + /** * Ensures that an object reference passed as a parameter to the calling * method is not null.