GenericConverter.java revision bd42d20f70b1f88e27e3b3c9c3a9c55ec155d128
1/*
2 * Copyright (C) 2015 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 */
16package android.databinding.testapp.adapter;
17
18import android.databinding.BindingConversion;
19import java.util.ArrayList;
20import java.util.LinkedList;
21import java.util.List;
22
23public class GenericConverter {
24    @BindingConversion
25    public static <T> String convertArrayList(ArrayList<T> values) {
26        return convert(values);
27    }
28
29    @BindingConversion
30    public static String convertLinkedList(LinkedList<?> values) {
31        return convert(values);
32    }
33
34    private static <T> String convert(List<T> values) {
35        if (values == null) {
36            return "";
37        }
38        StringBuilder vals = new StringBuilder();
39        for (T val : values) {
40            if (vals.length() != 0) {
41                vals.append(' ');
42            }
43            vals.append(val);
44        }
45        return vals.toString();
46    }
47}
48