<commit_before>package seedu.task.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

import javafx.collections.transformation.FilteredList;
import seedu.task.commons.core.ComponentManager;
import seedu.task.commons.core.History;
import seedu.task.commons.core.LogsCenter;
import seedu.task.commons.core.UnmodifiableObservableList;
import seedu.task.commons.events.model.FilePathChangedEvent;
import seedu.task.commons.events.model.TaskManagerChangedEvent;
import seedu.task.commons.exceptions.IllegalValueException;
import seedu.task.commons.util.CollectionUtil;
import seedu.task.commons.util.StringUtil;
import seedu.task.commons.util.TaskUtil;
import seedu.task.model.task.Date;
import seedu.task.model.task.ReadOnlyTask;
import seedu.task.model.task.Task;
import seedu.task.model.task.UniqueTaskList;
import seedu.task.model.task.UniqueTaskList.DuplicateTaskException;
import seedu.task.model.task.UniqueTaskList.TaskNotFoundException;

/**
 * Represents the in-memory model of KIT data. All changes to any model should be synchronized.
 */
public class ModelManager extends ComponentManager implements Model {
    private static final Logger logger = LogsCenter.getLogger(ModelManager.class);

    private final TaskManager taskManager;
    private final FilteredList<ReadOnlyTask> filteredTasks;
    private final History history;
    private UserPrefs userPrefs;

    /**
     * Initializes a ModelManager with the given taskManager and userPrefs.
     *
     * @throws IllegalValueException
     */
    public ModelManager(ReadOnlyTaskManager taskManager, UserPrefs userPrefs) {
        super();
        assert !CollectionUtil.isAnyNull(taskManager, userPrefs);

        logger.fine("Initializing with task manager: " + taskManager + " and user prefs " + userPrefs);

        this.taskManager = new TaskManager(taskManager);
        filteredTasks = new FilteredList<>(this.taskManager.getTaskList());

        this.userPrefs = userPrefs;
        this.history = History.getInstance();
    }

    public ModelManager() {
        this(new TaskManager(), new UserPrefs());
    }

    // @@author A0140063X
    /**
     * Resets data of taskManager.
     */
    @Override
    public void resetData(ReadOnlyTaskManager newData) throws IllegalValueException {
        taskManager.resetData(newData);
        indicateTaskManagerChanged(history.getBackupFilePath());
    }

    // @@author A0140063X
    /**
     * Load data into taskManager. Used by Undo/Redo Command.
     */
    @Override
    public void loadData(ReadOnlyTaskManager newData) throws IllegalValueException {
        taskManager.resetData(newData);
        raise(new TaskManagerChangedEvent(taskManager, history.getBackupFilePath()));
    }

    // @@author A0140063X
    /**
     *
     * @return taskManager of model.
     */
    @Override
    public ReadOnlyTaskManager getTaskManager() {
        return taskManager;
    }

    // @@author A0140063X
    /**
     * Raises an event to indicate the model has changed.
     *
     * @param backupFilePath
     *            File path to back up into.
     */
    private void indicateTaskManagerChanged(String backupFilePath) {
        history.handleTaskManagerChanged(backupFilePath);
        raise(new TaskManagerChangedEvent(taskManager, backupFilePath));
    }

    // @@author
    /** Raises an event to indicate the file path has changed */
    private void indicateFilePathChanged(String newPath) {
        raise(new FilePathChangedEvent(newPath, taskManager));
    }

    @Override
    public synchronized void deleteTask(ReadOnlyTask target) throws TaskNotFoundException {
        taskManager.removeTask(target);
        indicateTaskManagerChanged(history.getBackupFilePath());
    }

    // @@author A0139975J
    @Override
    public synchronized void isDoneTask(int index) throws TaskNotFoundException {
        int taskManagerIndex = filteredTasks.getSourceIndex(index);
        taskManager.updateDone(taskManagerIndex);
        indicateTaskManagerChanged(history.getBackupFilePath());
    }

    // @@author A0139975J
    @Override
    public synchronized void unDoneTask(int index) throws TaskNotFoundException {
        int taskManagerIndex = filteredTasks.getSourceIndex(index);
        taskManager.updateUnDone(taskManagerIndex);
        indicateTaskManagerChanged(history.getBackupFilePath());
    }

