<commit_before>package za.co.retrorabbit.habanero.firebase.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.StringRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.SectionIndexer;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.Query;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import za.co.retrorabbit.habanero.firebase.datatype.FireHashSet;

/**
 * Created by wsche on 2016/09/15.
 */
public abstract class FireListAdapter<T, VH extends FireListAdapter.ViewHolder> extends BaseAdapter implements Filterable, SectionIndexer {
    Query mQuery;
    Class<T> mModelClass;
    int mModelLayout;
    Class<VH> mViewHolderClass;
    FireHashSet<T> mData;
    String filterConstraint;
    Map<String, Integer> indicatorMap = new LinkedHashMap<>();
    String[] sections = new String[]{};

    public FireListAdapter(Class<T> mModelClass, int mModelLayout, Class<VH> mViewHolderClass, Query mQuery) {
        this.mModelClass = mModelClass;
        this.mModelLayout = mModelLayout;
        this.mViewHolderClass = mViewHolderClass;
        this.mQuery = mQuery;
        mData = new FireHashSet<>(mQuery, this.mModelClass);
        addListeners();
    }

    public FireListAdapter(Class<T> mModelClass, int mModelLayout, Class<VH> mViewHolderClass, DatabaseReference mReference) {
        this(mModelClass, mModelLayout, mViewHolderClass, (Query) mReference);
    }

    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewGroup view = (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
        try {
            Constructor<VH> constructor = mViewHolderClass.getConstructor(View.class);
            return constructor.newInstance(view);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public final void onBindViewHolder(VH viewHolder, int position) {
        populateViewHolder(viewHolder, getItem(position), position);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Filter getFilter() {
        return customFilter();
    }

    public Filter customFilter() {
        return null;
    }

    @Override
    public final View getView(int position, View convertView, ViewGroup parent) {
        VH viewHolder;
        if (convertView == null) {
            viewHolder = onCreateViewHolder(parent, getItemViewType(position));
            convertView = viewHolder.itemView;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (VH) convertView.getTag();
            convertView = viewHolder.itemView;
        }
        populateViewHolder(viewHolder, getItem(position), position);
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        return mModelLayout;
    }

    @Override
    public long getItemId(int position) {
        // http://stackoverflow.com/questions/5100071/whats-the-purpose-of-item-ids-in-android-listview-adapter
        return mData.getKey(position).hashCode();
    }

    public void cleanup() {
        mData.cleanup();
    }

    public DatabaseReference getRef(int position) {
        return mData.getReference().child(mData.getKey(position));
    }<commit_msg>Moved get item view resource to its own function<commit_after>package za.co.retrorabbit.habanero.firebase.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.StringRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.SectionIndexer;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.Query;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import za.co.retrorabbit.habanero.firebase.datatype.FireHashSet;

/**
 * Created by wsche on 2016/09/15.
 */
public abstract class FireListAdapter<T, VH extends FireListAdapter.ViewHolder> extends BaseAdapter implements Filterable, SectionIndexer {
    Query mQuery;
    Class<T> mModelClass;
    int mModelLayout;
    Class<VH> mViewHolderClass;
    FireHashSet<T> mData;
    String filterConstraint;
    Map<String, Integer> indicatorMap = new LinkedHashMap<>();
    String[] sections = new String[]{};

    public FireListAdapter(Class<T> mModelClass, int mModelLayout, Class<VH> mViewHolderClass, Query mQuery) {
        this.mModelClass = mModelClass;
        this.mModelLayout = mModelLayout;
        this.mViewHolderClass = mViewHolderClass;
        this.mQuery = mQuery;
        mData = new FireHashSet<>(mQuery, this.mModelClass);
        addListeners();
    }

    public FireListAdapter(Class<T> mModelClass, int mModelLayout, Class<VH> mViewHolderClass, DatabaseReference mReference) {
        this(mModelClass, mModelLayout, mViewHolderClass, (Query) mReference);
    }

    public VH onCreateViewHolder(ViewGroup parent, int viewResource) {
        ViewGroup view = (ViewGroup) LayoutInflater.from(parent.getContext()).inflate(viewResource, parent, false);
        try {
            Constructor<VH> constructor = mViewHolderClass.getConstructor(View.class);
            return constructor.newInstance(view);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public final void onBindViewHolder(VH viewHolder, int position) {
        populateViewHolder(viewHolder, getItem(position), position);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Filter getFilter() {
        return customFilter();
    }

    public Filter customFilter() {
        return null;
    }

    @Override
    public final View getView(int position, View convertView, ViewGroup parent) {
        VH viewHolder;
        if (convertView == null) {
            viewHolder = onCreateViewHolder(parent, getItemViewResource(position));
            convertView = viewHolder.itemView;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (VH) convertView.getTag();
            convertView = viewHolder.itemView;
        }
        populateViewHolder(viewHolder, getItem(position), position);
        return convertView;
    }

    public int getItemViewResource(int position) {
        return mModelLayout;
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    public long getItemId(int position) {
        // http://stackoverflow.com/questions/5100071/whats-the-purpose-of-item-ids-in-android-listview-adapter
        return mData.getKey(position).hashCode();
    }

    public void cleanup() {
        mData.cleanup();
    }

    public DatabaseReference getRef(int position) {
        return mData.getReference().child(mData.getKey(position));
    }
{'fraction_non_alphanumeric': 0.09475781347279293, 'fraction_numerical': 0.004109077325364214, 'mean_word_length': 3.2497354497354496, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3233821', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2225, 'n_words': 494}