<commit_before>/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * 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 me.drakeet.multitype;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;

/**
 * @author drakeet
 */
public class MultiTypeAdapter extends RecyclerView.Adapter<ViewHolder>
    implements FlatTypeAdapter, TypePool {

    protected final List<? extends Item> items;
    protected LayoutInflater inflater;
    protected TypePool delegate;


    public MultiTypeAdapter(@NonNull List<? extends Item> items) {
        this.delegate = new MultiTypePool();
        this.items = items;
    }


    public MultiTypeAdapter(@NonNull List<? extends Item> items, TypePool pool) {
        this.delegate = pool;
        this.items = items;
    }


    @NonNull @Override public Class onFlattenClass(@NonNull final Item item) {
        return item.getClass();
    }


    @NonNull @Override public Item onFlattenItem(@NonNull final Item item) {
        return item;
    }


    @SuppressWarnings("unchecked") @Override
    public int getItemViewType(int position) {
        Item item = items.get(position);
        return indexOf(onFlattenClass(item));
    }


    @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int indexViewType) {
        if (inflater == null) {
            inflater = LayoutInflater.from(parent.getContext());
        }
        return getProviderByIndex(indexViewType).onCreateViewHolder(inflater, parent);
    }


    @SuppressWarnings("unchecked") @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        int type = getItemViewType(position);
        Item item = items.get(position);
        getProviderByIndex(type).onBindViewHolder(holder, onFlattenItem(item));
    }


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


    public void applyGlobalMultiTypePool() {
        for (int i = 0; i < GlobalMultiTypePool.getContents().size(); i++) {
            final Class<? extends Item> clazz = GlobalMultiTypePool.getContents().get(i);
            final ItemViewProvider provider = GlobalMultiTypePool.getProviders().get(i);
            if (!this.getContents().contains(clazz)) {
                this.register(clazz, provider);
            }
        }<commit_msg>Let getProvider more intuitive (#32)<commit_after>/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * 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 me.drakeet.multitype;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;

/**
 * @author drakeet
 */
public class MultiTypeAdapter extends RecyclerView.Adapter<ViewHolder>
    implements FlatTypeAdapter, TypePool {

    protected final List<? extends Item> items;
    protected LayoutInflater inflater;
    protected TypePool delegate;


    public MultiTypeAdapter(@NonNull List<? extends Item> items) {
        this.delegate = new MultiTypePool();
        this.items = items;
    }


    public MultiTypeAdapter(@NonNull List<? extends Item> items, TypePool pool) {
        this.delegate = pool;
        this.items = items;
    }


    @NonNull @Override public Class onFlattenClass(@NonNull final Item item) {
        return item.getClass();
    }


    @NonNull @Override public Item onFlattenItem(@NonNull final Item item) {
        return item;
    }


    @SuppressWarnings("unchecked") @Override
    public int getItemViewType(int position) {
        Item item = items.get(position);
        return indexOf(onFlattenClass(item));
    }


    @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int indexViewType) {
        if (inflater == null) {
            inflater = LayoutInflater.from(parent.getContext());
        }
        return getProviderByIndex(indexViewType).onCreateViewHolder(inflater, parent);
    }


    @SuppressWarnings("unchecked") @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Item item = items.get(position);
        getProviderByClass(onFlattenClass(item)).onBindViewHolder(holder, onFlattenItem(item));
    }


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


    public void applyGlobalMultiTypePool() {
        for (int i = 0; i < GlobalMultiTypePool.getContents().size(); i++) {
            final Class<? extends Item> clazz = GlobalMultiTypePool.getContents().get(i);
            final ItemViewProvider provider = GlobalMultiTypePool.getProviders().get(i);
            if (!this.getContents().contains(clazz)) {
                this.register(clazz, provider);
            }
        }
{'fraction_non_alphanumeric': 0.0958812739703185, 'fraction_numerical': 0.00400200100050025, 'mean_word_length': 3.312005751258088, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '1791856', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1586, 'n_words': 473}