    // @@author A0140063X
    /**
     *
     * @param target
     *            Target task to change.
     * @param eventId
     *            Event id to change into.
     */
    @Override
    public void setTaskEventId(ReadOnlyTask target, String eventId)
            throws TaskNotFoundException, IllegalValueException {
        taskManager.setTaskEventId(target, eventId);
        indicateTaskManagerChanged("");
    }

    // @@author
    @Override
    public synchronized void addTask(Task task) throws UniqueTaskList.DuplicateTaskException {
        taskManager.addTaskToFront(task);
        updateFilteredListToShowAll();
        indicateTaskManagerChanged(history.getBackupFilePath());
    }

    // @@author A0140063X
    /**
     * This method adds every task in tasks to model.
     *
     * @param tasks
     *            ArrayList of task to add.
     */
    @Override
    public void addMultipleTasks(ArrayList<Task> tasks) {
        for (Task task : tasks) {
            try {
                taskManager.addTaskToFront(task);
            } catch (DuplicateTaskException e) {
                logger.info("Duplicate task " + task.getName() + " not added.");
            }
        }

        updateFilteredListToShowAll();
        indicateTaskManagerChanged(history.getBackupFilePath());
    }

    // @@author
    @Override
    public void updateTask(int filteredTaskListIndex, ReadOnlyTask editedTask) throws IllegalValueException {
        assert editedTask != null;

        int taskManagerIndex = filteredTasks.getSourceIndex(filteredTaskListIndex);
        taskManager.updateTask(taskManagerIndex, editedTask);
        indicateTaskManagerChanged(history.getBackupFilePath());
    }

    @Override
    public void sortTaskList() {
        taskManager.sortTaskList();
        indicateTaskManagerChanged("");
    }

    @Override
    public void changeFilePath(String newPath) {
        indicateFilePathChanged(newPath);
        indicateTaskManagerChanged("");
    }

    @Override
    public void loadFromLocation(String loadPath) {
        indicateFilePathChanged(loadPath);
    }

    // =========== Filtered Task List Accessors =============================================================

    @Override
    public UnmodifiableObservableList<ReadOnlyTask> getFilteredTaskList() {
        return new UnmodifiableObservableList<>(filteredTasks);
    }
<commit_msg>collate more parts<commit_after>package seedu.task.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

import javafx.collections.transformation.FilteredList;
import seedu.task.commons.core.ComponentManager;
import seedu.task.commons.core.History;
import seedu.task.commons.core.LogsCenter;
import seedu.task.commons.core.UnmodifiableObservableList;
import seedu.task.commons.events.model.FilePathChangedEvent;
import seedu.task.commons.events.model.TaskManagerChangedEvent;
import seedu.task.commons.exceptions.IllegalValueException;
import seedu.task.commons.util.CollectionUtil;
import seedu.task.commons.util.StringUtil;
import seedu.task.commons.util.TaskUtil;
import seedu.task.model.task.Date;
import seedu.task.model.task.ReadOnlyTask;
import seedu.task.model.task.Task;
import seedu.task.model.task.UniqueTaskList;
import seedu.task.model.task.UniqueTaskList.DuplicateTaskException;
import seedu.task.model.task.UniqueTaskList.TaskNotFoundException;

/**
 * Represents the in-memory model of KIT data. All changes to any model should be synchronized.
 */
public class ModelManager extends ComponentManager implements Model {
    private static final Logger logger = LogsCenter.getLogger(ModelManager.class);

    private final TaskManager taskManager;
    private final FilteredList<ReadOnlyTask> filteredTasks;
    private final History history;
    private UserPrefs userPrefs;

    /**
     * Initializes a ModelManager with the given taskManager and userPrefs.
     *
     * @throws IllegalValueException
     */
    public ModelManager(ReadOnlyTaskManager taskManager, UserPrefs userPrefs) {
        super();
        assert !CollectionUtil.isAnyNull(taskManager, userPrefs);

        logger.fine("Initializing with task manager: " + taskManager + " and user prefs " + userPrefs);

        this.taskManager = new TaskManager(taskManager);
        filteredTasks = new FilteredList<>(this.taskManager.getTaskList());

        this.userPrefs = userPrefs;
        this.history = History.getInstance();
    }

    public ModelManager() {
        this(new TaskManager(), new UserPrefs());
    }

    // @@author A0140063X
    /**
     * Resets data of taskManager.
     */
    @Override
    public void resetData(ReadOnlyTaskManager newData) throws IllegalValueException {
        taskManager.resetData(newData);
        indicateTaskManagerChanged(history.getBackupFilePath());
    }

