| 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 |
|
| 14 |
package android.databinding.tool.ext |
| 15 |
|
| 16 |
import android.databinding.tool.expr.VersionProvider |
| 17 |
import kotlin.properties.ReadOnlyProperty |
| 18 |
import kotlin.reflect.KProperty |
| 19 |
|
| 20 |
private class LazyExt<K, T>(private val initializer: (k : K) -> T) : ReadOnlyProperty<K, T> { |
| 21 |
private val mapping = hashMapOf<K, T>() |
| 22 |
override fun getValue(thisRef: K, property: kotlin.reflect.KProperty<*>): T { |
| 23 |
val t = mapping[thisRef] |
| 24 |
if (t != null) { |
| 25 |
return t |
| 26 |
} |
| 27 |
val result = initializer(thisRef) |
| 28 |
mapping.put(thisRef, result) |
| 29 |
return result |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
private class VersionedLazyExt<K, T>(private val initializer: (k : K) -> T) : ReadOnlyProperty<K, T> { |
| 34 |
private val mapping = hashMapOf<K, VersionedResult<T>>() |
| 35 |
|
| 36 |
override fun getValue(thisRef: K, property: KProperty<*>): T { |
| 37 |
val t = mapping[thisRef] |
| 38 |
val version = if(thisRef is VersionProvider) thisRef.version else 1 |
| 39 |
if (t != null && version == t.version) { |
| 40 |
return t.result |
| 41 |
} |
| 42 |
val result = initializer(thisRef) |
| 43 |
mapping.put(thisRef, VersionedResult(version, result)) |
| 44 |
return result |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
data class VersionedResult<T>(val version : Int, val result : T) |
| 49 |
|
| 50 |
fun <K, T> lazyProp(initializer: (k : K) -> T): ReadOnlyProperty<K, T> = LazyExt(initializer) |
| 51 |
fun <K, T> versionedLazy(initializer: (k : K) -> T): ReadOnlyProperty<K, T> = VersionedLazyExt(initializer) |
| 52 |
|
| 53 |
public fun Class<*>.toJavaCode() : String { |
| 54 |
if (name.startsWith('[')) { |
| 55 |
val numArray = name.lastIndexOf('[') + 1; |
| 56 |
val componentType : String; |
| 57 |
when (name[numArray]) { |
| 58 |
'Z' -> componentType = "boolean" |
| 59 |
'B' -> componentType = "byte" |
| 60 |
'C' -> componentType = "char" |
| 61 |
'L' -> componentType = name.substring(numArray + 1, name.length - 1).replace('$', '.'); |
| 62 |
'D' -> componentType = "double" |
| 63 |
'F' -> componentType = "float" |
| 64 |
'I' -> componentType = "int" |
| 65 |
'J' -> componentType = "long" |
| 66 |
'S' -> componentType = "short" |
| 67 |
else -> componentType = name.substring(numArray) |
| 68 |
} |
| 69 |
val arrayComp = name.substring(0, numArray).replace("[", "[]"); |
| 70 |
return componentType + arrayComp; |
| 71 |
} else { |
| 72 |
return name.replace("$", ".") |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public fun String.androidId() : String = this.split("/")[1] |
| 77 |
|
| 78 |
public fun String.toCamelCase() : String { |
| 79 |
val split = this.split("_") |
| 80 |
if (split.size == 0) return "" |
| 81 |
if (split.size == 1) return split[0].capitalize() |
| 82 |
return split.joinToCamelCase() |
| 83 |
} |
| 84 |
|
| 85 |
public fun String.toCamelCaseAsVar() : String { |
| 86 |
val split = this.split("_") |
| 87 |
if (split.size == 0) return "" |
| 88 |
if (split.size == 1) return split[0] |
| 89 |
return split.joinToCamelCaseAsVar() |
| 90 |
} |
| 91 |
|
| 92 |
public fun String.br() : String = |
| 93 |
"BR.${if (this == "") "_all" else this}" |