diff --git a/ntcore/src/main/java/org/wpilib/networktables/PubSubOption.java b/ntcore/src/main/java/org/wpilib/networktables/PubSubOption.java index 606e6bb9db..2cbee836ce 100644 --- a/ntcore/src/main/java/org/wpilib/networktables/PubSubOption.java +++ b/ntcore/src/main/java/org/wpilib/networktables/PubSubOption.java @@ -5,40 +5,139 @@ package org.wpilib.networktables; /** NetworkTables publish/subscribe option. */ -public class PubSubOption { - enum Kind { - periodic, - sendAll, - topicsOnly, - pollStorage, - keepDuplicates, - disableRemote, - disableLocal, - excludePublisher, - excludeSelf, - hidden - } +public sealed interface PubSubOption { + /** + * How frequently changes will be sent over the network. NetworkTables may send more frequently + * than this (e.g. use a combined minimum period for all values) or apply a restricted range to + * this value. The default if unspecified is 100 ms. + * + * @param period time between updates, in seconds + */ + record Periodic(double period) implements PubSubOption {} - PubSubOption(Kind kind, boolean value) { - m_kind = kind; - m_bValue = value; - m_iValue = 0; - m_dValue = 0; - } + /** + * If enabled, sends all value changes over the network. This option defaults to disabled. + * + * @param enabled True to enable, false to disable + */ + record SendAll(boolean enabled) implements PubSubOption {} - PubSubOption(Kind kind, int value) { - m_kind = kind; - m_bValue = false; - m_iValue = value; - m_dValue = 0; - } + /** + * If enabled on a subscription, does not request value changes. This option defaults to disabled. + * + * @param enabled True to enable, false to disable + */ + record TopicsOnly(boolean enabled) implements PubSubOption {} - PubSubOption(Kind kind, double value) { - m_kind = kind; - m_bValue = false; - m_iValue = 0; - m_dValue = value; - } + /** + * If enabled, preserves duplicate value changes (rather than ignoring them). This option defaults + * to disabled. + * + * @param enabled True to enable, false to disable + */ + record KeepDuplicates(boolean enabled) implements PubSubOption {} + + /** + * Polling storage for subscription. Specifies the maximum number of updates NetworkTables should + * store between calls to the subscriber's readQueue() function. Defaults to 1 if sendAll is + * false, 20 if sendAll is true. + * + * @param depth number of entries to save for polling. + */ + record PollStorage(int depth) implements PubSubOption {} + + /** + * For subscriptions, specify whether remote value updates should not be queued for readQueue(). + * See also disableLocal(). Defaults to false (remote value updates are queued). + * + * @param disabled True to disable, false to enable + */ + record DisableRemote(boolean disabled) implements PubSubOption {} + + /** + * For subscriptions, specify whether local value updates should not be queued for readQueue(). + * See also disableRemote(). Defaults to false (local value updates are queued). + * + * @param disabled True to disable, false to enable + */ + record DisableLocal(boolean disabled) implements PubSubOption {} + + /** + * Don't queue value updates for the given publisher. Only has an effect on subscriptions. Only + * one exclusion may be set. + * + * @param publisher publisher to exclude + */ + record ExcludePublisher(Publisher publisher) implements PubSubOption {} + + /** + * Don't queue value updates for the given publisher. Only has an effect on subscriptions. Only + * one exclusion may be set. + * + * @param publisher publisher handle to exclude + */ + record ExcludePublisherHandle(int publisher) implements PubSubOption {} + + /** + * Don't queue value updates for the internal publisher for an entry. Only has an effect on + * entries. + * + * @param enabled True to enable, false to disable + */ + record ExcludeSelf(boolean enabled) implements PubSubOption {} + + /** + * For subscriptions, don't share the existence of the subscription with the network. Note this + * means updates will not be received from the network unless another subscription overlaps with + * this one, and the subscription will not appear in metatopics. + * + * @param enabled True to enable, false to disable + */ + record Hidden(boolean enabled) implements PubSubOption {} + + /** Indicates only value changes will be sent over the network (default). */ + PubSubOption SEND_CHANGES = new SendAll(false); + + /** Indicates all value changes will be sent over the network. */ + PubSubOption SEND_ALL = new SendAll(true); + + /** Indicates both topics and values will be sent over the network (default). */ + PubSubOption TOPICS_AND_VALUES = new TopicsOnly(false); + + /** Indicates only topics will be sent over the network. */ + PubSubOption TOPICS_ONLY = new TopicsOnly(true); + + /** Indicates duplicate value changes will be ignored (default). */ + PubSubOption IGNORE_DUPLICATES = new KeepDuplicates(false); + + /** Indicates duplicate value changes will be preserved. */ + PubSubOption KEEP_DUPLICATES = new KeepDuplicates(true); + + /** For subscriptions, indicates remote value updates will be queued for readQueue() (default). */ + PubSubOption ENABLE_REMOTE = new DisableRemote(false); + + /** For subscriptions, indicates remote value updates will not be queued for readQueue() . */ + PubSubOption DISABLE_REMOTE = new DisableRemote(true); + + /** For subscriptions, indicates local value updates will be queued for readQueue() (default). */ + PubSubOption ENABLE_LOCAL = new DisableLocal(false); + + /** For subscriptions, indicates local value updates will not be queued for readQueue() . */ + PubSubOption DISABLE_LOCAL = new DisableLocal(true); + + /** + * For entries, indicates value updates from the internal publisher will be included (default). + */ + PubSubOption INCLUDE_SELF = new ExcludeSelf(false); + + /** For entries, indicates value updates from the internal publisher will be excluded. */ + PubSubOption EXCLUDE_SELF = new ExcludeSelf(true); + + /** For subscriptions, indicates the subscription is visible to the network (default). */ + PubSubOption VISIBLE = new Hidden(false); + + /** For subscriptions, indicates the subscription is hidden from the network. */ + PubSubOption HIDDEN = new Hidden(true); /** * How frequently changes will be sent over the network. NetworkTables may send more frequently @@ -48,39 +147,8 @@ public class PubSubOption { * @param period time between updates, in seconds * @return option */ - public static PubSubOption periodic(double period) { - return new PubSubOption(Kind.periodic, period); - } - - /** - * If enabled, sends all value changes over the network. This option defaults to disabled. - * - * @param enabled True to enable, false to disable - * @return option - */ - public static PubSubOption sendAll(boolean enabled) { - return new PubSubOption(Kind.sendAll, enabled); - } - - /** - * If enabled on a subscription, does not request value changes. This option defaults to disabled. - * - * @param enabled True to enable, false to disable - * @return option - */ - public static PubSubOption topicsOnly(boolean enabled) { - return new PubSubOption(Kind.topicsOnly, enabled); - } - - /** - * If enabled, preserves duplicate value changes (rather than ignoring them). This option defaults - * to disabled. - * - * @param enabled True to enable, false to disable - * @return option - */ - public static PubSubOption keepDuplicates(boolean enabled) { - return new PubSubOption(Kind.keepDuplicates, enabled); + static PubSubOption periodic(double period) { + return new Periodic(period); } /** @@ -91,30 +159,8 @@ public class PubSubOption { * @param depth number of entries to save for polling. * @return option */ - public static PubSubOption pollStorage(int depth) { - return new PubSubOption(Kind.pollStorage, depth); - } - - /** - * For subscriptions, specify whether remote value updates should not be queued for readQueue(). - * See also disableLocal(). Defaults to false (remote value updates are queued). - * - * @param disabled True to disable, false to enable - * @return option - */ - public static PubSubOption disableRemote(boolean disabled) { - return new PubSubOption(Kind.disableRemote, disabled); - } - - /** - * For subscriptions, specify whether local value updates should not be queued for readQueue(). - * See also disableRemote(). Defaults to false (local value updates are queued). - * - * @param disabled True to disable, false to enable - * @return option - */ - public static PubSubOption disableLocal(boolean disabled) { - return new PubSubOption(Kind.disableLocal, disabled); + static PubSubOption pollStorage(int depth) { + return new PollStorage(depth); } /** @@ -124,8 +170,8 @@ public class PubSubOption { * @param publisher publisher handle to exclude * @return option */ - public static PubSubOption excludePublisher(int publisher) { - return new PubSubOption(Kind.excludePublisher, publisher); + static PubSubOption excludePublisher(int publisher) { + return new ExcludePublisherHandle(publisher); } /** @@ -135,35 +181,7 @@ public class PubSubOption { * @param publisher publisher to exclude * @return option */ - public static PubSubOption excludePublisher(Publisher publisher) { - return excludePublisher(publisher != null ? publisher.getHandle() : 0); + static PubSubOption excludePublisher(Publisher publisher) { + return new ExcludePublisher(publisher); } - - /** - * Don't queue value updates for the internal publisher for an entry. Only has an effect on - * entries. - * - * @param enabled True to enable, false to disable - * @return option - */ - public static PubSubOption excludeSelf(boolean enabled) { - return new PubSubOption(Kind.excludeSelf, enabled); - } - - /** - * For subscriptions, don't share the existence of the subscription with the network. Note this - * means updates will not be received from the network unless another subscription overlaps with - * this one, and the subscription will not appear in metatopics. - * - * @param enabled True to enable, false to disable - * @return option - */ - public static PubSubOption hidden(boolean enabled) { - return new PubSubOption(Kind.hidden, enabled); - } - - final Kind m_kind; - final boolean m_bValue; - final int m_iValue; - final double m_dValue; } diff --git a/ntcore/src/main/java/org/wpilib/networktables/PubSubOptions.java b/ntcore/src/main/java/org/wpilib/networktables/PubSubOptions.java index b0bbdbbae0..3aec699bc8 100644 --- a/ntcore/src/main/java/org/wpilib/networktables/PubSubOptions.java +++ b/ntcore/src/main/java/org/wpilib/networktables/PubSubOptions.java @@ -14,20 +14,19 @@ public class PubSubOptions { */ public PubSubOptions(PubSubOption... options) { for (PubSubOption option : options) { - switch (option.m_kind) { - case periodic -> periodic = option.m_dValue; - case sendAll -> sendAll = option.m_bValue; - case topicsOnly -> topicsOnly = option.m_bValue; - case pollStorage -> pollStorage = option.m_iValue; - case keepDuplicates -> keepDuplicates = option.m_bValue; - case disableRemote -> disableRemote = option.m_bValue; - case disableLocal -> disableLocal = option.m_bValue; - case excludePublisher -> excludePublisher = option.m_iValue; - case excludeSelf -> excludeSelf = option.m_bValue; - case hidden -> hidden = option.m_bValue; - default -> { - // NOP - } + switch (option) { + case PubSubOption.Periodic p -> periodic = p.period(); + case PubSubOption.SendAll s -> sendAll = s.enabled(); + case PubSubOption.TopicsOnly t -> topicsOnly = t.enabled(); + case PubSubOption.PollStorage p -> pollStorage = p.depth(); + case PubSubOption.KeepDuplicates k -> keepDuplicates = k.enabled(); + case PubSubOption.DisableRemote d -> disableRemote = d.disabled(); + case PubSubOption.DisableLocal l -> disableLocal = l.disabled(); + case PubSubOption.ExcludePublisher e -> + excludePublisher = e.publisher() == null ? 0 : e.publisher().getHandle(); + case PubSubOption.ExcludePublisherHandle e -> excludePublisher = e.publisher(); + case PubSubOption.ExcludeSelf s -> excludeSelf = s.enabled(); + case PubSubOption.Hidden h -> hidden = h.enabled(); } } } diff --git a/wpilibj/src/main/java/org/wpilib/hardware/expansionhub/ExpansionHubMotor.java b/wpilibj/src/main/java/org/wpilib/hardware/expansionhub/ExpansionHubMotor.java index 33f210db3c..50f6662e66 100644 --- a/wpilibj/src/main/java/org/wpilib/hardware/expansionhub/ExpansionHubMotor.java +++ b/wpilibj/src/main/java/org/wpilib/hardware/expansionhub/ExpansionHubMotor.java @@ -74,9 +74,7 @@ public class ExpansionHubMotor implements AutoCloseable { PubSubOption[] options = new PubSubOption[] { - PubSubOption.sendAll(true), - PubSubOption.keepDuplicates(true), - PubSubOption.periodic(0.005) + PubSubOption.SEND_ALL, PubSubOption.KEEP_DUPLICATES, PubSubOption.periodic(0.005) }; m_encoderSubscriber = diff --git a/wpilibj/src/main/java/org/wpilib/hardware/expansionhub/ExpansionHubPidConstants.java b/wpilibj/src/main/java/org/wpilib/hardware/expansionhub/ExpansionHubPidConstants.java index 2fd6e1dde6..20a504fc13 100644 --- a/wpilibj/src/main/java/org/wpilib/hardware/expansionhub/ExpansionHubPidConstants.java +++ b/wpilibj/src/main/java/org/wpilib/hardware/expansionhub/ExpansionHubPidConstants.java @@ -28,9 +28,7 @@ public class ExpansionHubPidConstants { PubSubOption[] options = new PubSubOption[] { - PubSubOption.sendAll(true), - PubSubOption.keepDuplicates(true), - PubSubOption.periodic(0.005) + PubSubOption.SEND_ALL, PubSubOption.KEEP_DUPLICATES, PubSubOption.periodic(0.005) }; String pidType = isVelocityPid ? "velocity" : "position"; diff --git a/wpilibj/src/main/java/org/wpilib/hardware/expansionhub/ExpansionHubServo.java b/wpilibj/src/main/java/org/wpilib/hardware/expansionhub/ExpansionHubServo.java index 63bef9b981..0d53e5c754 100644 --- a/wpilibj/src/main/java/org/wpilib/hardware/expansionhub/ExpansionHubServo.java +++ b/wpilibj/src/main/java/org/wpilib/hardware/expansionhub/ExpansionHubServo.java @@ -60,9 +60,7 @@ public class ExpansionHubServo implements AutoCloseable { PubSubOption[] options = new PubSubOption[] { - PubSubOption.sendAll(true), - PubSubOption.keepDuplicates(true), - PubSubOption.periodic(0.005) + PubSubOption.SEND_ALL, PubSubOption.KEEP_DUPLICATES, PubSubOption.periodic(0.005) }; m_pulseWidthPublisher =