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 */
16
17package android.databinding.tool.util;
18
19import android.databinding.tool.reflection.Callable;
20
21/**
22 * Central place to convert method/field names to BR observable fields
23 */
24public class BrNameUtil {
25    private static String stripPrefixFromField(String name) {
26        if (name.length() >= 2) {
27            char firstChar = name.charAt(0);
28            char secondChar = name.charAt(1);
29            if (name.length() > 2 && firstChar == 'm' && secondChar == '_') {
30                char thirdChar = name.charAt(2);
31                if (Character.isJavaIdentifierStart(thirdChar)) {
32                    return "" + Character.toLowerCase(thirdChar) +
33                            name.subSequence(3, name.length());
34                }
35            } else if ((firstChar == 'm' && Character.isUpperCase(secondChar)) ||
36                    (firstChar == '_' && Character.isJavaIdentifierStart(secondChar))) {
37                return "" + Character.toLowerCase(secondChar) + name.subSequence(2, name.length());
38            }
39        }
40        return name;
41    }
42
43    public static String brKey(Callable callable) {
44        if (callable.type == Callable.Type.FIELD) {
45            return stripPrefixFromField(callable.name);
46        }
47        CharSequence propertyName;
48        final String name = callable.name;
49        if (isGetter(callable) || isSetter(callable)) {
50            propertyName = name.subSequence(3, name.length());
51        } else if (isBooleanGetter(callable)) {
52            propertyName = name.subSequence(2, name.length());
53        } else {
54            L.e("@Bindable associated with method must follow JavaBeans convention %s", callable);
55            return null;
56        }
57        char firstChar = propertyName.charAt(0);
58        return "" + Character.toLowerCase(firstChar) +
59                propertyName.subSequence(1, propertyName.length());
60    }
61
62    private static boolean isGetter(Callable callable) {
63        return prefixes(callable.name, "get") &&
64                Character.isJavaIdentifierStart(callable.name.charAt(3)) &&
65                callable.getParameterCount() == 0 &&
66                !callable.resolvedType.isVoid();
67    }
68
69    private static boolean isSetter(Callable callable) {
70        return prefixes(callable.name, "set") &&
71                Character.isJavaIdentifierStart(callable.name.charAt(3)) &&
72                callable.getParameterCount() == 1 &&
73                callable.resolvedType.isVoid();
74    }
75
76    private static boolean isBooleanGetter(Callable callable) {
77        return prefixes(callable.name, "is") &&
78                Character.isJavaIdentifierStart(callable.name.charAt(2)) &&
79                callable.getParameterCount() == 0 &&
80                callable.resolvedType.isBoolean();
81    }
82
83    private static boolean prefixes(CharSequence sequence, String prefix) {
84        boolean prefixes = false;
85        if (sequence.length() > prefix.length()) {
86            int count = prefix.length();
87            prefixes = true;
88            for (int i = 0; i < count; i++) {
89                if (sequence.charAt(i) != prefix.charAt(i)) {
90                    prefixes = false;
91                    break;
92                }
93            }
94        }
95        return prefixes;
96    }
97}
98