<commit_before>package com.eaglesakura.android.framework.support.ui.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import com.eaglesakura.android.framework.support.ui.FragmentChooser;
import com.eaglesakura.android.framework.support.ui.SupportAnnotationUtil;
import com.eaglesakura.util.LogUtil;

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

/**
 * FragmentListAdapterをサポートする
 */
public class FragmentListAdapter extends FragmentPagerAdapter {
    FragmentChooser chooser;
    final List<FragmentCreater> creaters = new ArrayList<FragmentCreater>();

    final FragmentManager fragmentManager;

    int containerViewId;

    public FragmentListAdapter(final FragmentManager fragmentManager) {
        super(fragmentManager);
        this.fragmentManager = fragmentManager;
    }

    String genFragmentId(int index) {
        return "android:switcher:" + containerViewId + ":" + index;
    }

    private static String genFragmentId(int containerViewId, int index) {
        return "android:switcher:" + containerViewId + ":" + index;
    }

    @Override
    public void startUpdate(ViewGroup container) {
        if (containerViewId == 0) {
            this.containerViewId = container.getId();
        }
        super.startUpdate(container);
    }

    public void setContainerViewId(int containerViewId) {
        this.containerViewId = containerViewId;
    }

    /**
     * ViewPagerと接続する
     *
     * @param pager
     */
    public void setViewPager(ViewPager pager) {
        this.containerViewId = pager.getId();
        pager.setAdapter(this);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Fragment item = getItem(position);
        if (item instanceof IFragmentPagerTitle) {
            return ((IFragmentPagerTitle) item).getTitle();
        }
        return super.getPageTitle(position);
    }

    /**
     * Fragment管理を行う
     * Callbackを自動で設定するため、事前に設定したCallbackが呼び出されないことに注意すること。
     *
     * @param chooser
     */
    public void setChooser(FragmentChooser chooser) {
        this.chooser = chooser;
        chooser.setCallback(new FragmentChooser.Callback() {
            @Override
            public FragmentManager getFragmentManager(FragmentChooser chooser) {
                return fragmentManager;
            }

            @Override
            public boolean isFragmentExist(FragmentChooser chooser, Fragment fragment) {
                return fragment != null;
            }

            @Override
            public Fragment newFragment(FragmentChooser chooser, String requestTag) {
                return null;
            }
        });
    }

    @Override
    public Fragment getItem(int position) {
        FragmentCreater creater = creaters.get(position);
        final String reqTag = genFragmentId(position);
        Fragment result = chooser.find(reqTag);
        if (result == null) {
            result = creater.newFragment(this, position);
            LogUtil.log("adapter new fragment(%s)", reqTag);
        } else {
            LogUtil.log("adapter find fragment from chooser(%s)", reqTag);
        }
        return result;
    }

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

    public void addFragmentCreater(FragmentCreater creater) {
        creaters.add(creater);
    }

    public List<Fragment> listFragments() {
        List<Fragment> result = new ArrayList<Fragment>();
        for (int i = 0; i < creaters.size(); ++i) {
            result.add(getItem(i));
        }
        return result;
    }

    /**
     * 指定した条件のFragmentを生成する
     */
    public interface FragmentCreater {
        /**
         * Fragmentを生成させる
         *
         * @param adapter
         * @param index
         * @return
         */
        Fragment newFragment(FragmentListAdapter adapter, int index);
    }

    /**
     * 単純にClassを生成する
     */
    public static class SimpleFragmentCreater implements FragmentCreater {
        Class<? extends Fragment> clazz;

        public SimpleFragmentCreater(Class<? extends Fragment> clazz) {
            this.clazz = clazz;
        }

        /**
         * 何か初期化の必要がある時はオーバーライドする
         *
         * @param fragment
         * @return
         */
        protected Fragment initialize(Fragment fragment) {
            return fragment;
        }

        @Override
        public Fragment newFragment(FragmentListAdapter adapter, int index) {
            return initialize(SupportAnnotationUtil.newFragment(clazz));
        }
    }


    public static class SimpleFragmentCreater2 extends SimpleFragmentCreater {
        /**
         * 登録先のセレクタ
         */
        final FragmentChooser parentChooser;
        final int containerViewId;

        public SimpleFragmentCreater2(Class<? extends Fragment> clazz, ViewPager pager, FragmentChooser parentChooser) {
            super(clazz);
            this.parentChooser = parentChooser;
            this.containerViewId = pager.getId();
        }

        @Override
        public Fragment newFragment(FragmentListAdapter adapter, int index) {
            Fragment result = super.newFragment(adapter, index);
            parentChooser.compact();
            parentChooser.addFragment(FragmentChooser.ReferenceType.Weak, result, genFragmentId(containerViewId, index), 0);
            return result;
        }
    }
}
<commit_msg>Adapterを修正<commit_after>package com.eaglesakura.android.framework.support.ui.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import com.eaglesakura.android.framework.support.ui.FragmentChooser;
import com.eaglesakura.android.framework.support.ui.SupportAnnotationUtil;
import com.eaglesakura.util.LogUtil;

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

