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 */ 16package android.databinding.tool.writer; 17 18import android.databinding.tool.reflection.ModelAnalyzer; 19import android.databinding.tool.store.SetterStore; 20 21import java.util.List; 22import java.util.Map; 23 24public class ComponentWriter { 25 private static final String INDENT = " "; 26 27 public ComponentWriter() { 28 } 29 30 public String createComponent() { 31 final StringBuilder builder = new StringBuilder(); 32 builder.append("package android.databinding;\n\n"); 33 builder.append("public interface DataBindingComponent {\n"); 34 final SetterStore setterStore = SetterStore.get(ModelAnalyzer.getInstance()); 35 Map<String, List<String>> bindingAdapters = setterStore.getComponentBindingAdapters(); 36 for (final String simpleName : bindingAdapters.keySet()) { 37 final List<String> classes = bindingAdapters.get(simpleName); 38 if (classes.size() > 1) { 39 int index = 1; 40 for (String className : classes) { 41 addGetter(builder, simpleName, className, index++); 42 } 43 } else { 44 addGetter(builder, simpleName, classes.iterator().next(), 0); 45 } 46 } 47 builder.append("}\n"); 48 return builder.toString(); 49 } 50 51 private static void addGetter(StringBuilder builder, String simpleName, String className, 52 int index) { 53 builder.append(INDENT) 54 .append(className) 55 .append(" get") 56 .append(simpleName); 57 if (index > 0) { 58 builder.append(index); 59 } 60 builder.append("();\n"); 61 } 62} 63