<commit_before>/*
 * Copyright (c) 2008-2016 Haulmont.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.haulmont.cuba.gui.executors;

import com.haulmont.cuba.gui.components.Frame;
import com.haulmont.cuba.gui.components.Window;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Background task for execute by {@link BackgroundWorker}.
 * <p/> If the task is associated with a screen through ownerFrame constructor parameter, it will be canceled when
 * the screen is closed.
 * <p/> If timeout passed to constructor is exceeded, the task is canceled by special {@link WatchDog} thread.
 *
 * <p/> Simplest usage example:
 * <pre>
 *    BackgroundTask<Integer, Void> task = new BackgroundTask<Integer, Void>(10, this) {
 *        public Void run(TaskLifeCycle<Integer> taskLifeCycle) throws Exception {
 *            for (int i = 0; i < 5; i++) {
 *                TimeUnit.SECONDS.sleep(1);
 *            }
 *            return null;
 *        }
 *    };
 *    BackgroundTaskHandler taskHandler = backgroundWorker.handle(task);
 *    taskHandler.execute();
 * </pre>
 *
 * @param <T> task progress measurement unit
 * @param <V> result type
 */
@SuppressWarnings("unused")
public abstract class BackgroundTask<T, V> {

    private final Frame ownerFrame;

    private final long timeoutMilliseconds;

    private final List<ProgressListener<T, V>> progressListeners = Collections.synchronizedList(new ArrayList<>());

    /**
     * Create a task with timeout.
     *
     * @param timeout     timeout
     * @param timeUnit    timeout time unit
     * @param ownerFrame  owner frame
     */
    protected BackgroundTask(long timeout, TimeUnit timeUnit, Frame ownerFrame) {
        this.ownerFrame = ownerFrame;
        this.timeoutMilliseconds = timeUnit.toMillis(timeout);
    }

    /**
     * Create a task with timeout.
     * <p/> The task will not be associated with any window.
     *
     * @param timeout  timeout
     * @param timeUnit timeout time unit
     */
    protected BackgroundTask(long timeout, TimeUnit timeUnit) {
        this.ownerFrame = null;
        this.timeoutMilliseconds = timeUnit.toMillis(timeout);
    }

    /**
     * Create a task with timeout in default SECONDS unit.
     * <p/> The task will not be associated with any window.
     *
     * @param timeoutSeconds timeout in seconds
     */
    protected BackgroundTask(long timeoutSeconds) {
        this.ownerFrame = null;
        this.timeoutMilliseconds = TimeUnit.SECONDS.toMillis(timeoutSeconds);
    }

    /**
     * Create a task with timeout in default SECONDS unit.
     *
     * @param timeoutSeconds timeout in seconds
     * @param ownerWindow    owner window
     */
    protected BackgroundTask(long timeoutSeconds, Window ownerWindow) {
        this.ownerFrame = ownerWindow;
        this.timeoutMilliseconds = TimeUnit.SECONDS.toMillis(timeoutSeconds);
    }

    /**
     * Main method that performs a task.<commit_msg>PL-7649 Inconsistent BackgroundTask API - different constructors require Window or Frame<commit_after>/*
 * Copyright (c) 2008-2016 Haulmont.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.haulmont.cuba.gui.executors;

import com.haulmont.cuba.gui.components.Frame;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Background task for execute by {@link BackgroundWorker}.
 * <p/> If the task is associated with a screen through ownerFrame constructor parameter, it will be canceled when
 * the screen is closed.
 * <p/> If timeout passed to constructor is exceeded, the task is canceled by special {@link WatchDog} thread.
 *
 * <p/> Simplest usage example:
 * <pre>
 *    BackgroundTask<Integer, Void> task = new BackgroundTask<Integer, Void>(10, this) {
 *        public Void run(TaskLifeCycle<Integer> taskLifeCycle) throws Exception {
 *            for (int i = 0; i < 5; i++) {
 *                TimeUnit.SECONDS.sleep(1);
 *            }
 *            return null;
 *        }
 *    };
 *    BackgroundTaskHandler taskHandler = backgroundWorker.handle(task);
 *    taskHandler.execute();
 * </pre>
 *
 * @param <T> task progress measurement unit
 * @param <V> result type
 */
@SuppressWarnings("unused")
public abstract class BackgroundTask<T, V> {

    private final Frame ownerFrame;

    private final long timeoutMilliseconds;

    private final List<ProgressListener<T, V>> progressListeners = Collections.synchronizedList(new ArrayList<>());

    /**
     * Create a task with timeout.
     *
     * @param timeout     timeout
     * @param timeUnit    timeout time unit
     * @param ownerFrame  owner frame
     */
    protected BackgroundTask(long timeout, TimeUnit timeUnit, Frame ownerFrame) {
        this.ownerFrame = ownerFrame;
        this.timeoutMilliseconds = timeUnit.toMillis(timeout);
    }

    /**
     * Create a task with timeout.
     * <p/> The task will not be associated with any window.
     *
     * @param timeout  timeout
     * @param timeUnit timeout time unit
     */
    protected BackgroundTask(long timeout, TimeUnit timeUnit) {
        this.ownerFrame = null;
        this.timeoutMilliseconds = timeUnit.toMillis(timeout);
    }

    /**
     * Create a task with timeout in default SECONDS unit.
     * <p/> The task will not be associated with any window.
     *
     * @param timeoutSeconds timeout in seconds
     */
    protected BackgroundTask(long timeoutSeconds) {
        this.ownerFrame = null;
        this.timeoutMilliseconds = TimeUnit.SECONDS.toMillis(timeoutSeconds);
    }

    /**
     * Create a task with timeout in default SECONDS unit.
     *
     * @param timeoutSeconds timeout in seconds
     * @param ownerFrame     owner frame
     */
    protected BackgroundTask(long timeoutSeconds, Frame ownerFrame) {
        this.ownerFrame = ownerFrame;
        this.timeoutMilliseconds = TimeUnit.SECONDS.toMillis(timeoutSeconds);
    }

    /**
     * Main method that performs a task.
{'fraction_non_alphanumeric': 0.09188063844552394, 'fraction_numerical': 0.00527411519777932, 'mean_word_length': 2.9922437673130196, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4441888', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1915, 'n_words': 676}