001package jmri;
002
003import jmri.implementation.AbstractTurnout;
004
005/**
006 * Specialization of TurnoutOperation to contain
007 * common properties and methods for concrete subclasses.
008 *
009 * @author John Harper
010 */
011public abstract class CommonTurnoutOperation extends TurnoutOperation {
012
013    /*
014     * Parameters of this object
015     */
016    int interval;     // time between attempts
017    int maxTries;     // no of times to try
018
019    /*
020     * Default values and constraints
021     */
022    static public final int minInterval = 100;
023    static public final int maxInterval = 5000;  // let's not get silly...
024    static public final int intervalStepSize = 50;
025    static public final int minMaxTries = 1;
026    static public final int maxMaxTries = 10;
027
028    /**
029     * Create common properties for Turnout Operation.
030     * @param name Operator Name.
031     * @param interval Interval between retries.
032     * @param maxTries maximum retry attempts.
033     */
034    public CommonTurnoutOperation(String name, int interval, int maxTries) {
035        super(name);
036        init(interval, maxTries);
037    }
038
039    private void init(int interval, int maxTries) {
040        setInterval(getDefaultInterval()); // set to default just in case
041        setInterval(interval); // this might not be a valid number.
042        setMaxTries(getDefaultMaxTries()); // set to default just in case
043        setMaxTries(maxTries); // this might not be a valid number.
044        InstanceManager.getDefault(TurnoutOperationManager.class).addOperation(this);
045    }
046
047    /**
048     * Get a TurnoutOperator instance for this operation.
049     *
050     * @return the operator
051     */
052    @Override
053    public abstract TurnoutOperator getOperator(AbstractTurnout t);
054
055    public int getInterval() {
056        return interval;
057    }
058
059    public int getMaxTries() {
060        return maxTries;
061    }
062
063    public abstract int getDefaultInterval();
064
065    public abstract int getDefaultMaxTries();
066
067    @Override
068    public boolean equivalentTo(TurnoutOperation other) {
069        if (other!= null && this.getClass() == other.getClass()) {
070            return interval == ((CommonTurnoutOperation) other).interval
071                    && maxTries == ((CommonTurnoutOperation) other).maxTries;
072        } else {
073            return false;
074        }
075    }
076
077    /**
078     * Set new value for interval. Do nothing if not in range.
079     *
080     * @param newInterval new retry interval time
081     */
082    public void setInterval(int newInterval) {
083        if (newInterval >= minInterval && newInterval <= maxInterval) {
084            interval = newInterval;
085            return;
086        }
087        log.warn("Could not set {} Interval to {}, out of range {}-{}",
088            name, newInterval, minInterval, maxInterval);
089    }
090
091    /**
092     * Set new value for MaxTries. Do nothing if not in range.
093     *
094     * @param newMaxTries new maximum number of retries
095     */
096    public void setMaxTries(int newMaxTries) {
097        if (newMaxTries >= minMaxTries && newMaxTries <= maxMaxTries) {
098            maxTries = newMaxTries;
099        }
100    }
101    
102    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CommonTurnoutOperation.class);
103
104}