<commit_before>package net.stuxcrystal.commandhandler.contrib.scheduler;

import net.stuxcrystal.commandhandler.CommandHandler;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Manages tasks for the command handler.</p>
 *
 * <p>
 *     Please note that the task queue actually depends on the Root-{@link net.stuxcrystal.commandhandler.CommandHandler}
 *     being garbage collected because once it's finalized, it will <b>not</b> continue running the
 *     tasks handled by the task queue.
 * </p>
 *
 * <p>
 *     To make sure the CommandHandler will be collected after the plugin is being disabled
 *     set the
 * </p>
 */
public class TaskQueue extends Thread {

    /**
     * Executes the task.
     */
    private List<Task> tasks = new ArrayList<>();

    /**
     * <p>Contains a weak reference to a command handler.</p>
     * <p>Weak to ensure that when the commandhandler does not exist anymore, the thread stops.</p>
     */
    private WeakReference<CommandHandler> owner;

    /**
     * Creates the new task manager.
     * @param handler The handler that executes these tasks.
     */
    public TaskQueue(CommandHandler handler) {
        this.owner = new WeakReference<>(handler);
        this.setDaemon(true);
    }

    /**
     * Returns the command-handler that actually handles the commands.
     * @return {@code null} if the handler has already been garbage collected.
     */
    public CommandHandler getCommandHandler() {
        return this.owner.get();
    }

    /**
     * Register a new task.
     * @param task The task to register.
     */
    public void addTask(Task task) {
        this.tasks.add(task);
    }

    public void run() {
        // While the CommandHandler exists...
        while (this.owner.get() != null) {
            List<Integer> toRemove = new ArrayList<>();

            long fTime = 50;

            // Check if we want to execute this task.
            long cTime = System.currentTimeMillis();

            Task task;
            for (int i = 0; i<this.tasks.size(); i++) {
                // Gets the task.
                task = this.tasks.get(i);

                // Check if we don't need the task anymore.
                if (task.isCancelled() || task.isCompleted()) {
                    toRemove.add(i);
                    continue;
                }

                long waitTime = task.getNextExecutionTime() - cTime;
                if (waitTime >= 0) {
                    // If the next task is scheduled to executed
                    if (fTime > waitTime)
                        fTime = waitTime;
                    continue;
                }

                // Executes the task.
                task.execute(this.owner.get());
            }

            // Remove completed tasks.
            for (Integer tId : toRemove) {
                this.tasks.remove((int)tId);
            }

            // Make sure we allow other threads to work.
            Thread.yield();
            try {<commit_msg>Since the taskqueue is part of the fallback scheduler we use BasicTask.<commit_after>package net.stuxcrystal.commandhandler.contrib.scheduler;

import net.stuxcrystal.commandhandler.CommandHandler;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Manages tasks for the command handler.</p>
 *
 * <p>
 *     Please note that the task queue actually depends on the Root-{@link net.stuxcrystal.commandhandler.CommandHandler}
 *     being garbage collected because once it's finalized, it will <b>not</b> continue running the
 *     tasks handled by the task queue.
 * </p>
 *
 * <p>
 *     To make sure the CommandHandler will be collected after the plugin is being disabled
 *     set the
 * </p>
 */
public class TaskQueue extends Thread {

    /**
     * Executes the task.
     */
    private List<BasicTask> tasks = new ArrayList<>();

    /**
     * <p>Contains a weak reference to a command handler.</p>
     * <p>Weak to ensure that when the commandhandler does not exist anymore, the thread stops.</p>
     */
    private WeakReference<CommandHandler> owner;

    /**
     * Creates the new task manager.
     * @param handler The handler that executes these tasks.
     */
    public TaskQueue(CommandHandler handler) {
        this.owner = new WeakReference<>(handler);
        this.setDaemon(true);
    }

    /**
     * Returns the command-handler that actually handles the commands.
     * @return {@code null} if the handler has already been garbage collected.
     */
    public CommandHandler getCommandHandler() {
        return this.owner.get();
    }

    /**
     * Register a new task.
     * @param task The task to register.
     */
    public void addTask(BasicTask task) {
        this.tasks.add(task);
    }

    public void run() {
        // While the CommandHandler exists...
        while (this.owner.get() != null) {
            List<Integer> toRemove = new ArrayList<>();

            long fTime = 50;

            // Check if we want to execute this task.
            long cTime = System.currentTimeMillis();

            BasicTask task;
            for (int i = 0; i<this.tasks.size(); i++) {
                // Gets the task.
                task = this.tasks.get(i);

                // Check if we don't need the task anymore.
                if (task.isCancelled() || task.isCompleted()) {
                    toRemove.add(i);
                    continue;
                }

                long waitTime = task.getNextExecutionTime() - cTime;
                if (waitTime >= 0) {
                    // If the next task is scheduled to executed
                    if (fTime > waitTime)
                        fTime = waitTime;
                    continue;
                }

                // Executes the task.
                task.execute(this.owner.get());
            }

            // Remove completed tasks.
            for (Integer tId : toRemove) {
                this.tasks.remove((int)tId);
            }

            // Make sure we allow other threads to work.
            Thread.yield();
            try {
{'fraction_non_alphanumeric': 0.10099203122458937, 'fraction_numerical': 0.0013010245568385104, 'mean_word_length': 1.7516778523489933, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 43, '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': '4944382', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1570, 'n_words': 533}