<commit_before>package com.lush.lib.adapter;

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

import com.lush.lib.listener.OnListItemClickListener;
import com.lush.view.holder.BaseViewHolder;

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

/**
 * @author <NAME>
 */
public abstract class BaseListAdapter<T> extends RecyclerView.Adapter<BaseViewHolder<T>>
{
	private List<T> mItems;
	private OnListItemClickListener<T> mListener;

	public BaseListAdapter(List<T> items, OnListItemClickListener<T> listener)
	{
		setItems(items);
		mListener = listener;
	}

	@Override
	public int getItemCount()
	{
		return mItems != null ? mItems.size() : 0;
	}

	public T getItem(int position)
	{
		return mItems != null ? mItems.get(position) : null;
	}

	public void setItems(List<T> items)
	{
		if (items != null)
		{
			mItems = new ArrayList<>(items);
			notifyDataSetChanged();
		}
	}

	public List<T> getItems()
	{
		if (mItems == null)
		{
			mItems = new ArrayList<>();
		}
		return mItems;
	}

	public void addItem(T item)
	{
		if (mItems == null)
		{
			mItems = new ArrayList<>();
		}
		mItems.add(item);
		notifyItemInserted(mItems.indexOf(item));
	}

	public void removeItem(T item)
	{
		if (mItems != null)
		{
			notifyItemRemoved(mItems.indexOf(item));
			mItems.remove(item);
		}
	}

	public T removeItem(int position)
	{
		if (mItems != null)
		{
			T item = mItems.remove(position);
			notifyItemRemoved(position);
			return item;
		}
		return null;
	}

	public void addItem(int position, T item)
	{
		getItems().add(position, item);
		notifyItemInserted(position);
	}

	public void moveItem(int fromPosition, int toPosition)
	{
		if (mItems != null)
		{
			T item = mItems.remove(fromPosition);
			mItems.add(toPosition, item);
			notifyItemMoved(fromPosition, toPosition);
		}
	}

	protected OnListItemClickListener<T> getListener()
	{
		return mListener;
	}

	protected int getPosition(T item)
	{
		if (mItems != null && mItems.size() > 0)
		{
			return mItems.indexOf(item);
		}
		return -1;
	}

	public void setItemsWithAnimation(List<T> items)
	{
		applyAndAnimateRemovals(items);
		applyAndAnimateAdditions(items);
		applyAndAnimateMovedItems(items);
		notifyItemRangeChanged(0, items.size());
	}

	private void applyAndAnimateRemovals(List<T> newItems)
	{
		for (int i = getItems().size() - 1; i >= 0; i--)
		{
			T item = getItems().get(i);
			if (!newItems.contains(item))
			{
				removeItem(i);
			}
		}
	}

	private void applyAndAnimateAdditions(List<T> newItems)
	{
		for (int i = 0, count = newItems.size(); i < count; i++)
		{
			T item = newItems.get(i);
			if (!getItems().contains(item))
			{
				addItem(i, item);
			}
		}
	}

	private void applyAndAnimateMovedItems(List<T> newItems)
	{
		for (int toPosition = newItems.size() - 1; toPosition >= 0; toPosition--)
		{
			T model = newItems.get(toPosition);
			final int fromPosition = getItems().indexOf(model);
			if (fromPosition >= 0 && fromPosition != toPosition)
			{
				moveItem(fromPosition, toPosition);
			}
		}
	}

	public void setListener(OnListItemClickListener<T> listener)
	{
		mListener = listener;
	}

