Home | History | Annotate | Download | only in binding
      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 
     17 package com.android.messaging.datamodel.binding;
     18 
     19 import java.util.concurrent.atomic.AtomicLong;
     20 
     21 public class Binding<T extends BindableData> extends BindingBase<T> {
     22     private static AtomicLong sBindingIdx = new AtomicLong(System.currentTimeMillis() * 1000);
     23 
     24     private String mBindingId;
     25     private T mData;
     26     private final Object mOwner;
     27     private boolean mWasBound;
     28 
     29     /**
     30      * Initialize a binding instance - the owner is typically the containing class
     31      */
     32     Binding(final Object owner) {
     33         mOwner = owner;
     34     }
     35 
     36     @Override
     37     public T getData() {
     38         ensureBound();
     39         return mData;
     40     }
     41 
     42     @Override
     43     public boolean isBound() {
     44         return (mData != null && mData.isBound(mBindingId));
     45     }
     46 
     47     @Override
     48     public boolean isBound(final T data) {
     49         return (isBound() && data == mData);
     50     }
     51 
     52     @Override
     53     public void ensureBound() {
     54         if (!isBound()) {
     55             throw new IllegalStateException("not bound; wasBound = " + mWasBound);
     56         }
     57     }
     58 
     59     @Override
     60     public void ensureBound(final T data) {
     61         if (!isBound()) {
     62             throw new IllegalStateException("not bound; wasBound = " + mWasBound);
     63         } else if (data != mData) {
     64             throw new IllegalStateException("not bound to correct data " + data + " vs " + mData);
     65         }
     66     }
     67 
     68     @Override
     69     public String getBindingId() {
     70         return mBindingId;
     71     }
     72 
     73     public void bind(final T data) {
     74         // Check both this binding and the data not already bound
     75         if (mData != null || data.isBound()) {
     76             throw new IllegalStateException("already bound when binding to " + data);
     77         }
     78         // Generate a unique identifier for this bind call
     79         mBindingId = Long.toHexString(sBindingIdx.getAndIncrement());
     80         data.bind(mBindingId);
     81         mData = data;
     82         mWasBound = true;
     83     }
     84 
     85     public void unbind() {
     86         // Check this binding is bound and that data is bound to this binding
     87         if (mData == null || !mData.isBound(mBindingId)) {
     88             throw new IllegalStateException("not bound when unbind");
     89         }
     90         mData.unbind(mBindingId);
     91         mData = null;
     92         mBindingId = null;
     93     }
     94 }
     95