Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2010 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.android.mail.common.base;
     18 
     19 import static com.google.android.mail.common.base.Preconditions.checkArgument;
     20 import static com.google.android.mail.common.base.Preconditions.checkNotNull;
     21 
     22 import java.util.Formatter;
     23 
     24 /**
     25  * Static utility methods pertaining to {@code String} or {@code CharSequence}
     26  * instances.
     27  *
     28  * @author Kevin Bourrillion
     29  * @since 3
     30  */
     31 public final class Strings {
     32   private Strings() {}
     33 
     34   /**
     35    * Returns the given string if it is non-null; the empty string otherwise.
     36    *
     37    * @param string the string to test and possibly return
     38    * @return {@code string} itself if it is non-null; {@code ""} if it is null
     39    */
     40   public static String nullToEmpty(String string) {
     41     return (string == null) ? "" : string;
     42   }
     43 
     44   /**
     45    * Returns the given string if it is nonempty; {@code null} otherwise.
     46    *
     47    * @param string the string to test and possibly return
     48    * @return {@code string} itself if it is nonempty; {@code null} if it is
     49    *     empty or null
     50    */
     51   public static String emptyToNull(String string) {
     52     return isNullOrEmpty(string) ? null : string;
     53   }
     54 
     55   /**
     56    * Returns {@code true} if the given string is null or is the empty string.
     57    *
     58    * <p>Consider normalizing your string references with {@link #nullToEmpty}.
     59    * If you do, you can use {@link String#isEmpty()} instead of this
     60    * method, and you won't need special null-safe forms of methods like {@link
     61    * String#toUpperCase} either. Or, if you'd like to normalize "in the other
     62    * direction," converting empty strings to {@code null}, you can use {@link
     63    * #emptyToNull}.
     64    *
     65    * @param string a string reference to check
     66    * @return {@code true} if the string is null or is the empty string
     67    */
     68   public static boolean isNullOrEmpty(String string) {
     69     return string == null || string.length() == 0; // string.isEmpty() in Java 6
     70   }
     71 
     72   /**
     73    * Returns a string, of length at least {@code minLength}, consisting of
     74    * {@code string} prepended with as many copies of {@code padChar} as are
     75    * necessary to reach that length. For example,
     76    *
     77    * <ul>
     78    * <li>{@code padStart("7", 3, '0')} returns {@code "007"}
     79    * <li>{@code padStart("2010", 3, '0')} returns {@code "2010"}
     80    * </ul>
     81    *
     82    * <p>See {@link Formatter} for a richer set of formatting capabilities.
     83    *
     84    * @param string the string which should appear at the end of the result
     85    * @param minLength the minimum length the resulting string must have. Can be
     86    *     zero or negative, in which case the input string is always returned.
     87    * @param padChar the character to insert at the beginning of the result until
     88    *     the minimum length is reached
     89    * @return the padded string
     90    */
     91   public static String padStart(String string, int minLength, char padChar) {
     92     checkNotNull(string);  // eager for GWT.
     93     if (string.length() >= minLength) {
     94       return string;
     95     }
     96     StringBuilder sb = new StringBuilder(minLength);
     97     for (int i = string.length(); i < minLength; i++) {
     98       sb.append(padChar);
     99     }
    100     sb.append(string);
    101     return sb.toString();
    102   }
    103 
    104   /**
    105    * Returns a string, of length at least {@code minLength}, consisting of
    106    * {@code string} appended with as many copies of {@code padChar} as are
    107    * necessary to reach that length. For example,
    108    *
    109    * <ul>
    110    * <li>{@code padEnd("4.", 5, '0')} returns {@code "4.000"}
    111    * <li>{@code padEnd("2010", 3, '!')} returns {@code "2010"}
    112    * </ul>
    113    *
    114    * <p>See {@link Formatter} for a richer set of formatting capabilities.
    115    *
    116    * @param string the string which should appear at the beginning of the result
    117    * @param minLength the minimum length the resulting string must have. Can be
    118    *     zero or negative, in which case the input string is always returned.
    119    * @param padChar the character to append to the end of the result until the
    120    *     minimum length is reached
    121    * @return the padded string
    122    */
    123   public static String padEnd(String string, int minLength, char padChar) {
    124     checkNotNull(string);  // eager for GWT.
    125     if (string.length() >= minLength) {
    126       return string;
    127     }
    128     StringBuilder sb = new StringBuilder(minLength);
    129     sb.append(string);
    130     for (int i = string.length(); i < minLength; i++) {
    131       sb.append(padChar);
    132     }
    133     return sb.toString();
    134   }
    135 
    136   /**
    137    * Returns a string consisting of a specific number of concatenated copies of
    138    * an input string. For example, {@code repeat("hey", 3)} returns the string
    139    * {@code "heyheyhey"}.
    140    *
    141    * @param string any non-null string
    142    * @param count the number of times to repeat it; a nonnegative integer
    143    * @return a string containing {@code string} repeated {@code count} times
    144    *     (the empty string if {@code count} is zero)
    145    * @throws IllegalArgumentException if {@code count} is negative
    146    */
    147   public static String repeat(String string, int count) {
    148     checkNotNull(string);  // eager for GWT.
    149     checkArgument(count >= 0, "invalid count: %s", count);
    150 
    151     // If this multiplication overflows, a NegativeArraySizeException or
    152     // OutOfMemoryError is not far behind
    153     StringBuilder builder = new StringBuilder(string.length() * count);
    154     for (int i = 0; i < count; i++) {
    155       builder.append(string);
    156     }
    157     return builder.toString();
    158   }
    159 }
    160