001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.io;
003
004/**
005 * This is an abstract task for uploading or saving a data layer.
006 * @since 2025
007 */
008public abstract class AbstractIOTask implements Runnable {
009
010    /** indicates whether the task has been canceled */
011    private boolean canceled;
012    /** indicates whether the task has been failed */
013    private boolean failed;
014    /** the last exception caught */
015    private Exception lastException;
016
017    /**
018     * Constructs a new {@code AbstractIOTask}.
019     */
020    protected AbstractIOTask() {
021        canceled = false;
022        failed = false;
023        lastException = null;
024    }
025
026    /**
027     * Replies true if the task has been canceled
028     *
029     * @return true if the task has been canceled
030     */
031    public boolean isCanceled() {
032        return canceled;
033    }
034
035    /**
036     * Set whether this task has been canceled
037     *
038     * @param canceled true, if the task has been canceled; false otherwise
039     */
040    protected void setCanceled(boolean canceled) {
041        this.canceled = canceled;
042    }
043
044    /**
045     * Replies true if the task has been failed
046     *
047     * @return true if the task has been failed
048     */
049    public boolean isFailed() {
050        return failed || lastException != null;
051    }
052
053    /**
054     * Sets whether the task has been failed
055     *
056     * @param failed whether the task has been failed
057     */
058    protected void setFailed(boolean failed) {
059        this.failed = failed;
060    }
061
062    /**
063     * Replies the last exception caught
064     *
065     * @return the last exception caught; null, if no exception was caught
066     */
067    public Exception getLastException() {
068        return lastException;
069    }
070
071    /**
072     * Sets the last exception caught
073     *
074     * @param lastException the last exception
075     */
076    protected void setLastException(Exception lastException) {
077        this.lastException = lastException;
078    }
079
080    /**
081     * Replies true if this  task was successful, i.e. if it wasn't
082     * canceled and didn't fail
083     *
084     * @return true if this  task was successful
085     */
086    public boolean isSuccessful() {
087        return !isCanceled() && !isFailed();
088    }
089
090    /**
091     * Cancel the task
092     */
093    public abstract void cancel();
094}