Merge "Refactor TextUtils.join() and add documentation"
diff --git a/core/java/android/text/TextUtils.java b/core/java/android/text/TextUtils.java
index 440c88e..f981e74 100644
--- a/core/java/android/text/TextUtils.java
+++ b/core/java/android/text/TextUtils.java
@@ -297,37 +297,46 @@
/**
* Returns a string containing the tokens joined by delimiters.
- * @param tokens an array objects to be joined. Strings will be formed from
- * the objects by calling object.toString().
+ *
+ * @param delimiter a CharSequence that will be inserted between the tokens. If null, the string
+ * "null" will be used as the delimiter.
+ * @param tokens an array objects to be joined. Strings will be formed from the objects by
+ * calling object.toString(). If tokens is null, a NullPointerException will be thrown. If
+ * tokens is an empty array, an empty string will be returned.
*/
- public static String join(CharSequence delimiter, Object[] tokens) {
- StringBuilder sb = new StringBuilder();
- boolean firstTime = true;
- for (Object token: tokens) {
- if (firstTime) {
- firstTime = false;
- } else {
- sb.append(delimiter);
- }
- sb.append(token);
+ public static String join(@NonNull CharSequence delimiter, @NonNull Object[] tokens) {
+ final int length = tokens.length;
+ if (length == 0) {
+ return "";
+ }
+ final StringBuilder sb = new StringBuilder();
+ sb.append(tokens[0]);
+ for (int i = 1; i < length; i++) {
+ sb.append(delimiter);
+ sb.append(tokens[i]);
}
return sb.toString();
}
/**
* Returns a string containing the tokens joined by delimiters.
- * @param tokens an array objects to be joined. Strings will be formed from
- * the objects by calling object.toString().
+ *
+ * @param delimiter a CharSequence that will be inserted between the tokens. If null, the string
+ * "null" will be used as the delimiter.
+ * @param tokens an array objects to be joined. Strings will be formed from the objects by
+ * calling object.toString(). If tokens is null, a NullPointerException will be thrown. If
+ * tokens is empty, an empty string will be returned.
*/
- public static String join(CharSequence delimiter, Iterable tokens) {
- StringBuilder sb = new StringBuilder();
- Iterator<?> it = tokens.iterator();
- if (it.hasNext()) {
+ public static String join(@NonNull CharSequence delimiter, @NonNull Iterable tokens) {
+ final Iterator<?> it = tokens.iterator();
+ if (!it.hasNext()) {
+ return "";
+ }
+ final StringBuilder sb = new StringBuilder();
+ sb.append(it.next());
+ while (it.hasNext()) {
+ sb.append(delimiter);
sb.append(it.next());
- while (it.hasNext()) {
- sb.append(delimiter);
- sb.append(it.next());
- }
}
return sb.toString();
}
diff --git a/graphics/java/android/graphics/fonts/FontVariationAxis.java b/graphics/java/android/graphics/fonts/FontVariationAxis.java
index 99564fa..1b7408a 100644
--- a/graphics/java/android/graphics/fonts/FontVariationAxis.java
+++ b/graphics/java/android/graphics/fonts/FontVariationAxis.java
@@ -178,7 +178,7 @@
* @return String a valid font variation settings string.
*/
public static @NonNull String toFontVariationSettings(@Nullable FontVariationAxis[] axes) {
- if (axes == null || axes.length == 0) {
+ if (axes == null) {
return "";
}
return TextUtils.join(",", axes);