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     * @param lockLogger the logger on which to emit status messages
026     * @return True if lock is clear and operation permitted
027     */
028    boolean isLockClear(LockLogger lockLogger);
029
030    /**
031     * Check a collection of Locks, handling the logging etc as needed.
032     * @param locks collection of locks.
033     * @param lockLogger the logger on which to emit status messages
034     * @return false if a lock is not clear, else true.
035     */
036    static boolean checkLocksClear(List<Lock> locks, LockLogger lockLogger) {
037        lockLogger.clear();
038        if (locks != null) {
039            for (Lock lock : locks) {
040                if ( ! lock.isLockClear(lockLogger)) return false; // return immediately so that lockLogger isn't overwritten
041            }
042        }
043        return true;
044    }
045
046    // static while we decide whether to access via scripts
047    LockLogger signalLockLogger  = new LockLogger("IMUSS CTC:SIGNAL LOCK:1:LOG"){
048
049        @edu.umd.cs.findbugs.annotations.SuppressFBWarnings( value="SLF4J_FORMAT_SHOULD_BE_CONST",
050        justification="Status provided by implementing class.")
051        @Override
052        void log(String message) {
053            log.info(message);
054        }
055    };
056    LockLogger turnoutLockLogger = new LockLogger("IMUSS CTC:TURNOUT LOCK:1:LOG"){
057
058        @edu.umd.cs.findbugs.annotations.SuppressFBWarnings( value="SLF4J_FORMAT_SHOULD_BE_CONST",
059        justification="Status provided by implementing class.")
060        @Override
061        void log(String message) {
062            log.info(message);
063        }
064    };
065
066    LockLogger debugLockLogger = new LockLogger("IMUSS CTC:DEBUG LOCK:1:LOG");
067}