MergedBinding.java revision e9b33bac04bb1ce1444d7f1744fcec1ecd3a57da
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 */
16
17package android.databinding.tool;
18
19import com.google.common.base.Function;
20import com.google.common.collect.Iterables;
21
22import org.apache.commons.lang3.StringUtils;
23
24import android.databinding.tool.expr.ArgListExpr;
25import android.databinding.tool.expr.Expr;
26import android.databinding.tool.expr.ExprModel;
27import android.databinding.tool.reflection.ModelAnalyzer;
28import android.databinding.tool.store.SetterStore;
29import android.databinding.tool.util.L;
30import android.databinding.tool.writer.CodeGenUtil;
31
32import java.util.List;
33
34/**
35 * Multiple binding expressions can be evaluated using a single adapter. In those cases,
36 * we replace the Binding with a MergedBinding.
37 */
38public class MergedBinding extends Binding {
39    private final SetterStore.MultiAttributeSetter mMultiAttributeSetter;
40    public MergedBinding(ExprModel model, SetterStore.MultiAttributeSetter multiAttributeSetter,
41            BindingTarget target, Iterable<Binding> bindings) {
42        super(target, createMergedName(bindings), createArgListExpr(model, bindings));
43        mMultiAttributeSetter = multiAttributeSetter;
44    }
45
46    private static Expr createArgListExpr(ExprModel model, final Iterable<Binding> bindings) {
47        Expr expr = model.argListExpr(Iterables.transform(bindings, new Function<Binding, Expr>() {
48            @Override
49            public Expr apply(Binding input) {
50                return input.getExpr();
51            }
52        }));
53        expr.setBindingExpression(true);
54        return expr;
55    }
56
57    private static String createMergedName(Iterable<Binding> bindings) {
58        return Iterables.toString(Iterables.transform(bindings, new Function<Binding, String>() {
59            @Override
60            public String apply(Binding input) {
61                return input.getName();
62            }
63        }));
64    }
65
66    @Override
67    public int getMinApi() {
68        return 1;
69    }
70
71    @Override
72    public String toJavaCode(String targetViewName) {
73        ArgListExpr args = (ArgListExpr) getExpr();
74        final String[] expressions = Iterables
75                .toArray(Iterables.transform(args.getChildren(), new Function<Expr, String>() {
76                    @Override
77                    public String apply(Expr input) {
78                        return CodeGenUtil.Companion.toCode(input, false).generate();
79                    }
80                }), String.class);
81        L.d("merged binding arg: %s", args.getUniqueKey());
82        return mMultiAttributeSetter.toJava(targetViewName, expressions);
83    }
84}
85