summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/datamodel/binding/ImmutableBindingRef.java
blob: 9a0a3d64324b042baff74f8e986de056e7b2de0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.messaging.datamodel.binding;

import com.android.messaging.util.Assert;

/**
 * A immutable wrapper around a Binding object. Callers can only access readonly methods like
 * getData(), isBound() and ensureBound() but not bind() and unbind(). This is used for MVC pattern
 * where both the View and the Controller needs access to a centrally bound Model object. The View
 * is the one that owns the bind/unbind logic of the Binding, whereas controller only serves as a
 * consumer.
 */
public class ImmutableBindingRef<T extends BindableData> extends BindingBase<T> {
    /**
     * The referenced, read-only binding object.
     */
    private final BindingBase<T> mBinding;

    /**
     * Hidden ctor.
     */
    ImmutableBindingRef(final BindingBase<T> binding) {
        mBinding = resolveBinding(binding);
    }

    @Override
    public T getData() {
        return mBinding.getData();
    }

    @Override
    public boolean isBound() {
        return mBinding.isBound();
    }

    @Override
    public boolean isBound(final T data) {
        return mBinding.isBound(data);
    }

    @Override
    public void ensureBound() {
        mBinding.ensureBound();
    }

    @Override
    public void ensureBound(final T data) {
        mBinding.ensureBound(data);
    }

    @Override
    public String getBindingId() {
        return mBinding.getBindingId();
    }

    /**
     * Resolve the source binding to the real BindingImpl it's referencing. This avoids the
     * redundancy of multiple wrapper calls when creating a binding reference from an existing
     * binding reference.
     */
    private BindingBase<T> resolveBinding(final BindingBase<T> binding) {
        BindingBase<T> resolvedBinding = binding;
        while (resolvedBinding instanceof ImmutableBindingRef<?>) {
            resolvedBinding = ((ImmutableBindingRef<T>) resolvedBinding).mBinding;
        }
        Assert.isTrue(resolvedBinding instanceof Binding<?>);
        return resolvedBinding;
    }
}