001package jmri.jmrit.logixng.actions;
002
003import jmri.InstanceManager;
004import jmri.JmriException;
005import jmri.Manager;
006import jmri.jmrit.logixng.implementation.AbstractBase;
007import jmri.jmrit.logixng.Base;
008import jmri.jmrit.logixng.DigitalBooleanActionManager;
009import jmri.jmrit.logixng.DigitalBooleanActionBean;
010
011/**
012 * The base class for LogixNG Boolean Actions
013 *
014 * @author Daniel Bergqvist Copyright 2018
015 */
016public abstract class AbstractDigitalBooleanAction extends AbstractBase
017        implements DigitalBooleanActionBean {
018
019    private Base _parent = null;
020    private int _state = DigitalBooleanActionBean.UNKNOWN;
021
022
023    public AbstractDigitalBooleanAction(String sys, String user)
024            throws BadUserNameException, BadSystemNameException {
025        super(sys, user);
026
027        // Do this test here to ensure all the tests are using correct system names
028        Manager.NameValidity isNameValid = InstanceManager.getDefault(DigitalBooleanActionManager.class).validSystemNameFormat(mSystemName);
029        if (isNameValid != Manager.NameValidity.VALID) {
030            throw new IllegalArgumentException("system name is not valid: "+mSystemName);
031        }
032    }
033
034    /** {@inheritDoc} */
035    @Override
036    public Base getParent() {
037        return _parent;
038    }
039
040    /** {@inheritDoc} */
041    @Override
042    public void setParent(Base parent) {
043        _parent = parent;
044    }
045
046    public String getNewSocketName() {
047        int x = 1;
048        while (x < 10000) {     // Protect from infinite loop
049            String name = "A" + Integer.toString(x);
050            boolean validName = true;
051            for (int i=0; i < getChildCount(); i++) {
052                if (name.equals(getChild(i).getName())) {
053                    validName = false;
054                    break;
055                }
056            }
057            if (validName) {
058                return name;
059            }
060            x++;
061        }
062        throw new RuntimeException("Unable to find a new socket name");
063    }
064
065    @Override
066    public String getBeanType() {
067        return Bundle.getMessage("BeanNameDigitalBooleanAction");
068    }
069
070    @Override
071    public void setState(int s) throws JmriException {
072        log.warn("Unexpected call to setState in AbstractDigitalBooleanAction.");  // NOI18N
073        _state = s;
074    }
075
076    @Override
077    public int getState() {
078        log.warn("Unexpected call to getState in AbstractDigitalBooleanAction.");  // NOI18N
079        return _state;
080    }
081
082
083    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AbstractDigitalBooleanAction.class);
084}