<commit_before>package com.thornbirds.frameworkext.component.page;

import static com.thornbirds.frameworkext.component.route.RouteComponentController.STATE_DESTROYED;
import static com.thornbirds.frameworkext.component.route.RouteComponentController.STATE_STARTED;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.thornbirds.frameworkext.component.route.IPageCreator;
import com.thornbirds.frameworkext.component.route.IPageEntry;
import com.thornbirds.frameworkext.component.route.IPageParams;
import com.thornbirds.frameworkext.component.route.IPageRouter;
import com.thornbirds.frameworkext.component.route.RouteComponentController;

import java.util.LinkedList;

/**
 * @author YangLi <EMAIL>
 */
final class StackedPageRouter extends PageRouter<IPageEntry, IPageCreator<IPageEntry>> {
    private final RouteComponentController<IPageRouter> mRouterController;
    private final LinkedList<IPageEntry> mPageStack = new LinkedList<>();

    public StackedPageRouter(@NonNull RouteComponentController controller) {
        this.mRouterController = controller;
    }

    final boolean isEmpty() {
        return mPageStack.isEmpty();
    }

    private int size() {
        return mPageStack.size();
    }

    private IPageEntry top() {
        return mPageStack.isEmpty() ? null : mPageStack.getFirst();
    }

    private IPageEntry pop() {
        return mPageStack.isEmpty() ? null : mPageStack.removeFirst();
    }

    @Override
    protected final boolean doStartPage(@NonNull String route, @Nullable IPageParams params) {
        final IPageCreator creator = resolveRoute(route);
        if (creator == null) {
            final IPageRouter parentRouter = mRouterController.getParentRouter();
            if (parentRouter != null) {
                return parentRouter.startPage(route, params);
            }
            LogE("gotoPage failed, no route found for " + route);
            return false;
        }
        final IPageEntry entry = creator.create(params);
        if (entry == null) {
            LogE("gotoPage failed, no page found for " + route);
            return false;
        }
        return doAddPageInner(entry);
    }

    private boolean doAddPageInner(@NonNull IPageEntry entry) {
        final int state = mRouterController.getState();
        if (state == STATE_DESTROYED) {
            return false;
        }
        final IPageEntry topEntry = top();
        if (topEntry != null) {
            if (topEntry == entry) {
                return true;
            }
            topEntry.performStop();
        }
        entry.performCreate(mRouterController);
        if (state == STATE_STARTED) {
            entry.performStart();
        }
        mPageStack.push(entry);
        return true;
    }

    @Override
    protected final boolean doPopPage(@Nullable RouteComponentController page) {
        if (page != null && top() != null && top().matchController(page)) {
            return false;
        }
        if (size() <= 1) {
            return mRouterController.exitRouter();<commit_msg>修复页面无法退出<commit_after>package com.thornbirds.frameworkext.component.page;

import static com.thornbirds.frameworkext.component.route.RouteComponentController.STATE_DESTROYED;
import static com.thornbirds.frameworkext.component.route.RouteComponentController.STATE_STARTED;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.thornbirds.frameworkext.component.route.IPageCreator;
import com.thornbirds.frameworkext.component.route.IPageEntry;
import com.thornbirds.frameworkext.component.route.IPageParams;
import com.thornbirds.frameworkext.component.route.IPageRouter;
import com.thornbirds.frameworkext.component.route.RouteComponentController;

import java.util.LinkedList;

/**
 * @author YangLi <EMAIL>
 */
final class StackedPageRouter extends PageRouter<IPageEntry, IPageCreator<IPageEntry>> {
    private final RouteComponentController<IPageRouter> mRouterController;
    private final LinkedList<IPageEntry> mPageStack = new LinkedList<>();

    public StackedPageRouter(@NonNull RouteComponentController controller) {
        this.mRouterController = controller;
    }

    final boolean isEmpty() {
        return mPageStack.isEmpty();
    }

    private int size() {
        return mPageStack.size();
    }

    private IPageEntry top() {
        return mPageStack.isEmpty() ? null : mPageStack.getFirst();
    }

    private IPageEntry pop() {
        return mPageStack.isEmpty() ? null : mPageStack.removeFirst();
    }

    @Override
    protected final boolean doStartPage(@NonNull String route, @Nullable IPageParams params) {
        final IPageCreator creator = resolveRoute(route);
        if (creator == null) {
            final IPageRouter parentRouter = mRouterController.getParentRouter();
            if (parentRouter != null) {
                return parentRouter.startPage(route, params);
            }
            LogE("gotoPage failed, no route found for " + route);
            return false;
        }
        final IPageEntry entry = creator.create(params);
        if (entry == null) {
            LogE("gotoPage failed, no page found for " + route);
            return false;
        }
        return doAddPageInner(entry);
    }

    private boolean doAddPageInner(@NonNull IPageEntry entry) {
        final int state = mRouterController.getState();
        if (state == STATE_DESTROYED) {
            return false;
        }
        final IPageEntry topEntry = top();
        if (topEntry != null) {
            if (topEntry == entry) {
                return true;
            }
            topEntry.performStop();
        }
        entry.performCreate(mRouterController);
        if (state == STATE_STARTED) {
            entry.performStart();
        }
        mPageStack.push(entry);
        return true;
    }

    @Override
    protected final boolean doPopPage(@Nullable RouteComponentController page) {
        if (page != null && top() != null && !top().matchController(page)) {
            return false;
        }
        if (size() <= 1) {
            return mRouterController.exitRouter();
{'fraction_non_alphanumeric': 0.09559768637532133, 'fraction_numerical': 0.00032133676092544985, 'mean_word_length': 2.826060233558697, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '7155145', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1680, 'n_words': 367}