001package jmri.implementation;
002
003import java.beans.PropertyChangeEvent;
004import javax.annotation.OverridingMethodsMustInvokeSuper;
005import jmri.ShutDownTask;
006
007/**
008 * Abstract ShutDownTask implementation.
009 * <p>
010 * This implementation provides a "doRun" property with a protected getter and
011 * setter to allow subclasses to set the "doRun" property to true inside
012 * {@link #call()} so that the property can be checked inside {@link #run()} to
013 * determine if anything should be done during shut down.
014 *
015 * @author Bob Jacobsen Copyright (C) 2008
016 * @author Randall Wood Copyright 2020
017 */
018public abstract class AbstractShutDownTask implements ShutDownTask {
019
020    private final String mName;
021    private boolean doRun = false;
022
023    /**
024     * Constructor specifies the name
025     *
026     * @param name Name to give this task
027     */
028    public AbstractShutDownTask(String name) {
029        this.mName = name;
030    }
031
032    /**
033     * {@inheritDoc}
034     *
035     * This implementation merely sets the "doRun" property to true, and should
036     * be overridden for any real checking. Note that overriding implementations
037     * should call {@link #setDoRun(boolean)} correctly.
038     */
039    @Override
040    public Boolean call() {
041        doRun = true;
042        return doRun;
043    }
044
045    @Override
046    public String getName() {
047        return mName;
048    }
049
050    @Override
051    public String toString() {
052        return "ShutDownTask: " + getName();
053    }
054
055    /**
056     * {@inheritDoc}
057     *
058     * Note that overriding implementations should call this implementation to set
059     * the doRun property correctly.
060     */
061    @OverridingMethodsMustInvokeSuper
062    @Override
063    public void propertyChange(PropertyChangeEvent evt) {
064        if ("shuttingDown".equals(evt.getPropertyName()) && Boolean.FALSE.equals(evt.getNewValue())) {
065            doRun = false;
066        }
067    }
068
069    /**
070     * Check if action should be taken in {@link #run()} method. This defaults
071     * to false, although the default implementation of {@link #call()} sets
072     * this to true.
073     *
074     * @return true if action should be taken; false otherwise
075     */
076    public boolean isDoRun() {
077        return doRun;
078    }
079
080    /**
081     * Set if action should be taken in {@link #run()} method. Overriding
082     * implementations of {@link #call()} must call this to set
083     * {@link #isDoRun()} to true.
084       *
085     * @param flag true if action should be taken; false otherwise
086     */
087    public void setDoRun(boolean flag) {
088        doRun = flag;
089    }
090
091    // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractShutDownTask.class);
092}