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