1 /*
<lambda>null2  * 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 @file:JvmName("StringUtil")
18 @file:RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX) // used in generated code
19 
20 package androidx.room.util
21 
22 import androidx.annotation.RestrictTo
23 import kotlin.jvm.JvmField
24 import kotlin.jvm.JvmName
25 
26 @Suppress("unused")
27 @JvmField
28 @Deprecated("No longer used by generated code")
29 val EMPTY_STRING_ARRAY = arrayOfNulls<String>(0)
30 
31 /**
32  * Returns a new StringBuilder to be used while producing SQL queries.
33  *
34  * @return A new or recycled StringBuilder
35  */
36 @Deprecated("No longer used by generated code")
37 fun newStringBuilder(): StringBuilder = StringBuilder()
38 
39 /**
40  * Adds bind variable placeholders (?) to the given string. Each placeholder is separated by a
41  * comma.
42  *
43  * @param builder The StringBuilder for the query
44  * @param count Number of placeholders
45  */
46 fun appendPlaceholders(builder: StringBuilder, count: Int) {
47     for (i in 0 until count) {
48         builder.append("?")
49         if (i < count - 1) {
50             builder.append(",")
51         }
52     }
53 }
54 
55 /**
56  * Splits a comma separated list of integers to integer list.
57  *
58  * If an input is malformed, it is omitted from the result.
59  *
60  * @param input Comma separated list of integers.
61  * @return A List containing the integers or null if the input is null.
62  */
splitToIntListnull63 fun splitToIntList(input: String?): List<Int>? {
64     return input?.split(',')?.mapNotNull { item ->
65         try {
66             item.toInt()
67         } catch (ex: NumberFormatException) {
68             null
69         }
70     }
71 }
72 
73 /**
74  * Joins the given list of integers into a comma separated list.
75  *
76  * @param input The list of integers.
77  * @return Comma separated string composed of integers in the list. If the list is null, return
78  *   value is null.
79  */
joinIntoStringnull80 fun joinIntoString(input: List<Int>?): String? {
81     return input?.joinToString(",")
82 }
83