<commit_before>/*
 * Copyright (C) 2015 Actor LLC. <https://actor.im>
 */

package im.actor.runtime.actors;

import java.util.ArrayList;
import java.util.HashMap;

import im.actor.runtime.actors.dispatch.Mailbox;
import im.actor.runtime.actors.messages.DeadLetter;
import im.actor.runtime.threading.SimpleDispatcher;
import im.actor.runtime.threading.ThreadDispatcher;

/**
 * Actor object
 */
public class Actor {

    private final SimpleDispatcher dispatcher = runnable -> self().post(runnable);

    private Scheduler scheduler;

    private String path;

    private ActorContext context;
    private Mailbox mailbox;

    private ArrayList<Receiver> receivers;
    private HashMap<Integer, ArrayList<StashedMessage>> stashed;

    public Actor() {

    }

    /**
     * <p>INTERNAL API</p>
     */
    public SimpleDispatcher getDispatcher() {
        return dispatcher;
    }

    /**
     * <p>INTERNAL API</p>
     * Initialization of actor
     *
     * @param path    path of actor
     * @param context context of actor
     * @param mailbox mailbox of actor
     */
    public final void initActor(String path, ActorContext context, Mailbox mailbox) {
        this.path = path;
        this.context = context;
        this.mailbox = mailbox;
    }

    /**
     * <p>INTERNAL API</p>
     * Handling of a message in Actor
     *
     * @param message message
     */
    public final void handleMessage(Object message, ActorRef sender) {
        intHandle(message, sender);
    }

    public void stash() {
        stash(0);
    }

    public void stash(int index) {
        if (stashed == null) {
            stashed = new HashMap<>();
        }

        ArrayList<StashedMessage> stashedMessages = stashed.get(index);
        if (stashedMessages == null) {
            stashedMessages = new ArrayList<>();
            stashed.put(index, stashedMessages);
        }
        stashedMessages.add(new StashedMessage(context.message(), context.sender()));
    }

    public void unstashAll() {
        unstashAll(0);
    }

    public void unstashAll(int index) {
        if (stashed == null) {
            return;
        }
        ArrayList<StashedMessage> stashedMessages = stashed.get(index);
        if (stashedMessages == null || stashedMessages.size() == 0) {
            return;
        }
        for (StashedMessage stashedMessage : stashedMessages) {
            self().sendFirst(stashedMessage.getMessage(), stashedMessage.getSender());
        }
        stashedMessages.clear();
    }

    public void become(Receiver receiver) {
        if (receivers == null) {<commit_msg>fix(core): unstashAll in right order<commit_after>/*
 * Copyright (C) 2015 Actor LLC. <https://actor.im>
 */

package im.actor.runtime.actors;

import java.util.ArrayList;
import java.util.HashMap;

import im.actor.runtime.actors.dispatch.Mailbox;
import im.actor.runtime.actors.messages.DeadLetter;
import im.actor.runtime.threading.SimpleDispatcher;
import im.actor.runtime.threading.ThreadDispatcher;

/**
 * Actor object
 */
public class Actor {

    private final SimpleDispatcher dispatcher = runnable -> self().post(runnable);

    private Scheduler scheduler;

    private String path;

    private ActorContext context;
    private Mailbox mailbox;

    private ArrayList<Receiver> receivers;
    private HashMap<Integer, ArrayList<StashedMessage>> stashed;

    public Actor() {

    }

    /**
     * <p>INTERNAL API</p>
     */
    public SimpleDispatcher getDispatcher() {
        return dispatcher;
    }

    /**
     * <p>INTERNAL API</p>
     * Initialization of actor
     *
     * @param path    path of actor
     * @param context context of actor
     * @param mailbox mailbox of actor
     */
    public final void initActor(String path, ActorContext context, Mailbox mailbox) {
        this.path = path;
        this.context = context;
        this.mailbox = mailbox;
    }

    /**
     * <p>INTERNAL API</p>
     * Handling of a message in Actor
     *
     * @param message message
     */
    public final void handleMessage(Object message, ActorRef sender) {
        intHandle(message, sender);
    }

    public void stash() {
        stash(0);
    }

    public void stash(int index) {
        if (stashed == null) {
            stashed = new HashMap<>();
        }

        ArrayList<StashedMessage> stashedMessages = stashed.get(index);
        if (stashedMessages == null) {
            stashedMessages = new ArrayList<>();
            stashed.put(index, stashedMessages);
        }
        stashedMessages.add(new StashedMessage(context.message(), context.sender()));
    }

    public void unstashAll() {
        unstashAll(0);
    }

    public void unstashAll(int index) {
        if (stashed == null) {
            return;
        }
        ArrayList<StashedMessage> stashedMessages = stashed.get(index);
        if (stashedMessages == null || stashedMessages.size() == 0) {
            return;
        }
        StashedMessage stashedMessage;
        for (int i = stashedMessages.size() - 1; i >= 0; i--) {
            stashedMessage = stashedMessages.get(i);
            self().sendFirst(stashedMessage.getMessage(), stashedMessage.getSender());
        }
        stashedMessages.clear();
    }

    public void become(Receiver receiver) {
        if (receivers == null) {
{'fraction_non_alphanumeric': 0.11094368054247504, 'fraction_numerical': 0.0030137502354492373, 'mean_word_length': 2.4191886670959435, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 34, 'https://': 2, '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': '2375449', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1485, 'n_words': 368}