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