001package jmri.jmrit.ussctc;
002
003import java.util.*;
004import jmri.*;
005
006/**
007 * Lock if any of a list of sensors isn't INACTIVE.
008 *
009 * @author Bob Jacobsen Copyright (C) 2007, 2017
010 */
011public class OccupancyLock implements Lock {
012
013    public OccupancyLock(List<NamedBeanHandle<Sensor>> list) {
014        this.list = list;
015    }
016
017    public OccupancyLock(String[] array) {
018        NamedBeanHandleManager hm = InstanceManager.getDefault(NamedBeanHandleManager.class);
019        SensorManager sm = InstanceManager.getDefault(SensorManager.class);
020
021        list = new ArrayList<>();
022        for (String s : array) list.add(hm.getNamedBeanHandle(s, sm.provideSensor(s)));
023    }
024
025    public OccupancyLock(String sensor) {
026        this(new String[]{sensor});
027    }
028
029    List<NamedBeanHandle<Sensor>> list;
030
031    /**
032     * Test the lock conditions
033     * @return True if lock is clear and operation permitted
034     */
035    @Override
036    public boolean isLockClear(LockLogger lockLogger) {
037        for (NamedBeanHandle<Sensor> handle : list) {
038            if (handle.getBean().getState() != Sensor.INACTIVE) {
039                lockLogger.setStatus(this, "Locked due to occupancy: "+handle.getBean().getDisplayName());
040                return false;
041            }
042        }
043        lockLogger.setStatus(this, "");
044        return true;
045    }
046
047    @Override
048    public String toString() {
049        String retval = isLockClear(debugLockLogger) ? "clear " : "locked";
050        retval = retval+debugLockLogger.memory.getValue();
051        return retval;
052    }
053
054}