14df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar/*
24df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar * Copyright (C) 2015 The Android Open Source Project
34df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar *
44df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
54df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar * you may not use this file except in compliance with the License.
64df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar * You may obtain a copy of the License at
74df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar *
84df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
94df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar *
104df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar * Unless required by applicable law or agreed to in writing, software
114df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
124df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar * See the License for the specific language governing permissions and
144df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar * limitations under the License.
154df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar */
164df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar
174df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyarpackage android.databinding.tool.writer
184df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar
194ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Guptaimport android.databinding.tool.util.StringUtils
204df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar
214df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyarclass BRWriter(properties: Set<String>, val useFinal : Boolean) {
2259229481aec5a284d322a2ca80dff836485feb0cYigit Boyar    val indexedProps = properties.sorted().withIndex()
234ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Gupta    public fun write(pkg : String): String = "package $pkg;${StringUtils.LINE_SEPARATOR}$klass"
2459229481aec5a284d322a2ca80dff836485feb0cYigit Boyar    val klass: String by lazy {
254df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar        kcode("") {
264df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar            val prefix = if (useFinal) "final " else "";
274df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar            nl("public class BR {") {
284df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar                tab("public static ${prefix}int _all = 0;")
294df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar                indexedProps.forEach {
304df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar                    tab ("public static ${prefix}int ${it.value} = ${it.index + 1};")
314df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar                }
324df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar            } nl ("}")
334df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar        }.generate()
344df4ba38a62b791bbbc25e923efe8d9c2f9a52e9Yigit Boyar    }
354ba16229a40e9758db86d4fb1df5119fdcb8aa2aDeepanshu Gupta}
36