Provide more extensive listener features.

This enables listeners to be notified of not only value updates, but also flag
changes and deletions by using a bitmask to specify what notifications are
desired.  The old API (which only provided a new/not new) flag is still
supported.  This also subsumes the feature to listen to local changes (that's
one of the bitmask options).
This commit is contained in:
Peter Johnson
2015-09-25 11:54:17 -07:00
parent 51064f5e75
commit 90959defd9
17 changed files with 246 additions and 128 deletions

View File

@@ -297,6 +297,14 @@ public interface ITable {
*/
public String[] getStringArray(String key, String[] defaultValue);
/** Notifier flag values. */
public static final int NOTIFY_IMMEDIATE = 0x01;
public static final int NOTIFY_LOCAL = 0x02;
public static final int NOTIFY_NEW = 0x04;
public static final int NOTIFY_DELETE = 0x08;
public static final int NOTIFY_UPDATE = 0x10;
public static final int NOTIFY_FLAGS = 0x20;
/**
* Add a listener for changes to the table
* @param listener the listener to add
@@ -313,13 +321,9 @@ public interface ITable {
/**
* Add a listener for changes to the table
* @param listener the listener to add
* @param immediateNotify if true then this listener will be notified of all
* current entries (marked as new)
* @param localNotify if true then this listener will be notified of all
* local changes in addition to all remote changes
* @param flags bitmask specifying desired notifications
*/
public void addTableListener(ITableListener listener, boolean immediateNotify,
boolean localNotify);
public void addTableListenerEx(ITableListener listener, int flags);
/**
* Add a listener for changes to a specific key the table
@@ -334,13 +338,10 @@ public interface ITable {
* Add a listener for changes to a specific key the table
* @param key the key to listen for
* @param listener the listener to add
* @param immediateNotify if true then this listener will be notified of all
* current entries (marked as new)
* @param localNotify if true then this listener will be notified of all
* local changes in addition to all remote changes
* @param flags bitmask specifying desired notifications
*/
public void addTableListener(String key, ITableListener listener,
boolean immediateNotify, boolean localNotify);
public void addTableListenerEx(String key, ITableListener listener,
int flags);
/**
* This will immediately notify the listener of all current sub tables
* @param listener

View File

@@ -9,11 +9,26 @@ package edu.wpi.first.wpilibj.tables;
public interface ITableListener {
/**
* Called when a key-value pair is changed in a {@link ITable}
* WARNING: If a new key-value is put in this method value changed will immediatly be called which could lead to recursive code
* @param source the table the key-value pair exists in
* @param key the key associated with the value that changed
* @param value the new value
* @param isNew true if the key did not previously exist in the table, otherwise it is false
*/
public void valueChanged(ITable source, String key, Object value, boolean isNew);
/**
* Extended version of valueChanged. Called when a key-value pair is
* changed in a {@link ITable}. The default implementation simply calls
* valueChanged(). If this is overridden, valueChanged() will not be
* called.
* @param source the table the key-value pair exists in
* @param key the key associated with the value that changed
* @param value the new value
* @param flags update flags; for example, NOTIFY_NEW if the key did not
* previously exist in the table
*/
default public void valueChangedEx(ITable source, String key, Object value, int flags) {
// NOTIFY_NEW = 0x04
valueChanged(source, key, value, (flags & 0x04) != 0);
}
}