/**
 * FragmentListAdapterをサポートする
 */
public class FragmentListAdapter extends FragmentPagerAdapter {
    FragmentChooser chooser;
    final List<FragmentCreater> creaters = new ArrayList<FragmentCreater>();

    final FragmentManager fragmentManager;

    int containerViewId;

    public FragmentListAdapter(final FragmentManager fragmentManager) {
        super(fragmentManager);
        this.fragmentManager = fragmentManager;
    }

    String genFragmentId(int index) {
        return "android:switcher:" + containerViewId + ":" + index;
    }

    private static String genFragmentId(int containerViewId, int index) {
        return "android:switcher:" + containerViewId + ":" + index;
    }

    @Override
    public void startUpdate(ViewGroup container) {
        if (containerViewId == 0) {
            this.containerViewId = container.getId();
        }
        super.startUpdate(container);
    }

    public void setContainerViewId(int containerViewId) {
        this.containerViewId = containerViewId;
    }

    /**
     * ViewPagerと接続する
     *
     * @param pager
     */
    public void setViewPager(ViewPager pager) {
        this.containerViewId = pager.getId();
        pager.setAdapter(this);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Fragment item = getItem(position);
        if (item instanceof IFragmentPagerTitle) {
            return ((IFragmentPagerTitle) item).getTitle();
        }
        return super.getPageTitle(position);
    }

    /**
     * Fragment管理を行う
     * Callbackを自動で設定するため、事前に設定したCallbackが呼び出されないことに注意すること。
     *
     * @param chooser
     */
    public void setChooser(FragmentChooser chooser) {
        this.chooser = chooser;
        chooser.setCallback(new FragmentChooser.Callback() {
            @Override
            public FragmentManager getFragmentManager(FragmentChooser chooser) {
                return fragmentManager;
            }

            @Override
            public boolean isFragmentExist(FragmentChooser chooser, Fragment fragment) {
                return fragment != null;
            }

            @Override
            public Fragment newFragment(FragmentChooser chooser, String requestTag) {
                return null;
            }
        });
    }

    @Override
    public Fragment getItem(int position) {
        FragmentCreater creater = creaters.get(position);
        final String reqTag = genFragmentId(position);
        Fragment result = chooser.find(reqTag);
        if (result == null) {
            result = creater.newFragment(this, position);
            LogUtil.log("adapter new fragment(%s)", reqTag);
        } else {
            LogUtil.log("adapter find fragment from chooser(%s)", reqTag);
        }
        return result;
    }

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

    public void addFragmentCreater(FragmentCreater creater) {
        creaters.add(creater);
    }

    public List<Fragment> listFragments() {
        List<Fragment> result = new ArrayList<Fragment>();
        for (int i = 0; i < creaters.size(); ++i) {
            result.add(getItem(i));
        }
        return result;
    }

    /**
     * 指定した条件のFragmentを生成する
     */
    public interface FragmentCreater {
        /**
         * Fragmentを生成させる
         *
         * @param adapter
         * @param index
         * @return
         */
        Fragment newFragment(FragmentListAdapter adapter, int index);
    }

    /**
     * 単純にClassを生成する
     */
    public static class SimpleFragmentCreater implements FragmentCreater {
        Class<? extends Fragment> clazz;

        public SimpleFragmentCreater(Class<? extends Fragment> clazz) {
            this.clazz = clazz;
        }

        /**
         * 何か初期化の必要がある時はオーバーライドする
         *
         * @param fragment
         * @return
         */
        protected Fragment initialize(Fragment fragment) {
            return fragment;
        }

        @Override
        public Fragment newFragment(FragmentListAdapter adapter, int index) {
            return initialize(SupportAnnotationUtil.newFragment(clazz));
        }
    }


    public static class SimpleFragmentCreater2 extends SimpleFragmentCreater {
        /**
         * 登録先のセレクタ
         */
        final FragmentChooserRef chooserRef;
        final int containerViewId;

        public SimpleFragmentCreater2(Class<? extends Fragment> clazz, ViewPager pager, FragmentChooserRef chooserRef) {
            super(clazz);
            this.chooserRef = chooserRef;
            this.containerViewId = pager.getId();
        }

        @Override
        public Fragment newFragment(FragmentListAdapter adapter, int index) {
            Fragment result = super.newFragment(adapter, index);
            FragmentChooser parentChooser = chooserRef.getFragmentChooser();
            parentChooser.compact();
            parentChooser.addFragment(FragmentChooser.ReferenceType.Weak, result, genFragmentId(containerViewId, index), 0);
            return result;
        }
    }

    public interface FragmentChooserRef {
        FragmentChooser getFragmentChooser();
    }
}

{'fraction_non_alphanumeric': 0.08729093998747876, 'fraction_numerical': 0.0016098738932116983, 'mean_word_length': 2.357957957957958, 'pattern_counts': {'":': 4, '<': 21, '<?xml version=': 0, '>': 19, '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': '4561594', 'n_tokens_mistral': 3230, 'n_tokens_neox': 3036, 'n_words': 622}