    // @@author A0140063X
    /**
     * Load data into taskManager. Used by Undo/Redo Command.
     */
    @Override
    public void loadData(ReadOnlyTaskManager newData) throws IllegalValueException {
        taskManager.resetData(newData);
        raise(new TaskManagerChangedEvent(taskManager, history.getBackupFilePath()));
    }

    // @@author A0140063X
    /**
     *
     * @return taskManager of model.
     */
    @Override
    public ReadOnlyTaskManager getTaskManager() {
        return taskManager;
    }

    // @@author A0140063X
    /**
     * Raises an event to indicate the model has changed.
     *
     * @param backupFilePath
     *            File path to back up into.
     */
    private void indicateTaskManagerChanged(String backupFilePath) {
        history.handleTaskManagerChanged(backupFilePath);
        raise(new TaskManagerChangedEvent(taskManager, backupFilePath));
    }

    // @@author
    /** Raises an event to indicate the file path has changed */
    private void indicateFilePathChanged(String newPath) {
        raise(new FilePathChangedEvent(newPath, taskManager));
    }

    @Override
    public synchronized void deleteTask(ReadOnlyTask target) throws TaskNotFoundException {
        taskManager.removeTask(target);
        indicateTaskManagerChanged(history.getBackupFilePath());
    }

    // @@author A0139975J
    @Override
    public synchronized void isDoneTask(int index) throws TaskNotFoundException {
        int taskManagerIndex = filteredTasks.getSourceIndex(index);
        taskManager.updateDone(taskManagerIndex);
        indicateTaskManagerChanged(history.getBackupFilePath());
    }

    // @@author A0139975J
    @Override
    public synchronized void unDoneTask(int index) throws TaskNotFoundException {
        int taskManagerIndex = filteredTasks.getSourceIndex(index);
        taskManager.updateUnDone(taskManagerIndex);
        indicateTaskManagerChanged(history.getBackupFilePath());
    }

    // @@author A0140063X
    /**
     *
     * @param target
     *            Target task to change.
     * @param eventId
     *            Event id to change into.
     */
    @Override
    public void setTaskEventId(ReadOnlyTask target, String eventId)
            throws TaskNotFoundException, IllegalValueException {
        taskManager.setTaskEventId(target, eventId);
        indicateTaskManagerChanged("");
    }

    // @@author
    @Override
    public synchronized void addTask(Task task) throws UniqueTaskList.DuplicateTaskException {
        taskManager.addTaskToFront(task);
        updateFilteredListToShowAll();
        indicateTaskManagerChanged(history.getBackupFilePath());
    }

    // @@author A0140063X
    /**
     * This method adds every task in tasks to model.
     *
     * @param tasks
     *            ArrayList of task to add.
     */
    @Override
    public void addMultipleTasks(ArrayList<Task> tasks) {
        for (Task task : tasks) {
            try {
                taskManager.addTaskToFront(task);
            } catch (DuplicateTaskException e) {
                logger.info("Duplicate task " + task.getName() + " not added.");
            }
        }

        updateFilteredListToShowAll();
        indicateTaskManagerChanged(history.getBackupFilePath());
    }

    // @@author
    @Override
    public void updateTask(int filteredTaskListIndex, ReadOnlyTask editedTask) throws IllegalValueException {
        assert editedTask != null;

        int taskManagerIndex = filteredTasks.getSourceIndex(filteredTaskListIndex);
        taskManager.updateTask(taskManagerIndex, editedTask);
        indicateTaskManagerChanged(history.getBackupFilePath());
    }

    @Override
    public void sortTaskList() {
        taskManager.sortTaskList();
        indicateTaskManagerChanged("");
    }

    //@@author A0142939W
    @Override
    public void changeFilePath(String newPath) {
        indicateFilePathChanged(newPath);
        indicateTaskManagerChanged("");
    }

    //@@author A0142939W
    @Override
    public void loadFromLocation(String loadPath) {
        indicateFilePathChanged(loadPath);
    }
    //@@author

    // =========== Filtered Task List Accessors =============================================================

    @Override
    public UnmodifiableObservableList<ReadOnlyTask> getFilteredTaskList() {
        return new UnmodifiableObservableList<>(filteredTasks);
    }

{'fraction_non_alphanumeric': 0.09733216065669892, 'fraction_numerical': 0.009234828496042216, 'mean_word_length': 3.1855828220858897, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '5444139', 'n_tokens_mistral': 3673, 'n_tokens_neox': 3576, 'n_words': 793}