	@Override
	public void onBindViewHolder(BaseViewHolder<T> holder, int position)
	{
		final T item = getItem(position);
		holder.bind(item);
		if (mListener != null)<commit_msg>Fixed NPE crashes when passing a null list into the setItemsWithAnimation method<commit_after>package com.lush.lib.adapter;

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

import com.lush.lib.listener.OnListItemClickListener;
import com.lush.view.holder.BaseViewHolder;

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

/**
 * @author <NAME>
 */
public abstract class BaseListAdapter<T> extends RecyclerView.Adapter<BaseViewHolder<T>>
{
	private List<T> mItems;
	private OnListItemClickListener<T> mListener;

	public BaseListAdapter(List<T> items, OnListItemClickListener<T> listener)
	{
		setItems(items);
		mListener = listener;
	}

	@Override
	public int getItemCount()
	{
		return mItems != null ? mItems.size() : 0;
	}

	public T getItem(int position)
	{
		return mItems != null ? mItems.get(position) : null;
	}

	public void setItems(List<T> items)
	{
		if (items != null)
		{
			mItems = new ArrayList<>(items);
			notifyDataSetChanged();
		}
	}

	public List<T> getItems()
	{
		if (mItems == null)
		{
			mItems = new ArrayList<>();
		}
		return mItems;
	}

	public void addItem(T item)
	{
		if (mItems == null)
		{
			mItems = new ArrayList<>();
		}
		mItems.add(item);
		notifyItemInserted(mItems.indexOf(item));
	}

	public void removeItem(T item)
	{
		if (mItems != null)
		{
			notifyItemRemoved(mItems.indexOf(item));
			mItems.remove(item);
		}
	}

	public T removeItem(int position)
	{
		if (mItems != null)
		{
			T item = mItems.remove(position);
			notifyItemRemoved(position);
			return item;
		}
		return null;
	}

	public void addItem(int position, T item)
	{
		getItems().add(position, item);
		notifyItemInserted(position);
	}

	public void moveItem(int fromPosition, int toPosition)
	{
		if (mItems != null)
		{
			T item = mItems.remove(fromPosition);
			mItems.add(toPosition, item);
			notifyItemMoved(fromPosition, toPosition);
		}
	}

	protected OnListItemClickListener<T> getListener()
	{
		return mListener;
	}

	protected int getPosition(T item)
	{
		if (mItems != null && mItems.size() > 0)
		{
			return mItems.indexOf(item);
		}
		return -1;
	}

	public void setItemsWithAnimation(List<T> items)
	{
		applyAndAnimateRemovals(items);
		applyAndAnimateAdditions(items);
		applyAndAnimateMovedItems(items);
		if (items != null)
		{
			notifyItemRangeChanged(0, items.size());
		}
	}

	private void applyAndAnimateRemovals(List<T> newItems)
	{
		for (int i = getItems().size() - 1; i >= 0; i--)
		{
			T item = getItems().get(i);
			if (!newItems.contains(item))
			{
				removeItem(i);
			}
		}
	}

	private void applyAndAnimateAdditions(List<T> newItems)
	{
		if (newItems != null)
		{
			for (int i = 0, count = newItems.size(); i < count; i++)
			{
				T item = newItems.get(i);
				if (!getItems().contains(item))
				{
					addItem(i, item);
				}
			}
		}
	}

	private void applyAndAnimateMovedItems(List<T> newItems)
	{
		if (newItems != null)
		{
			for (int toPosition = newItems.size() - 1; toPosition >= 0; toPosition--)
			{
				T model = newItems.get(toPosition);
				final int fromPosition = getItems().indexOf(model);
				if (fromPosition >= 0 && fromPosition != toPosition)
				{
					moveItem(fromPosition, toPosition);
				}
			}
		}
	}

	public void setListener(OnListItemClickListener<T> listener)
	{
		mListener = listener;
	}

	@Override
	public void onBindViewHolder(BaseViewHolder<T> holder, int position)
	{
		final T item = getItem(position);
		holder.bind(item);
		if (mListener != null)
{'fraction_non_alphanumeric': 0.13392330383480827, 'fraction_numerical': 0.0032448377581120944, 'mean_word_length': 3.768635724331927, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 51, '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': '416907', 'n_tokens_mistral': 2708, 'n_tokens_neox': 2408, 'n_words': 517}