Package jmri.jmrit.ctc
Class NBHSensor
java.lang.Object
jmri.jmrit.ctc.NBHSensor
This object attempts to "extend" (implements) Sensor functionality that
is needed by the CTC system.
Goals:
- Catches and thereby prevents exceptions from propagating upward. No need for this everywhere in your code: try { sensor.getBean().setKnownState(Sensor.ACTIVE); } catch (JmriException ex) {} We ASSUME here that you are ALWAYS passing a valid "newState". If not, then this call is effectively a no-op (do nothing).
- Use ONLY named beans internally for proper JMRI support of renaming objects.
- Allow any CTC Editor routines to mindlessly change the underlying sensor name used WITHOUT affecting any existing run time (CTCRunTime) code. For example: User changes existing IS:XYZ to IS:ABC, which is a different sensor altogether. Everything happens under the covers, and the higher level code that uses these objects do not know the change happened, and they SHOULD continue to function without affect.
- Prevents "null" access to improperly constructed internal objects. For example: If the caller passes invalid parameter(s) to the constructor(s), then this object's internal Sensor will be set to null by the constructor(s). If the USER then attempts to use the CTC panel and the underlying code winds up calling method(s) in here that reference that null sensor, the JMRI system would crash.
- If the internal Sensor is null and you call a function that returns a value, that function will return a "sane" value. See the constants below for general return values in this situation and individual functions for specific info.
- Support for required sensors. If the sensor name doesn't exist or is invalid in ANY way, then it is considered an error, and this object logs it as such. We do NOT create the JMRI object automatically in this situation.
- Support for optional sensors. In my system, a sensor may be optional. If specified, it MUST be valid and exist in the system already.
- In each of the two above situations, ANY error situation will leave this object properly protected.
- My CTC system that uses this object can "mindlessly" call methods on this object at any time, and return "sane" values in ANY error situation and rely on these values being consistent. This prevents the need of calling routines to CONSTANTLY check the internal status of this object, and can rely on those "sane" return values. I'm a lazy programmer, and want to prevent the following at the call site: int blah; if (NBHSensor.valid()) blah = NBHSensor.getKnownState(); or try { blah = NBHSensor.getKnownState()} catch() {} it becomes just: blah = NBHSensor.getKnownState(); You may (and I do) have specific circumstances whereby you need to know the internal state of this object is valid or not, and act on it differently than the default sane values. There is a function "valid()" for that situation.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddPropertyChangeListener(PropertyChangeListener propertyChangeListener) getBean()intbooleanmatchSensor(Sensor sensor) (package private) voidregisterSensor(String sensorName) voidremovePropertyChangeListener(PropertyChangeListener propertyChangeListener) voidsetKnownState(int newState) intFor Unit testing only.booleanvalid()
-
Field Details
-
DEFAULT_SENSOR_STATE_RV
- See Also:
-
-
Constructor Details
-
NBHSensor
-
NBHSensor
-
NBHSensor
-
-
Method Details
-
getBean
-
getBeanHandle
-
matchSensor
-
registerSensor
-
valid
-
getKnownState
-
setKnownState
-
addPropertyChangeListener
-
removePropertyChangeListener
-
getHandleName
- Returns:
- The sensor's handle name.
-
testingGetCountOfPropertyChangeListenersRegistered
For Unit testing only.- Returns:
- Returns the present number of property change listeners registered with us so far.
-