001package jmri.jmrit.ussctc;
002
003import java.util.*;
004import jmri.*;
005
006/**
007 * Lock if any of the SignalHeadSections controlling traffic are running time.
008 *
009 * @author Bob Jacobsen Copyright (C) 2007, 2017
010 */
011public class TimeLock implements Lock {
012
013    /**
014     * @param list SignalHeadSections that cover this route
015     */
016    public TimeLock(List<SignalHeadSection> list) {
017        this.list = list;
018        this.beans = null;
019    }
020
021    /**
022     * @param list SignalHeadSections that cover this route
023     * @param beans Defines the specific route
024     */
025    public TimeLock(List<SignalHeadSection> list, List<BeanSetting> beans) {
026        this.list = list;
027        this.beans = beans;
028    }
029
030    /**
031     * @param array SignalHeadSections that cover this route
032     * @param beans Defines the specific route
033     */
034    public TimeLock(SignalHeadSection[] array, BeanSetting[] beans) {
035        list = new ArrayList<>();
036        for (SignalHeadSection s : array) list.add(s);
037
038        this.beans = new ArrayList<>();
039        for (BeanSetting bean : beans) this.beans.add(bean);
040
041    }
042
043    /**
044     * @param array SignalHeadSections that cover this route
045     */
046    public TimeLock(SignalHeadSection[] array) {
047        list = new ArrayList<>();
048        for (SignalHeadSection s : array) list.add(s);
049
050        this.beans = null;
051    }
052
053    /**
054     * @param head SignalHeadSection that covers this route
055     */
056    public TimeLock(SignalHeadSection head) {
057        this(new SignalHeadSection[]{head});
058    }
059
060    List<SignalHeadSection> list;
061    List<BeanSetting> beans;
062
063    /**
064     * Test the lock conditions
065     * @return True if lock is clear and operation permitted
066     */
067    @Override
068    public boolean isLockClear(LockLogger lockLogger) {
069        // if this route isn't in effect, then permitted
070        if (beans != null) {
071            for (BeanSetting bean : beans) {
072                if ( ! bean.check()) {
073                    lockLogger.setStatus(this, "");
074                    return true;
075                }
076            }
077        }
078
079        for (SignalHeadSection section : list) {
080            if (section.isRunningTime()) {
081                lockLogger.setStatus(this, "Locked: Station "+section.getStation().getName()+" running time");
082                return false;
083            }
084        }
085        lockLogger.setStatus(this, "");
086        return true;
087    }
088
089    @Override
090    public String toString() {
091        String retval = isLockClear(debugLockLogger) ? "clear " : "locked";
092        retval = retval+debugLockLogger.memory.getValue();
093        return retval;
094    }
095}