<commit_before>/*
 *    Copyright (C) 2015 <NAME>
 *
 *    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 com.h6ah4i.android.widget.advrecyclerview.utils;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;

import com.h6ah4i.android.widget.advrecyclerview.adapter.AdapterPath;
import com.h6ah4i.android.widget.advrecyclerview.adapter.AdapterPathSegment;
import com.h6ah4i.android.widget.advrecyclerview.adapter.SimpleWrapperAdapter;
import com.h6ah4i.android.widget.advrecyclerview.adapter.UnwrapPositionResult;
import com.h6ah4i.android.widget.advrecyclerview.adapter.WrapperAdapter;

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

public class WrapperAdapterUtils {

    private WrapperAdapterUtils() {
    }

    public static <T> T findWrappedAdapter(RecyclerView.Adapter adapter, Class<T> clazz) {
        if (clazz.isInstance(adapter)) {
            return clazz.cast(adapter);
        } else if (adapter instanceof SimpleWrapperAdapter) {
            final RecyclerView.Adapter wrappedAdapter = ((SimpleWrapperAdapter) adapter).getWrappedAdapter();
            return findWrappedAdapter(wrappedAdapter, clazz);
        } else {
            return null;
        }
    }

    public static <T> T findWrappedAdapter(RecyclerView.Adapter originAdapter, Class<T> clazz, int position) {
        final AdapterPath path = new AdapterPath();
        final int wrappedPosition = unwrapPosition(originAdapter, null, null, position, path);

        if (wrappedPosition == RecyclerView.NO_POSITION) {
            return null;
        }

        for (AdapterPathSegment segment : path.segments()) {
            if (clazz.isInstance(segment.adapter)) {
                return clazz.cast(segment.adapter);
            }
        }

        return null;
    }

    public static RecyclerView.Adapter releaseAll(RecyclerView.Adapter adapter) {
        return releaseCyclically(adapter);
    }

    @SuppressWarnings("unchecked")
    private static RecyclerView.Adapter releaseCyclically(RecyclerView.Adapter adapter) {
        if (!(adapter instanceof WrapperAdapter)) {
            return adapter;
        }

        final WrapperAdapter wrapperAdapter = (WrapperAdapter) adapter;
        final List<RecyclerView.Adapter> wrappedAdapters = new ArrayList<>();

        wrapperAdapter.getWrappedAdapters(wrappedAdapters);

        wrapperAdapter.release();

        for (int i = wrappedAdapters.size() - 1; i >= 0; i--) {
            RecyclerView.Adapter wrappedAdapter = wrappedAdapters.get(i);
            releaseCyclically(wrappedAdapter);
        }

        wrappedAdapters.clear();

        return adapter;
    }

    public static int unwrapPosition(@NonNull RecyclerView.Adapter originAdapter, int position) {
        return unwrapPosition(originAdapter, null, position);
    }

    public static int unwrapPosition(@NonNull RecyclerView.Adapter originAdapter, @Nullable RecyclerView.Adapter targetAdapter, int position) {
        return unwrapPosition(originAdapter, targetAdapter, null, position, null);
    }


    public static int unwrapPosition(@NonNull RecyclerView.Adapter originAdapter, @Nullable RecyclerView.Adapter targetAdapter, Object targetAdapterTag, int position) {
        return unwrapPosition(originAdapter, targetAdapter, targetAdapterTag, position, null);
    }

    public static int unwrapPosition(RecyclerView.Adapter originAdapter, RecyclerView.Adapter targetAdapter, Object targetAdapterTag, int originPosition, @Nullable AdapterPath destPath) {
        RecyclerView.Adapter wrapper = originAdapter;
        int wrappedPosition = originPosition;
        final UnwrapPositionResult tmpResult = new UnwrapPositionResult();
        Object wrappedAdapterTag = null;

        if (destPath != null) {
            destPath.clear();
        }

        if (wrapper == null) {
            return RecyclerView.NO_POSITION;
        }

        if (destPath != null) {
            destPath.append(new AdapterPathSegment(originAdapter, null));
        }

        do {
            if (wrappedPosition == RecyclerView.NO_POSITION) {
                break;
            }

            if (wrapper == targetAdapter) {<commit_msg>Add variant of unwrapPosition() which takes an adapter path segment for the target adapter<commit_after>/*
 *    Copyright (C) 2015 <NAME>
 *
 *    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 com.h6ah4i.android.widget.advrecyclerview.utils;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;

import com.h6ah4i.android.widget.advrecyclerview.adapter.AdapterPath;
import com.h6ah4i.android.widget.advrecyclerview.adapter.AdapterPathSegment;
import com.h6ah4i.android.widget.advrecyclerview.adapter.SimpleWrapperAdapter;
import com.h6ah4i.android.widget.advrecyclerview.adapter.UnwrapPositionResult;
import com.h6ah4i.android.widget.advrecyclerview.adapter.WrapperAdapter;

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

public class WrapperAdapterUtils {

    private WrapperAdapterUtils() {
    }

    public static <T> T findWrappedAdapter(RecyclerView.Adapter adapter, Class<T> clazz) {
        if (clazz.isInstance(adapter)) {
            return clazz.cast(adapter);
        } else if (adapter instanceof SimpleWrapperAdapter) {
            final RecyclerView.Adapter wrappedAdapter = ((SimpleWrapperAdapter) adapter).getWrappedAdapter();
            return findWrappedAdapter(wrappedAdapter, clazz);
        } else {
            return null;
        }
    }

    public static <T> T findWrappedAdapter(RecyclerView.Adapter originAdapter, Class<T> clazz, int position) {
        final AdapterPath path = new AdapterPath();
        final int wrappedPosition = unwrapPosition(originAdapter, null, null, position, path);

        if (wrappedPosition == RecyclerView.NO_POSITION) {
            return null;
        }

        for (AdapterPathSegment segment : path.segments()) {
            if (clazz.isInstance(segment.adapter)) {
                return clazz.cast(segment.adapter);
            }
        }

        return null;
    }

    public static RecyclerView.Adapter releaseAll(RecyclerView.Adapter adapter) {
        return releaseCyclically(adapter);
    }

    @SuppressWarnings("unchecked")
    private static RecyclerView.Adapter releaseCyclically(RecyclerView.Adapter adapter) {
        if (!(adapter instanceof WrapperAdapter)) {
            return adapter;
        }

        final WrapperAdapter wrapperAdapter = (WrapperAdapter) adapter;
        final List<RecyclerView.Adapter> wrappedAdapters = new ArrayList<>();

        wrapperAdapter.getWrappedAdapters(wrappedAdapters);

        wrapperAdapter.release();

        for (int i = wrappedAdapters.size() - 1; i >= 0; i--) {
            RecyclerView.Adapter wrappedAdapter = wrappedAdapters.get(i);
            releaseCyclically(wrappedAdapter);
        }

        wrappedAdapters.clear();

        return adapter;
    }

    public static int unwrapPosition(@NonNull RecyclerView.Adapter originAdapter, int position) {
        return unwrapPosition(originAdapter, null, position);
    }

    public static int unwrapPosition(@NonNull RecyclerView.Adapter originAdapter, @Nullable RecyclerView.Adapter targetAdapter, int position) {
        return unwrapPosition(originAdapter, targetAdapter, null, position, null);
    }


    public static int unwrapPosition(@NonNull RecyclerView.Adapter originAdapter, @Nullable RecyclerView.Adapter targetAdapter, Object targetAdapterTag, int position) {
        return unwrapPosition(originAdapter, targetAdapter, targetAdapterTag, position, null);
    }

    public static int unwrapPosition(RecyclerView.Adapter originAdapter, AdapterPathSegment targetAdapterPathSegment, int originPosition, @Nullable AdapterPath destPath) {
        return unwrapPosition(originAdapter, targetAdapterPathSegment.adapter, targetAdapterPathSegment.tag, originPosition, destPath);
    }

    public static int unwrapPosition(RecyclerView.Adapter originAdapter, RecyclerView.Adapter targetAdapter, Object targetAdapterTag, int originPosition, @Nullable AdapterPath destPath) {
        RecyclerView.Adapter wrapper = originAdapter;
        int wrappedPosition = originPosition;
        final UnwrapPositionResult tmpResult = new UnwrapPositionResult();
        Object wrappedAdapterTag = null;

        if (destPath != null) {
            destPath.clear();
        }

        if (wrapper == null) {
            return RecyclerView.NO_POSITION;
        }

        if (destPath != null) {
            destPath.append(new AdapterPathSegment(originAdapter, null));
        }

        do {
            if (wrappedPosition == RecyclerView.NO_POSITION) {
                break;
            }

            if (wrapper == targetAdapter) {
{'fraction_non_alphanumeric': 0.08250958240871495, 'fraction_numerical': 0.004639903167238249, 'mean_word_length': 3.1764953664700926, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, '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': '3145964', 'n_tokens_mistral': 2655, 'n_tokens_neox': 2548, 'n_words': 673}