18b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira/*
28b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira * Copyright (C) 2010 Google Inc.
38b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira *
48b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira * Licensed under the Apache License, Version 2.0 (the "License");
58b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira * you may not use this file except in compliance with the License.
68b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira * You may obtain a copy of the License at
78b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira *
88b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira * http://www.apache.org/licenses/LICENSE-2.0
98b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira *
108b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira * Unless required by applicable law or agreed to in writing, software
118b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira * distributed under the License is distributed on an "AS IS" BASIS,
128b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira * See the License for the specific language governing permissions and
148b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira * limitations under the License.
158b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira */
168b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira
1730e2c24b056542f3b1b438aeb798305d1226d0c8Andy Huangpackage com.android.mail.lib.base;
188b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira
1930e2c24b056542f3b1b438aeb798305d1226d0c8Andy Huangimport static com.android.mail.lib.base.Preconditions.checkArgument;
2030e2c24b056542f3b1b438aeb798305d1226d0c8Andy Huangimport static com.android.mail.lib.base.Preconditions.checkNotNull;
218b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira
228b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereiraimport java.util.Formatter;
238b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira
248b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira/**
258b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira * Static utility methods pertaining to {@code String} or {@code CharSequence}
268b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira * instances.
278b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira *
288b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira * @author Kevin Bourrillion
298b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira * @since 3
308b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira */
318b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereirapublic final class Strings {
328b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  private Strings() {}
338b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira
348b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  /**
358b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * Returns the given string if it is non-null; the empty string otherwise.
368b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *
378b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @param string the string to test and possibly return
388b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @return {@code string} itself if it is non-null; {@code ""} if it is null
398b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   */
408b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  public static String nullToEmpty(String string) {
418b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    return (string == null) ? "" : string;
428b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  }
438b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira
448b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  /**
458b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * Returns the given string if it is nonempty; {@code null} otherwise.
468b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *
478b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @param string the string to test and possibly return
488b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @return {@code string} itself if it is nonempty; {@code null} if it is
498b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *     empty or null
508b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   */
518b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  public static String emptyToNull(String string) {
528b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    return isNullOrEmpty(string) ? null : string;
538b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  }
548b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira
558b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  /**
568b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * Returns {@code true} if the given string is null or is the empty string.
578b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *
588b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * <p>Consider normalizing your string references with {@link #nullToEmpty}.
598b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * If you do, you can use {@link String#isEmpty()} instead of this
608b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * method, and you won't need special null-safe forms of methods like {@link
618b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * String#toUpperCase} either. Or, if you'd like to normalize "in the other
628b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * direction," converting empty strings to {@code null}, you can use {@link
638b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * #emptyToNull}.
648b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *
658b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @param string a string reference to check
668b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @return {@code true} if the string is null or is the empty string
678b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   */
688b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  public static boolean isNullOrEmpty(String string) {
698b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    return string == null || string.length() == 0; // string.isEmpty() in Java 6
708b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  }
718b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira
728b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  /**
738b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * Returns a string, of length at least {@code minLength}, consisting of
748b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * {@code string} prepended with as many copies of {@code padChar} as are
758b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * necessary to reach that length. For example,
768b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *
778b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * <ul>
788b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * <li>{@code padStart("7", 3, '0')} returns {@code "007"}
798b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * <li>{@code padStart("2010", 3, '0')} returns {@code "2010"}
808b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * </ul>
818b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *
828b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * <p>See {@link Formatter} for a richer set of formatting capabilities.
838b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *
848b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @param string the string which should appear at the end of the result
858b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @param minLength the minimum length the resulting string must have. Can be
868b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *     zero or negative, in which case the input string is always returned.
878b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @param padChar the character to insert at the beginning of the result until
888b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *     the minimum length is reached
898b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @return the padded string
908b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   */
918b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  public static String padStart(String string, int minLength, char padChar) {
928b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    checkNotNull(string);  // eager for GWT.
938b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    if (string.length() >= minLength) {
948b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira      return string;
958b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    }
968b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    StringBuilder sb = new StringBuilder(minLength);
978b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    for (int i = string.length(); i < minLength; i++) {
988b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira      sb.append(padChar);
998b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    }
1008b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    sb.append(string);
1018b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    return sb.toString();
1028b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  }
1038b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira
1048b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  /**
1058b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * Returns a string, of length at least {@code minLength}, consisting of
1068b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * {@code string} appended with as many copies of {@code padChar} as are
1078b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * necessary to reach that length. For example,
1088b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *
1098b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * <ul>
1108b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * <li>{@code padEnd("4.", 5, '0')} returns {@code "4.000"}
1118b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * <li>{@code padEnd("2010", 3, '!')} returns {@code "2010"}
1128b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * </ul>
1138b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *
1148b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * <p>See {@link Formatter} for a richer set of formatting capabilities.
1158b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *
1168b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @param string the string which should appear at the beginning of the result
1178b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @param minLength the minimum length the resulting string must have. Can be
1188b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *     zero or negative, in which case the input string is always returned.
1198b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @param padChar the character to append to the end of the result until the
1208b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *     minimum length is reached
1218b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @return the padded string
1228b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   */
1238b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  public static String padEnd(String string, int minLength, char padChar) {
1248b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    checkNotNull(string);  // eager for GWT.
1258b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    if (string.length() >= minLength) {
1268b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira      return string;
1278b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    }
1288b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    StringBuilder sb = new StringBuilder(minLength);
1298b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    sb.append(string);
1308b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    for (int i = string.length(); i < minLength; i++) {
1318b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira      sb.append(padChar);
1328b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    }
1338b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    return sb.toString();
1348b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  }
1358b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira
1368b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  /**
1378b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * Returns a string consisting of a specific number of concatenated copies of
1388b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * an input string. For example, {@code repeat("hey", 3)} returns the string
1398b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * {@code "heyheyhey"}.
1408b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *
1418b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @param string any non-null string
1428b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @param count the number of times to repeat it; a nonnegative integer
1438b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @return a string containing {@code string} repeated {@code count} times
1448b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   *     (the empty string if {@code count} is zero)
1458b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   * @throws IllegalArgumentException if {@code count} is negative
1468b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira   */
1478b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  public static String repeat(String string, int count) {
1488b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    checkNotNull(string);  // eager for GWT.
1498b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    checkArgument(count >= 0, "invalid count: %s", count);
1508b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira
1518b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    // If this multiplication overflows, a NegativeArraySizeException or
1528b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    // OutOfMemoryError is not far behind
1538b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    StringBuilder builder = new StringBuilder(string.length() * count);
1548b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    for (int i = 0; i < count; i++) {
1558b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira      builder.append(string);
1568b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    }
1578b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira    return builder.toString();
1588b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira  }
1598b99ba451db6973978e60f91da2199686a9c85e7Mindy Pereira}
160