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