ext.kt revision fead9ca09b117136b35bc5bf137340a754f9eddd
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *      http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
12 */
13
14package android.databinding.tool.ext
15
16import kotlin.properties.ReadOnlyProperty
17import kotlin.properties.Delegates
18import android.databinding.tool.ext.joinToCamelCase
19import android.databinding.tool.ext.joinToCamelCaseAsVar
20import android.databinding.tool.reflection.ModelAnalyzer
21import android.databinding.tool.reflection.ModelClass
22import android.databinding.tool.reflection.ModelAnalyzer
23
24private class LazyExt<K, T>(private val initializer: (k : K) -> T) : ReadOnlyProperty<K, T> {
25    private val mapping = hashMapOf<K, T>()
26    override fun get(thisRef: K, desc: PropertyMetadata): T {
27        val t = mapping.get(thisRef)
28        if (t != null) {
29            return t
30        }
31        val result = initializer(thisRef)
32        mapping.put(thisRef, result)
33        return result
34    }
35}
36
37fun Delegates.lazy<K, T>(initializer: (k : K) -> T): ReadOnlyProperty<K, T> = LazyExt(initializer)
38
39public fun Class<*>.toJavaCode() : String {
40    val name = getName();
41    if (name.startsWith('[')) {
42        val numArray = name.lastIndexOf('[') + 1;
43        val componentType : String;
44        when (name.charAt(numArray)) {
45            'Z' -> componentType = "boolean"
46            'B' -> componentType = "byte"
47            'C' -> componentType = "char"
48            'L' -> componentType = name.substring(numArray + 1, name.length() - 1).replace('$', '.');
49            'D' -> componentType = "double"
50            'F' -> componentType = "float"
51            'I' -> componentType = "int"
52            'J' -> componentType = "long"
53            'S' -> componentType = "short"
54            else -> componentType = name.substring(numArray)
55        }
56        val arrayComp = name.substring(0, numArray).replace("[", "[]");
57        return componentType + arrayComp;
58    } else {
59        return name.replace("$", ".")
60    }
61}
62
63public fun String.androidId() : String = this.split("/")[1]
64
65public fun String.toCamelCase() : String {
66    val split = this.split("_")
67    if (split.size == 0) return ""
68    if (split.size == 1) return split[0].capitalize()
69    return split.joinToCamelCase()
70}
71
72public fun String.toCamelCaseAsVar() : String {
73    val split = this.split("_")
74    if (split.size == 0) return ""
75    if (split.size == 1) return split[0]
76    return split.joinToCamelCaseAsVar()
77}
78
79public fun String.br() : String =
80    "BR.${if (this == "") "_all" else this}"
81