1 /* 2 * Copyright (C) 2016 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 androidx.room.util; 18 19 import android.util.Log; 20 21 import androidx.annotation.Nullable; 22 import androidx.annotation.RestrictTo; 23 24 import java.util.ArrayList; 25 import java.util.List; 26 import java.util.StringTokenizer; 27 28 /** 29 * @hide 30 * 31 * String utilities for Room 32 */ 33 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 34 public class StringUtil { 35 36 @SuppressWarnings("unused") 37 public static final String[] EMPTY_STRING_ARRAY = new String[0]; 38 /** 39 * Returns a new StringBuilder to be used while producing SQL queries. 40 * 41 * @return A new or recycled StringBuilder 42 */ newStringBuilder()43 public static StringBuilder newStringBuilder() { 44 // TODO pool: 45 return new StringBuilder(); 46 } 47 48 /** 49 * Adds bind variable placeholders (?) to the given string. Each placeholder is separated 50 * by a comma. 51 * 52 * @param builder The StringBuilder for the query 53 * @param count Number of placeholders 54 */ appendPlaceholders(StringBuilder builder, int count)55 public static void appendPlaceholders(StringBuilder builder, int count) { 56 for (int i = 0; i < count; i++) { 57 builder.append("?"); 58 if (i < count - 1) { 59 builder.append(","); 60 } 61 } 62 } 63 /** 64 * Splits a comma separated list of integers to integer list. 65 * <p> 66 * If an input is malformed, it is omitted from the result. 67 * 68 * @param input Comma separated list of integers. 69 * @return A List containing the integers or null if the input is null. 70 */ 71 @Nullable splitToIntList(@ullable String input)72 public static List<Integer> splitToIntList(@Nullable String input) { 73 if (input == null) { 74 return null; 75 } 76 List<Integer> result = new ArrayList<>(); 77 StringTokenizer tokenizer = new StringTokenizer(input, ","); 78 while (tokenizer.hasMoreElements()) { 79 final String item = tokenizer.nextToken(); 80 try { 81 result.add(Integer.parseInt(item)); 82 } catch (NumberFormatException ex) { 83 Log.e("ROOM", "Malformed integer list", ex); 84 } 85 } 86 return result; 87 } 88 89 /** 90 * Joins the given list of integers into a comma separated list. 91 * 92 * @param input The list of integers. 93 * @return Comma separated string composed of integers in the list. If the list is null, return 94 * value is null. 95 */ 96 @Nullable joinIntoString(@ullable List<Integer> input)97 public static String joinIntoString(@Nullable List<Integer> input) { 98 if (input == null) { 99 return null; 100 } 101 102 final int size = input.size(); 103 if (size == 0) { 104 return ""; 105 } 106 StringBuilder sb = new StringBuilder(); 107 for (int i = 0; i < size; i++) { 108 sb.append(Integer.toString(input.get(i))); 109 if (i < size - 1) { 110 sb.append(","); 111 } 112 } 113 return sb.toString(); 114 } 115 StringUtil()116 private StringUtil() { 117 } 118 } 119