001package jmri.jmrit.ussctc;
002
003import java.util.*;
004
005/**
006 * A Lock is the base interface for implementations that check layout conditions.
007 * <p>
008 * Locks are used in multiple places: Machine and Field.
009 * They can be used to lock out various operations: Turnout, Signal.
010 * Those contexts are handled in how Locks are configured into other objects.
011 *
012 * @author Bob Jacobsen Copyright (C) 2007, 2017
013 */
014public interface Lock {
015
016    enum Valid {
017        FIELD_TURNOUT,
018        FIELD_SIGNAL,
019        MACHINE_TURNOUT,
020        MACHINE_SIGNAL
021    }
022    
023    /**
024     * Test the lock conditions
025     * @return True if lock is clear and operation permitted
026     */
027    public boolean isLockClear();  
028    
029    /**
030     * Check a collection of Locks, handling the logging etc as needed.
031     * @param locks collection of locks.
032     * @return false if a lock is not clear, else true.
033     */
034    static public boolean checkLocksClear(List<Lock> locks) {
035        lockLogger.clear();
036        boolean permitted = true;
037        if (locks != null) {
038            for (Lock lock : locks) {
039                if ( ! lock.isLockClear()) permitted = false;
040            }
041        }
042        return permitted;
043    }
044
045    // static while we decide whether to access via scripts
046    final static LockLogger lockLogger = new LockLogger();
047}