package jp.satorufujiwara.binder.recycler;

import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import jp.satorufujiwara.binder.Binder;
import jp.satorufujiwara.binder.ViewType;

class BaseRecyclerBinderAdapter<V extends ViewType, VH extends RecyclerView.ViewHolder>
        extends RecyclerView.Adapter<VH> {

    private final Object mLock = new Object();
    private final List<Binder<V, VH>> mObjects = new ArrayList<>();

    @Override
    public VH onCreateViewHolder(final ViewGroup parent, final int viewType) {
        return getItemByViewType(viewType).onCreateViewHolder(parent);
    }

    @Override
    public void onBindViewHolder(final VH holder, final int position) {
        getItem(position).onBindViewHolder(holder, position);
    }

    @Override
    public void onViewRecycled(VH holder) {
        final int position = holder.getAdapterPosition();
        if (position != RecyclerView.NO_POSITION) {
            getItem(position).onViewRecycled(holder);
        }
    }

    @Override
    public int getItemCount() {
        return mObjects.size();
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).getViewType().viewType();
    }

    public Binder<V, VH> getItem(int position) {
        return mObjects.get(position);
    }

    private Binder<V, VH> getItemByViewType(final int viewType) {
        for (final Binder<V, VH> item : mObjects) {
            if (item.getViewType().viewType() == viewType) {
                return item;
            }
        }
        throw new IllegalStateException("binder doesn't exist in list.");
    }

    void insert(final Binder<V, VH> object, final int index) {
        synchronized (mLock) {
            mObjects.add(index, object);
        }
    }

    void remove(final Binder<V, VH> object) {
        synchronized (mLock) {
            object.onRemoved();
            mObjects.remove(object);
        }
    }

    void clear() {
        synchronized (mLock) {
            for (final Binder<V, VH> item : mObjects) {
                item.onRemoved();
            }
            mObjects.clear();
        }
    }

}

@@ -1005,33 +1005,114 @@
         
-getItem(position)
+final Binder%3CV, VH%3E item = getItem(position);%0A            if (item != null) %7B%0A                item
 .onViewR
@@ -1128,16 +1128,30 @@
 older);%0A
+            %7D%0A
         
@@ -1317,114 +1317,338 @@
     
-return getItem(position).getViewType().viewType();%0A    %7D%0A%0A    public Binder%3CV, VH%3E getItem(int position) %7B
+final Binder%3CV, VH%3E item = getItem(position);%0A        if (item != null) %7B%0A            return item.getViewType().viewType();%0A        %7D else %7B%0A            return RecyclerView.INVALID_TYPE;%0A        %7D%0A    %7D%0A%0A    public Binder%3CV, VH%3E getItem(int position) %7B%0A        if (mObjects.size() %3C= position) %7B%0A            return null;%0A        %7D
 %0A   

Add null check in base recycler binder.