<commit_before>package com.mikepenz.fastadapter.utils;

import android.util.SparseIntArray;

import com.mikepenz.fastadapter.FastAdapter;
import com.mikepenz.fastadapter.IExpandable;
import com.mikepenz.fastadapter.IItem;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Created by mikepenz on 31.12.15.
 */
public class AdapterUtil {

    /**
     * internal method which correctly set the selected state and expandable state on the newly added items
     *
     * @param fastAdapter   the fastAdapter which manages everything
     * @param startPosition the position of the first item to handle
     * @param endPosition   the position of the last item to handle
     */
    public static <Item extends IItem> void handleStates(FastAdapter<Item> fastAdapter, int startPosition, int endPosition) {
        for (int i = endPosition; i >= startPosition; i--) {
            Item updateItem = fastAdapter.getItem(i);
            if (updateItem != null) {
                if (updateItem.isSelected()) {
                    fastAdapter.getSelections().add(i);
                } else if (fastAdapter.getSelections().contains(i)) {
                    fastAdapter.getSelections().remove(i);
                }
                if (updateItem instanceof IExpandable) {
                    if (((IExpandable) updateItem).isExpanded() && fastAdapter.getExpanded().indexOfKey(i) < 0) {
                        fastAdapter.expand(i);
                    }
                }
            }
        }
    }

    /**
     * internal method to handle the selections if items are added / removed
     *
     * @param positions     the positions map which should be adjusted
     * @param startPosition the global index of the first element modified
     * @param endPosition   the global index up to which the modification changed the indices (should be MAX_INT if we check til the end)
     * @param adjustBy      the value by which the data was shifted
     * @return the adjusted set
     */
    public static SortedSet<Integer> adjustPosition(Set<Integer> positions, int startPosition, int endPosition, int adjustBy) {
        SortedSet<Integer> newPositions = new TreeSet<>();

        for (Integer entry : positions) {
            int position = entry;

            //if our current position is not within the bounds to check for we can add it
            if (position < startPosition || position > endPosition) {
                newPositions.add(position);
            } else if (adjustBy > 0) {
                //if we added items and we are within the bounds we can simply add the adjustBy to our entry<commit_msg>* enhance memory consumption by using an ArraySet instead of an TreeSet<>   * THANKS @cbeyls<commit_after>package com.mikepenz.fastadapter.utils;

import android.support.v4.util.ArraySet;
import android.util.SparseIntArray;

import com.mikepenz.fastadapter.FastAdapter;
import com.mikepenz.fastadapter.IExpandable;
import com.mikepenz.fastadapter.IItem;

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

/**
 * Created by mikepenz on 31.12.15.
 */
public class AdapterUtil {

    /**
     * internal method which correctly set the selected state and expandable state on the newly added items
     *
     * @param fastAdapter   the fastAdapter which manages everything
     * @param startPosition the position of the first item to handle
     * @param endPosition   the position of the last item to handle
     */
    public static <Item extends IItem> void handleStates(FastAdapter<Item> fastAdapter, int startPosition, int endPosition) {
        for (int i = endPosition; i >= startPosition; i--) {
            Item updateItem = fastAdapter.getItem(i);
            if (updateItem != null) {
                if (updateItem.isSelected()) {
                    fastAdapter.getSelections().add(i);
                } else if (fastAdapter.getSelections().contains(i)) {
                    fastAdapter.getSelections().remove(i);
                }
                if (updateItem instanceof IExpandable) {
                    if (((IExpandable) updateItem).isExpanded() && fastAdapter.getExpanded().indexOfKey(i) < 0) {
                        fastAdapter.expand(i);
                    }
                }
            }
        }
    }

    /**
     * internal method to handle the selections if items are added / removed
     *
     * @param positions     the positions map which should be adjusted
     * @param startPosition the global index of the first element modified
     * @param endPosition   the global index up to which the modification changed the indices (should be MAX_INT if we check til the end)
     * @param adjustBy      the value by which the data was shifted
     * @return the adjusted set
     */
    public static Set<Integer> adjustPosition(Set<Integer> positions, int startPosition, int endPosition, int adjustBy) {
        Set<Integer> newPositions = new ArraySet<>();

        for (Integer entry : positions) {
            int position = entry;

            //if our current position is not within the bounds to check for we can add it
            if (position < startPosition || position > endPosition) {
                newPositions.add(position);
            } else if (adjustBy > 0) {
                //if we added items and we are within the bounds we can simply add the adjustBy to our entry
{'fraction_non_alphanumeric': 0.07985280588776449, 'fraction_numerical': 0.0031278748850045997, 'mean_word_length': 2.374301675977654, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 22, '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': '1118195', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1371, 'n_words': 502}