diff --git a/ntcore/src/generate/java/NetworkTableInstance.java.jinja b/ntcore/src/generate/java/NetworkTableInstance.java.jinja index 47158fd6026..d94787c429c 100644 --- a/ntcore/src/generate/java/NetworkTableInstance.java.jinja +++ b/ntcore/src/generate/java/NetworkTableInstance.java.jinja @@ -7,6 +7,7 @@ package edu.wpi.first.networktables; import edu.wpi.first.util.WPIUtilJNI; import edu.wpi.first.util.concurrent.Event; import edu.wpi.first.util.datalog.DataLog; +import java.util.EnumSet; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -35,16 +36,33 @@ import java.util.function.Consumer; */ @SuppressWarnings("PMD.CouplingBetweenObjects") public final class NetworkTableInstance implements AutoCloseable { - /** - * Client/server mode flag values (as returned by {@link #getNetworkMode()}). This is a bitmask. - */ - public static final int kNetModeNone = 0x00; + /** Client/server mode flag values (as returned by {@link #getNetworkMode()}). */ + public enum NetworkMode { + /** Running in server mode. */ + kServer(0x01), + + /** Running in NT3 client mode. */ + kClient3(0x02), + + /** Running in NT4 client mode. */ + kClient4(0x04), + + /** Currently starting up (either client or server). */ + kStarting(0x08), + + /** Running in local-only mode. */ + kLocal(0x10); + + private final int value; + + NetworkMode(int value) { + this.value = value; + } - public static final int kNetModeServer = 0x01; - public static final int kNetModeClient3 = 0x02; - public static final int kNetModeClient4 = 0x04; - public static final int kNetModeStarting = 0x08; - public static final int kNetModeLocal = 0x10; + public int getValue() { + return value; + } + } /** The default port that network tables operates on for NT3. */ public static final int kDefaultPort3 = 1735; @@ -370,14 +388,17 @@ public final class NetworkTableInstance implements AutoCloseable { m_inst = inst; } - int add(String[] prefixes, int mask, Consumer listener) { + int add( + String[] prefixes, + EnumSet eventKinds, + Consumer listener) { m_lock.lock(); try { if (m_poller == 0) { m_poller = NetworkTablesJNI.createListenerPoller(m_inst.getHandle()); startThread(); } - int h = NetworkTablesJNI.addListener(m_poller, prefixes, mask); + int h = NetworkTablesJNI.addListener(m_poller, prefixes, eventKinds); m_listeners.put(h, listener); return h; } finally { @@ -385,14 +406,17 @@ public final class NetworkTableInstance implements AutoCloseable { } } - int add(int handle, int mask, Consumer listener) { + int add( + int handle, + EnumSet eventKinds, + Consumer listener) { m_lock.lock(); try { if (m_poller == 0) { m_poller = NetworkTablesJNI.createListenerPoller(m_inst.getHandle()); startThread(); } - int h = NetworkTablesJNI.addListener(m_poller, handle, mask); + int h = NetworkTablesJNI.addListener(m_poller, handle, eventKinds); m_listeners.put(h, listener); return h; } finally { @@ -563,9 +587,11 @@ public final class NetworkTableInstance implements AutoCloseable { */ public int addConnectionListener( boolean immediateNotify, Consumer listener) { - return m_listeners.add(m_handle, - NetworkTableEvent.kConnection | (immediateNotify ? NetworkTableEvent.kImmediate : 0), - listener); + EnumSet eventKinds = EnumSet.of(NetworkTableEvent.Kind.kConnection); + if (immediateNotify) { + eventKinds.add(NetworkTableEvent.Kind.kImmediate); + } + return m_listeners.add(m_handle, eventKinds, listener); } /** @@ -577,16 +603,18 @@ public final class NetworkTableInstance implements AutoCloseable { * listener. * * @param topic Topic - * @param eventMask Bitmask of TopicListenerFlags values + * @param eventKinds set of event kinds to listen to * @param listener Listener function * @return Listener handle */ public int addListener( - Topic topic, int eventMask, Consumer listener) { + Topic topic, + EnumSet eventKinds, + Consumer listener) { if (topic.getInstance().getHandle() != m_handle) { throw new IllegalArgumentException("topic is not from this instance"); } - return m_listeners.add(topic.getHandle(), eventMask, listener); + return m_listeners.add(topic.getHandle(), eventKinds, listener); } /** @@ -596,16 +624,18 @@ public final class NetworkTableInstance implements AutoCloseable { * active. * * @param subscriber Subscriber - * @param eventMask Bitmask of TopicListenerFlags values + * @param eventKinds set of event kinds to listen to * @param listener Listener function * @return Listener handle */ public int addListener( - Subscriber subscriber, int eventMask, Consumer listener) { + Subscriber subscriber, + EnumSet eventKinds, + Consumer listener) { if (subscriber.getTopic().getInstance().getHandle() != m_handle) { throw new IllegalArgumentException("subscriber is not from this instance"); } - return m_listeners.add(subscriber.getHandle(), eventMask, listener); + return m_listeners.add(subscriber.getHandle(), eventKinds, listener); } /** @@ -615,16 +645,18 @@ public final class NetworkTableInstance implements AutoCloseable { * active. * * @param subscriber Subscriber - * @param eventMask Bitmask of TopicListenerFlags values + * @param eventKinds set of event kinds to listen to * @param listener Listener function * @return Listener handle */ public int addListener( - MultiSubscriber subscriber, int eventMask, Consumer listener) { + MultiSubscriber subscriber, + EnumSet eventKinds, + Consumer listener) { if (subscriber.getInstance().getHandle() != m_handle) { throw new IllegalArgumentException("subscriber is not from this instance"); } - return m_listeners.add(subscriber.getHandle(), eventMask, listener); + return m_listeners.add(subscriber.getHandle(), eventKinds, listener); } /** @@ -633,16 +665,18 @@ public final class NetworkTableInstance implements AutoCloseable { * accessing any shared state from the callback function. * * @param entry Entry - * @param eventMask Bitmask of TopicListenerFlags values + * @param eventKinds set of event kinds to listen to * @param listener Listener function * @return Listener handle */ public int addListener( - NetworkTableEntry entry, int eventMask, Consumer listener) { + NetworkTableEntry entry, + EnumSet eventKinds, + Consumer listener) { if (entry.getTopic().getInstance().getHandle() != m_handle) { throw new IllegalArgumentException("entry is not from this instance"); } - return m_listeners.add(entry.getHandle(), eventMask, listener); + return m_listeners.add(entry.getHandle(), eventKinds, listener); } /** @@ -655,15 +689,15 @@ public final class NetworkTableInstance implements AutoCloseable { * listener. * * @param prefixes Topic name string prefixes - * @param eventMask Bitmask of TopicListenerFlags values + * @param eventKinds set of event kinds to listen to * @param listener Listener function * @return Listener handle */ public int addListener( String[] prefixes, - int eventMask, + EnumSet eventKinds, Consumer listener) { - return m_listeners.add(prefixes, eventMask, listener); + return m_listeners.add(prefixes, eventKinds, listener); } /* @@ -673,10 +707,17 @@ public final class NetworkTableInstance implements AutoCloseable { /** * Get the current network mode. * - * @return Bitmask of NetworkMode. + * @return Enum set of NetworkMode. */ - public int getNetworkMode() { - return NetworkTablesJNI.getNetworkMode(m_handle); + public EnumSet getNetworkMode() { + int flags = NetworkTablesJNI.getNetworkMode(m_handle); + EnumSet rv = EnumSet.noneOf(NetworkMode.class); + for (NetworkMode mode : NetworkMode.values()) { + if ((flags & mode.getValue()) != 0) { + rv.add(mode); + } + } + return rv; } /** diff --git a/ntcore/src/generate/java/NetworkTablesJNI.java.jinja b/ntcore/src/generate/java/NetworkTablesJNI.java.jinja index 682951573df..68dc37e2e16 100644 --- a/ntcore/src/generate/java/NetworkTablesJNI.java.jinja +++ b/ntcore/src/generate/java/NetworkTablesJNI.java.jinja @@ -7,6 +7,7 @@ package edu.wpi.first.networktables; import edu.wpi.first.util.RuntimeLoader; import edu.wpi.first.util.datalog.DataLog; import java.io.IOException; +import java.util.EnumSet; import java.util.concurrent.atomic.AtomicBoolean; public final class NetworkTablesJNI { @@ -203,6 +204,22 @@ public final class NetworkTablesJNI { public static native void destroyListenerPoller(int poller); + private static int kindsToMask(EnumSet kinds) { + int mask = 0; + for (NetworkTableEvent.Kind kind : kinds) { + mask |= kind.getValue(); + } + return mask; + } + + public static int addListener(int poller, String[] prefixes, EnumSet kinds) { + return addListener(poller, prefixes, kindsToMask(kinds)); + } + + public static int addListener(int poller, int handle, EnumSet kinds) { + return addListener(poller, handle, kindsToMask(kinds)); + } + public static native int addListener(int poller, String[] prefixes, int mask); public static native int addListener(int poller, int handle, int mask); diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableEvent.java b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableEvent.java index 24fc52027a9..f1cfd5ce84f 100644 --- a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableEvent.java +++ b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableEvent.java @@ -7,52 +7,61 @@ /** * NetworkTables event. * - *

Events have flags. The flags are a bitmask and must be OR'ed together when listening to an - * event to indicate the combination of events desired to be received. + *

There are different kinds of events. When creating a listener, a combination of event kinds + * can be listened to by building an EnumSet of NetworkTableEvent.Kind. */ @SuppressWarnings("MemberName") public final class NetworkTableEvent { - /** No flags. */ - public static final int kNone = 0; + public enum Kind { + /** + * Initial listener addition. Set this to receive immediate notification of matches to other + * criteria. + */ + kImmediate(0x0001), - /** - * Initial listener addition. Set this flag to receive immediate notification of matches to the - * flag criteria. - */ - public static final int kImmediate = 0x01; + /** Client connected (on server, any client connected). */ + kConnected(0x0002), + + /** Client disconnected (on server, any client disconnected). */ + kDisconnected(0x0004), + + /** Any connection event (connect or disconnect). */ + kConnection(0x0004 | 0x0002), - /** Client connected (on server, any client connected). */ - public static final int kConnected = 0x02; + /** New topic published. */ + kPublish(0x0008), - /** Client disconnected (on server, any client disconnected). */ - public static final int kDisconnected = 0x04; + /** Topic unpublished. */ + kUnpublish(0x0010), - /** Any connection event (connect or disconnect). */ - public static final int kConnection = kConnected | kDisconnected; + /** Topic properties changed. */ + kProperties(0x0020), - /** New topic published. */ - public static final int kPublish = 0x08; + /** Any topic event (publish, unpublish, or properties changed). */ + kTopic(0x0020 | 0x0010 | 0x0008), - /** Topic unpublished. */ - public static final int kUnpublish = 0x10; + /** Topic value updated (via network). */ + kValueRemote(0x0040), - /** Topic properties changed. */ - public static final int kProperties = 0x20; + /** Topic value updated (local). */ + kValueLocal(0x0080), - /** Any topic event (publish, unpublish, or properties changed). */ - public static final int kTopic = kPublish | kUnpublish | kProperties; + /** Topic value updated (network or local). */ + kValueAll(0x0080 | 0x0040), - /** Topic value updated (via network). */ - public static final int kValueRemote = 0x40; + /** Log message. */ + kLogMessage(0x0100); - /** Topic value updated (local). */ - public static final int kValueLocal = 0x80; + private final int value; - /** Topic value updated (network or local). */ - public static final int kValueAll = kValueRemote | kValueLocal; + Kind(int value) { + this.value = value; + } - /** Log message. */ - public static final int kLogMessage = 0x100; + public int getValue() { + return value; + } + } /** * Handle of listener that was triggered. The value returned when adding the listener can be used @@ -61,8 +70,8 @@ public final class NetworkTableEvent { public final int listener; /** - * Event flags. For example, kPublish if the topic was not previously published. Also indicates - * the data included with the event: + * Determine if event is of a particular kind. For example, kPublish if the topic was not + * previously published. Also indicates the data included with the event: * *

    *
  • kConnected or kDisconnected: connInfo @@ -70,8 +79,15 @@ public final class NetworkTableEvent { *
  • kValueRemote, kValueLocal: valueData *
  • kLogMessage: logMessage *
+ * + * @param kind Kind + * @return True if event matches kind */ - public final int flags; + public boolean is(Kind kind) { + return (m_flags & kind.getValue()) != 0; + } + + private final int m_flags; /** Connection information (for connection events). */ public final ConnectionInfo connInfo; @@ -106,7 +122,7 @@ public NetworkTableEvent( LogMessage logMessage) { this.m_inst = inst; this.listener = listener; - this.flags = flags; + this.m_flags = flags; this.connInfo = connInfo; this.topicInfo = topicInfo; this.valueData = valueData; diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableListener.java b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableListener.java index a06d871450f..5b61abf4e85 100644 --- a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableListener.java +++ b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableListener.java @@ -4,6 +4,7 @@ package edu.wpi.first.networktables; +import java.util.EnumSet; import java.util.function.Consumer; /** @@ -18,16 +19,16 @@ public final class NetworkTableListener implements AutoCloseable { * * @param inst Instance * @param prefixes Topic name string prefixes - * @param eventMask Bitmask of NetworkTableEvent flags values + * @param eventKinds set of event kinds to listen to * @param listener Listener function * @return Listener */ public static NetworkTableListener createListener( NetworkTableInstance inst, String[] prefixes, - int eventMask, + EnumSet eventKinds, Consumer listener) { - return new NetworkTableListener(inst, inst.addListener(prefixes, eventMask, listener)); + return new NetworkTableListener(inst, inst.addListener(prefixes, eventKinds, listener)); } /** @@ -35,56 +36,64 @@ public static NetworkTableListener createListener( * subscriber with the lifetime of the listener. * * @param topic Topic - * @param eventMask Bitmask of NetworkTableEvent flags values + * @param eventKinds set of event kinds to listen to * @param listener Listener function * @return Listener */ public static NetworkTableListener createListener( - Topic topic, int eventMask, Consumer listener) { + Topic topic, + EnumSet eventKinds, + Consumer listener) { NetworkTableInstance inst = topic.getInstance(); - return new NetworkTableListener(inst, inst.addListener(topic, eventMask, listener)); + return new NetworkTableListener(inst, inst.addListener(topic, eventKinds, listener)); } /** * Create a listener for topic changes on a subscriber. This does NOT keep the subscriber active. * * @param subscriber Subscriber - * @param eventMask Bitmask of NetworkTableEvent flags values + * @param eventKinds set of event kinds to listen to * @param listener Listener function * @return Listener */ public static NetworkTableListener createListener( - Subscriber subscriber, int eventMask, Consumer listener) { + Subscriber subscriber, + EnumSet eventKinds, + Consumer listener) { NetworkTableInstance inst = subscriber.getTopic().getInstance(); - return new NetworkTableListener(inst, inst.addListener(subscriber, eventMask, listener)); + return new NetworkTableListener(inst, inst.addListener(subscriber, eventKinds, listener)); } /** * Create a listener for topic changes on a subscriber. This does NOT keep the subscriber active. * * @param subscriber Subscriber - * @param eventMask Bitmask of NetworkTableEvent flags values + * @param eventKinds set of event kinds to listen to * @param listener Listener function * @return Listener */ public static NetworkTableListener createListener( - MultiSubscriber subscriber, int eventMask, Consumer listener) { + MultiSubscriber subscriber, + EnumSet eventKinds, + Consumer listener) { NetworkTableInstance inst = subscriber.getInstance(); - return new NetworkTableListener(inst, inst.addListener(subscriber, eventMask, listener)); + return new NetworkTableListener(inst, inst.addListener(subscriber, eventKinds, listener)); } /** * Create a listener for topic changes on an entry. * * @param entry Entry - * @param eventMask Bitmask of NetworkTableEvent flags values + * @param eventKinds set of event kinds to listen to * @param listener Listener function * @return Listener */ public static NetworkTableListener createListener( - NetworkTableEntry entry, int eventMask, Consumer listener) { + NetworkTableEntry entry, + EnumSet eventKinds, + Consumer listener) { NetworkTableInstance inst = entry.getInstance(); - return new NetworkTableListener(inst, inst.addListener(entry, eventMask, listener)); + return new NetworkTableListener(inst, inst.addListener(entry, eventKinds, listener)); } /** diff --git a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableListenerPoller.java b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableListenerPoller.java index 105ff770906..b88e805e856 100644 --- a/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableListenerPoller.java +++ b/ntcore/src/main/java/edu/wpi/first/networktables/NetworkTableListenerPoller.java @@ -4,6 +4,8 @@ package edu.wpi.first.networktables; +import java.util.EnumSet; + /** * Topic change listener. This queues topic change events matching the specified mask. Code using * the listener must periodically call readQueue() to read the events. @@ -24,11 +26,11 @@ public NetworkTableListenerPoller(NetworkTableInstance inst) { * prefixes. This creates a corresponding internal subscriber with the lifetime of the listener. * * @param prefixes Topic name string prefixes - * @param eventMask Bitmask of TopicListenerFlags values + * @param eventKinds set of event kinds to listen to * @return Listener handle */ - public int addListener(String[] prefixes, int eventMask) { - return NetworkTablesJNI.addListener(m_handle, prefixes, eventMask); + public int addListener(String[] prefixes, EnumSet eventKinds) { + return NetworkTablesJNI.addListener(m_handle, prefixes, eventKinds); } /** @@ -36,44 +38,44 @@ public int addListener(String[] prefixes, int eventMask) { * subscriber with the lifetime of the listener. * * @param topic Topic - * @param eventMask Bitmask of TopicListenerFlags values + * @param eventKinds set of event kinds to listen to * @return Listener handle */ - public int addListener(Topic topic, int eventMask) { - return NetworkTablesJNI.addListener(m_handle, topic.getHandle(), eventMask); + public int addListener(Topic topic, EnumSet eventKinds) { + return NetworkTablesJNI.addListener(m_handle, topic.getHandle(), eventKinds); } /** * Start listening to topic changes on a subscriber. This does NOT keep the subscriber active. * * @param subscriber Subscriber - * @param eventMask Bitmask of TopicListenerFlags values + * @param eventKinds set of event kinds to listen to * @return Listener handle */ - public int addListener(Subscriber subscriber, int eventMask) { - return NetworkTablesJNI.addListener(m_handle, subscriber.getHandle(), eventMask); + public int addListener(Subscriber subscriber, EnumSet eventKinds) { + return NetworkTablesJNI.addListener(m_handle, subscriber.getHandle(), eventKinds); } /** * Start listening to topic changes on a subscriber. This does NOT keep the subscriber active. * * @param subscriber Subscriber - * @param eventMask Bitmask of TopicListenerFlags values + * @param eventKinds set of event kinds to listen to * @return Listener handle */ - public int addListener(MultiSubscriber subscriber, int eventMask) { - return NetworkTablesJNI.addListener(m_handle, subscriber.getHandle(), eventMask); + public int addListener(MultiSubscriber subscriber, EnumSet eventKinds) { + return NetworkTablesJNI.addListener(m_handle, subscriber.getHandle(), eventKinds); } /** * Start listening to topic changes on an entry. * * @param entry Entry - * @param eventMask Bitmask of TopicListenerFlags values + * @param eventKinds set of event kinds to listen to * @return Listener handle */ - public int addListener(NetworkTableEntry entry, int eventMask) { - return NetworkTablesJNI.addListener(m_handle, entry.getHandle(), eventMask); + public int addListener(NetworkTableEntry entry, EnumSet eventKinds) { + return NetworkTablesJNI.addListener(m_handle, entry.getHandle(), eventKinds); } /** @@ -85,10 +87,11 @@ public int addListener(NetworkTableEntry entry, int eventMask) { * @return Listener handle */ public int addConnectionListener(boolean immediateNotify) { - return NetworkTablesJNI.addListener( - m_handle, - m_inst.getHandle(), - NetworkTableEvent.kConnection | (immediateNotify ? NetworkTableEvent.kImmediate : 0)); + EnumSet eventKinds = EnumSet.of(NetworkTableEvent.Kind.kConnection); + if (immediateNotify) { + eventKinds.add(NetworkTableEvent.Kind.kImmediate); + } + return NetworkTablesJNI.addListener(m_handle, m_inst.getHandle(), eventKinds); } /** diff --git a/ntcore/src/main/native/include/ntcore_cpp.h b/ntcore/src/main/native/include/ntcore_cpp.h index 8c51d60037a..1dd33aea3bc 100644 --- a/ntcore/src/main/native/include/ntcore_cpp.h +++ b/ntcore/src/main/native/include/ntcore_cpp.h @@ -223,6 +223,14 @@ class Event { */ unsigned int flags{0}; + /** + * Test event flags. + * + * @param kind event flag(s) to test + * @return True if flags matches kind + */ + bool Is(unsigned int kind) const { return (flags & kind) != 0; } + /** Event data; content depends on flags. */ std::variant data; diff --git a/ntcore/src/test/java/edu/wpi/first/networktables/ConnectionListenerTest.java b/ntcore/src/test/java/edu/wpi/first/networktables/ConnectionListenerTest.java index 434e9279d51..9fac048e2ea 100644 --- a/ntcore/src/test/java/edu/wpi/first/networktables/ConnectionListenerTest.java +++ b/ntcore/src/test/java/edu/wpi/first/networktables/ConnectionListenerTest.java @@ -8,10 +8,12 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import edu.wpi.first.util.WPIUtilJNI; import java.util.ArrayList; +import java.util.EnumSet; import java.util.List; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -64,7 +66,7 @@ void testJNI() { assertNotSame(poller, 0, "bad poller handle"); int handle = NetworkTablesJNI.addListener( - poller, m_serverInst.getHandle(), NetworkTableEvent.kConnection); + poller, m_serverInst.getHandle(), EnumSet.of(NetworkTableEvent.Kind.kConnection)); assertNotSame(handle, 0, "bad listener handle"); // trigger a connect event @@ -82,7 +84,7 @@ void testJNI() { assertEquals(1, events.length); assertEquals(handle, events[0].listener); assertNotNull(events[0].connInfo); - assertEquals(events[0].flags, NetworkTableEvent.kConnected); + assertTrue(events[0].is(NetworkTableEvent.Kind.kConnected)); // trigger a disconnect event m_clientInst.stopClient(); @@ -103,7 +105,7 @@ void testJNI() { assertNotNull(events); assertEquals(1, events.length); assertEquals(handle, events[0].listener); - assertEquals(events[0].flags, NetworkTableEvent.kDisconnected); + assertTrue(events[0].is(NetworkTableEvent.Kind.kDisconnected)); } private static int threadedPort = 10001; @@ -155,7 +157,7 @@ void testThreaded(String address) { assertEquals(1, events.size()); assertEquals(handle, events.get(0).listener); assertNotNull(events.get(0).connInfo); - assertEquals(events.get(0).flags, NetworkTableEvent.kConnected); + assertTrue(events.get(0).is(NetworkTableEvent.Kind.kConnected)); events.clear(); } @@ -180,7 +182,7 @@ void testThreaded(String address) { assertEquals(1, events.size()); assertEquals(handle, events.get(0).listener); assertNotNull(events.get(0).connInfo); - assertEquals(events.get(0).flags, NetworkTableEvent.kDisconnected); + assertTrue(events.get(0).is(NetworkTableEvent.Kind.kDisconnected)); } } } diff --git a/ntcore/src/test/java/edu/wpi/first/networktables/LoggerTest.java b/ntcore/src/test/java/edu/wpi/first/networktables/LoggerTest.java index 17e8d43455f..c5622919940 100644 --- a/ntcore/src/test/java/edu/wpi/first/networktables/LoggerTest.java +++ b/ntcore/src/test/java/edu/wpi/first/networktables/LoggerTest.java @@ -37,7 +37,7 @@ void addMessageTest() { // wait for client to report it's started, then wait another 0.1 sec try { int count = 0; - while ((m_clientInst.getNetworkMode() & NetworkTableInstance.kNetModeClient4) == 0) { + while (!m_clientInst.getNetworkMode().contains(NetworkTableInstance.NetworkMode.kClient4)) { Thread.sleep(100); count++; if (count > 30) { diff --git a/ntcore/src/test/java/edu/wpi/first/networktables/TopicListenerTest.java b/ntcore/src/test/java/edu/wpi/first/networktables/TopicListenerTest.java index 130628d65a5..9f1d67f5745 100644 --- a/ntcore/src/test/java/edu/wpi/first/networktables/TopicListenerTest.java +++ b/ntcore/src/test/java/edu/wpi/first/networktables/TopicListenerTest.java @@ -6,9 +6,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import edu.wpi.first.util.WPIUtilJNI; +import java.util.EnumSet; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; @@ -37,7 +39,8 @@ private void connect() { // Use connection listener to ensure we've connected int poller = NetworkTablesJNI.createListenerPoller(m_clientInst.getHandle()); - NetworkTablesJNI.addListener(poller, m_clientInst.getHandle(), NetworkTableEvent.kConnected); + NetworkTablesJNI.addListener( + poller, m_clientInst.getHandle(), EnumSet.of(NetworkTableEvent.Kind.kConnected)); try { if (WPIUtilJNI.waitForObjectTimeout(poller, 1.0)) { fail("client didn't connect to server"); @@ -55,7 +58,8 @@ void testPrefixNewRemote() { connect(); final int poller = NetworkTablesJNI.createListenerPoller(m_serverInst.getHandle()); final int handle = - NetworkTablesJNI.addListener(poller, new String[] {"/foo"}, NetworkTableEvent.kPublish); + NetworkTablesJNI.addListener( + poller, new String[] {"/foo"}, EnumSet.of(NetworkTableEvent.Kind.kPublish)); // Trigger an event m_clientInst.getEntry("/foo/bar").setDouble(1.0); @@ -83,6 +87,6 @@ void testPrefixNewRemote() { assertNotNull(events[0].topicInfo); assertEquals(m_serverInst.getTopic("/foo/bar"), events[0].topicInfo.getTopic()); assertEquals("/foo/bar", events[0].topicInfo.name); - assertEquals(NetworkTableEvent.kPublish, events[0].flags); + assertTrue(events[0].is(NetworkTableEvent.Kind.kPublish)); } } diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Preferences.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Preferences.java index 8bae3ad4f93..7675e4d2fc9 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Preferences.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Preferences.java @@ -17,6 +17,7 @@ import edu.wpi.first.networktables.StringPublisher; import edu.wpi.first.networktables.Topic; import java.util.Collection; +import java.util.EnumSet; /** * The preferences class provides a relatively simple way to save important values to the roboRIO to @@ -77,7 +78,7 @@ public static synchronized void setNetworkTableInstance(NetworkTableInstance ins m_listener = NetworkTableListener.createListener( m_tableSubscriber, - NetworkTableEvent.kImmediate | NetworkTableEvent.kPublish, + EnumSet.of(NetworkTableEvent.Kind.kImmediate, NetworkTableEvent.Kind.kPublish), event -> { if (event.topicInfo != null) { Topic topic = event.topicInfo.getTopic(); diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/RobotBase.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/RobotBase.java index 7e27c1279aa..9f4aee2df08 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/RobotBase.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/RobotBase.java @@ -155,7 +155,7 @@ protected RobotBase() { // wait for the NT server to actually start try { int count = 0; - while ((inst.getNetworkMode() & NetworkTableInstance.kNetModeStarting) != 0) { + while (inst.getNetworkMode().contains(NetworkTableInstance.NetworkMode.kStarting)) { Thread.sleep(10); count++; if (count > 100) { diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/PreferencesTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/PreferencesTest.java index c53da487209..e3ccfcc72e4 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/PreferencesTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/PreferencesTest.java @@ -54,7 +54,7 @@ void setup(@TempDir Path tempDir) { m_inst.startServer(filepath.toString(), "", 0, 0); try { int count = 0; - while ((m_inst.getNetworkMode() & NetworkTableInstance.kNetModeStarting) != 0) { + while (m_inst.getNetworkMode().contains(NetworkTableInstance.NetworkMode.kStarting)) { Thread.sleep(100); count++; if (count > 30) {