Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      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.android.dialer.util;
     18 
     19 import android.support.annotation.Nullable;
     20 import android.text.TextUtils;
     21 
     22 /** Static utility methods for Strings. */
     23 public class MoreStrings {
     24 
     25   /**
     26    * Returns the given string if it is non-null; the empty string otherwise.
     27    *
     28    * @param string the string to test and possibly return
     29    * @return {@code string} itself if it is non-null; {@code ""} if it is null
     30    */
     31   public static String nullToEmpty(@Nullable String string) {
     32     return (string == null) ? "" : string;
     33   }
     34 
     35   /**
     36    * Returns the given string if it is nonempty; {@code null} otherwise.
     37    *
     38    * @param string the string to test and possibly return
     39    * @return {@code string} itself if it is nonempty; {@code null} if it is empty or null
     40    */
     41   @Nullable
     42   public static String emptyToNull(@Nullable String string) {
     43     return TextUtils.isEmpty(string) ? null : string;
     44   }
     45 
     46   public static String toSafeString(String value) {
     47     if (value == null) {
     48       return null;
     49     }
     50 
     51     // Do exactly same thing as Uri#toSafeString() does, which will enable us to compare
     52     // sanitized phone numbers.
     53     final StringBuilder builder = new StringBuilder();
     54     for (int i = 0; i < value.length(); i++) {
     55       final char c = value.charAt(i);
     56       if (c == '-' || c == '@' || c == '.') {
     57         builder.append(c);
     58       } else {
     59         builder.append('x');
     60       }
     61     }
     62     return builder.toString();
     63   }
     64 }
     65