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 *
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    public AbstractIOTask() {
018        canceled = false;
019        failed = false;
020        lastException = null;
021    }
022
023    /**
024     * Replies true if the task has been canceled
025     *
026     * @return true if the task has been canceled
027     */
028    public boolean isCanceled() {
029        return canceled;
030    }
031
032    /**
033     * Set whether this task has been canceled
034     *
035     * @param canceled true, if the task has been canceled; false otherwise
036     */
037    protected void setCanceled(boolean canceled) {
038        this.canceled = canceled;
039    }
040
041    /**
042     * Replies true if the task has been failed
043     *
044     * @return true if the task has been failed
045     */
046    public boolean isFailed() {
047        return failed || lastException != null;
048    }
049
050    /**
051     * Sets whether the task has been failed
052     *
053     * @param failed whether the task has been failed
054     */
055    protected void setFailed(boolean failed) {
056        this.failed = failed;
057    }
058
059    /**
060     * Replies the last exception caught
061     *
062     * @return the last exception caught; null, if no exception was caught
063     */
064    public Exception getLastException() {
065        return lastException;
066    }
067
068    /**
069     * Sets the last exception caught
070     *
071     * @param lastException the last exception
072     */
073    protected void setLastException(Exception lastException) {
074        this.lastException = lastException;
075    }
076
077    /**
078     * Replies true if this  task was successful, i.e. if it wasn't
079     * canceled and didn't fail
080     *
081     * @return true if this  task was successful
082     */
083    public boolean isSuccessful() {
084        return !isCanceled() && !isFailed();
085    }
086
087    /**
088     * Runs the task
089     */
090    @Override
091    public abstract void run();
092
093    /**
094     * Cancel the task
095     */
096    public abstract void cancel();
097}