1d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar/*
2d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * Copyright (C) 2015 The Android Open Source Project
3d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
4d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * you may not use this file except in compliance with the License.
5d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * You may obtain a copy of the License at
6d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
7d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * Unless required by applicable law or agreed to in writing, software
8d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
9d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * See the License for the specific language governing permissions and
11d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar * limitations under the License.
12d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar */
13d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar
14fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountpackage android.databinding.tool.ext
15d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar
167b07818f07c28c6dec34ca2a9ab5f61e86afb493Yigit Boyarimport android.databinding.tool.expr.VersionProvider
17d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyarimport kotlin.properties.ReadOnlyProperty
1859229481aec5a284d322a2ca80dff836485feb0cYigit Boyarimport kotlin.reflect.KProperty
19d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar
20d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyarprivate class LazyExt<K, T>(private val initializer: (k : K) -> T) : ReadOnlyProperty<K, T> {
21d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar    private val mapping = hashMapOf<K, T>()
2259229481aec5a284d322a2ca80dff836485feb0cYigit Boyar    override fun getValue(thisRef: K, property: kotlin.reflect.KProperty<*>): T {
2359229481aec5a284d322a2ca80dff836485feb0cYigit Boyar        val t = mapping[thisRef]
24d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar        if (t != null) {
25d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar            return t
26d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar        }
27d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar        val result = initializer(thisRef)
28d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar        mapping.put(thisRef, result)
29d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar        return result
30d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar    }
31d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar}
32d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar
337b07818f07c28c6dec34ca2a9ab5f61e86afb493Yigit Boyarprivate class VersionedLazyExt<K, T>(private val initializer: (k : K) -> T) : ReadOnlyProperty<K, T> {
347b07818f07c28c6dec34ca2a9ab5f61e86afb493Yigit Boyar    private val mapping = hashMapOf<K, VersionedResult<T>>()
3559229481aec5a284d322a2ca80dff836485feb0cYigit Boyar
3659229481aec5a284d322a2ca80dff836485feb0cYigit Boyar    override fun getValue(thisRef: K, property: KProperty<*>): T {
3759229481aec5a284d322a2ca80dff836485feb0cYigit Boyar        val t = mapping[thisRef]
3859229481aec5a284d322a2ca80dff836485feb0cYigit Boyar        val version = if(thisRef is VersionProvider) thisRef.version else 1
397b07818f07c28c6dec34ca2a9ab5f61e86afb493Yigit Boyar        if (t != null && version == t.version) {
407b07818f07c28c6dec34ca2a9ab5f61e86afb493Yigit Boyar            return t.result
417b07818f07c28c6dec34ca2a9ab5f61e86afb493Yigit Boyar        }
427b07818f07c28c6dec34ca2a9ab5f61e86afb493Yigit Boyar        val result = initializer(thisRef)
437b07818f07c28c6dec34ca2a9ab5f61e86afb493Yigit Boyar        mapping.put(thisRef, VersionedResult(version, result))
447b07818f07c28c6dec34ca2a9ab5f61e86afb493Yigit Boyar        return result
457b07818f07c28c6dec34ca2a9ab5f61e86afb493Yigit Boyar    }
467b07818f07c28c6dec34ca2a9ab5f61e86afb493Yigit Boyar}
477b07818f07c28c6dec34ca2a9ab5f61e86afb493Yigit Boyar
487b07818f07c28c6dec34ca2a9ab5f61e86afb493Yigit Boyardata class VersionedResult<T>(val version : Int, val result : T)
497b07818f07c28c6dec34ca2a9ab5f61e86afb493Yigit Boyar
5059229481aec5a284d322a2ca80dff836485feb0cYigit Boyarfun <K, T> lazyProp(initializer: (k : K) -> T): ReadOnlyProperty<K, T> = LazyExt(initializer)
5159229481aec5a284d322a2ca80dff836485feb0cYigit Boyarfun <K, T> versionedLazy(initializer: (k : K) -> T): ReadOnlyProperty<K, T> = VersionedLazyExt(initializer)
52d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar
5371eb6908f2689bd17000237eb645896c36da0138George Mountpublic fun Class<*>.toJavaCode() : String {
5471eb6908f2689bd17000237eb645896c36da0138George Mount    if (name.startsWith('[')) {
5571eb6908f2689bd17000237eb645896c36da0138George Mount        val numArray = name.lastIndexOf('[') + 1;
5671eb6908f2689bd17000237eb645896c36da0138George Mount        val componentType : String;
5759229481aec5a284d322a2ca80dff836485feb0cYigit Boyar        when (name[numArray]) {
5871eb6908f2689bd17000237eb645896c36da0138George Mount            'Z' -> componentType = "boolean"
5971eb6908f2689bd17000237eb645896c36da0138George Mount            'B' -> componentType = "byte"
6071eb6908f2689bd17000237eb645896c36da0138George Mount            'C' -> componentType = "char"
6159229481aec5a284d322a2ca80dff836485feb0cYigit Boyar            'L' -> componentType = name.substring(numArray + 1, name.length - 1).replace('$', '.');
6271eb6908f2689bd17000237eb645896c36da0138George Mount            'D' -> componentType = "double"
6371eb6908f2689bd17000237eb645896c36da0138George Mount            'F' -> componentType = "float"
6471eb6908f2689bd17000237eb645896c36da0138George Mount            'I' -> componentType = "int"
6571eb6908f2689bd17000237eb645896c36da0138George Mount            'J' -> componentType = "long"
6671eb6908f2689bd17000237eb645896c36da0138George Mount            'S' -> componentType = "short"
6771eb6908f2689bd17000237eb645896c36da0138George Mount            else -> componentType = name.substring(numArray)
6871eb6908f2689bd17000237eb645896c36da0138George Mount        }
6971eb6908f2689bd17000237eb645896c36da0138George Mount        val arrayComp = name.substring(0, numArray).replace("[", "[]");
7071eb6908f2689bd17000237eb645896c36da0138George Mount        return componentType + arrayComp;
7171eb6908f2689bd17000237eb645896c36da0138George Mount    } else {
7271eb6908f2689bd17000237eb645896c36da0138George Mount        return name.replace("$", ".")
7371eb6908f2689bd17000237eb645896c36da0138George Mount    }
7471eb6908f2689bd17000237eb645896c36da0138George Mount}
75d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar
7659229481aec5a284d322a2ca80dff836485feb0cYigit Boyarpublic fun String.androidId() : String = this.split("/")[1]
77d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar
78d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyarpublic fun String.toCamelCase() : String {
7959229481aec5a284d322a2ca80dff836485feb0cYigit Boyar    val split = this.split("_")
8059229481aec5a284d322a2ca80dff836485feb0cYigit Boyar    if (split.size == 0) return ""
8159229481aec5a284d322a2ca80dff836485feb0cYigit Boyar    if (split.size == 1) return split[0].capitalize()
82d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar    return split.joinToCamelCase()
83d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar}
84d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar
85d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyarpublic fun String.toCamelCaseAsVar() : String {
8659229481aec5a284d322a2ca80dff836485feb0cYigit Boyar    val split = this.split("_")
8759229481aec5a284d322a2ca80dff836485feb0cYigit Boyar    if (split.size == 0) return ""
8859229481aec5a284d322a2ca80dff836485feb0cYigit Boyar    if (split.size == 1) return split[0]
89d7af42b29ddf22f0068f7496c5ac6f4f34b543b6Yigit Boyar    return split.joinToCamelCaseAsVar()
9043596c2b2997e40b709627419732100d78a62ff0Yigit Boyar}
9143596c2b2997e40b709627419732100d78a62ff0Yigit Boyar
9243596c2b2997e40b709627419732100d78a62ff0Yigit Boyarpublic fun String.br() : String =
93a6e4583962e19e8e93b4ca3f9fe3d34560b6d96cYigit Boyar    "BR.${if (this == "") "_all" else this}"
94