Skip to content

Commit 6910ff1

Browse files
authored
[ntcore] PubSubOption: Use record approach for Java (#8659)
1 parent ccfb3ab commit 6910ff1

File tree

5 files changed

+117
-106
lines changed

5 files changed

+117
-106
lines changed

ntcore/src/main/java/org/wpilib/networktables/PubSubOption.java

Lines changed: 101 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -5,165 +5,183 @@
55
package org.wpilib.networktables;
66

77
/** NetworkTables publish/subscribe option. */
8-
public class PubSubOption {
9-
enum Kind {
10-
periodic,
11-
sendAll,
12-
topicsOnly,
13-
pollStorage,
14-
keepDuplicates,
15-
disableRemote,
16-
disableLocal,
17-
excludePublisher,
18-
excludeSelf,
19-
hidden
20-
}
21-
22-
PubSubOption(Kind kind, boolean value) {
23-
m_kind = kind;
24-
m_bValue = value;
25-
m_iValue = 0;
26-
m_dValue = 0;
27-
}
28-
29-
PubSubOption(Kind kind, int value) {
30-
m_kind = kind;
31-
m_bValue = false;
32-
m_iValue = value;
33-
m_dValue = 0;
34-
}
35-
36-
PubSubOption(Kind kind, double value) {
37-
m_kind = kind;
38-
m_bValue = false;
39-
m_iValue = 0;
40-
m_dValue = value;
41-
}
42-
8+
public sealed interface PubSubOption {
439
/**
4410
* How frequently changes will be sent over the network. NetworkTables may send more frequently
4511
* than this (e.g. use a combined minimum period for all values) or apply a restricted range to
4612
* this value. The default if unspecified is 100 ms.
4713
*
4814
* @param period time between updates, in seconds
49-
* @return option
5015
*/
51-
public static PubSubOption periodic(double period) {
52-
return new PubSubOption(Kind.periodic, period);
53-
}
16+
record Periodic(double period) implements PubSubOption {}
5417

5518
/**
5619
* If enabled, sends all value changes over the network. This option defaults to disabled.
5720
*
5821
* @param enabled True to enable, false to disable
59-
* @return option
6022
*/
61-
public static PubSubOption sendAll(boolean enabled) {
62-
return new PubSubOption(Kind.sendAll, enabled);
63-
}
23+
record SendAll(boolean enabled) implements PubSubOption {}
6424

6525
/**
6626
* If enabled on a subscription, does not request value changes. This option defaults to disabled.
6727
*
6828
* @param enabled True to enable, false to disable
69-
* @return option
7029
*/
71-
public static PubSubOption topicsOnly(boolean enabled) {
72-
return new PubSubOption(Kind.topicsOnly, enabled);
73-
}
30+
record TopicsOnly(boolean enabled) implements PubSubOption {}
7431

7532
/**
7633
* If enabled, preserves duplicate value changes (rather than ignoring them). This option defaults
7734
* to disabled.
7835
*
7936
* @param enabled True to enable, false to disable
80-
* @return option
8137
*/
82-
public static PubSubOption keepDuplicates(boolean enabled) {
83-
return new PubSubOption(Kind.keepDuplicates, enabled);
84-
}
38+
record KeepDuplicates(boolean enabled) implements PubSubOption {}
8539

8640
/**
8741
* Polling storage for subscription. Specifies the maximum number of updates NetworkTables should
8842
* store between calls to the subscriber's readQueue() function. Defaults to 1 if sendAll is
8943
* false, 20 if sendAll is true.
9044
*
9145
* @param depth number of entries to save for polling.
92-
* @return option
9346
*/
94-
public static PubSubOption pollStorage(int depth) {
95-
return new PubSubOption(Kind.pollStorage, depth);
96-
}
47+
record PollStorage(int depth) implements PubSubOption {}
9748

9849
/**
9950
* For subscriptions, specify whether remote value updates should not be queued for readQueue().
10051
* See also disableLocal(). Defaults to false (remote value updates are queued).
10152
*
10253
* @param disabled True to disable, false to enable
103-
* @return option
10454
*/
105-
public static PubSubOption disableRemote(boolean disabled) {
106-
return new PubSubOption(Kind.disableRemote, disabled);
107-
}
55+
record DisableRemote(boolean disabled) implements PubSubOption {}
10856

10957
/**
11058
* For subscriptions, specify whether local value updates should not be queued for readQueue().
11159
* See also disableRemote(). Defaults to false (local value updates are queued).
11260
*
11361
* @param disabled True to disable, false to enable
114-
* @return option
11562
*/
116-
public static PubSubOption disableLocal(boolean disabled) {
117-
return new PubSubOption(Kind.disableLocal, disabled);
118-
}
63+
record DisableLocal(boolean disabled) implements PubSubOption {}
11964

12065
/**
12166
* Don't queue value updates for the given publisher. Only has an effect on subscriptions. Only
12267
* one exclusion may be set.
12368
*
124-
* @param publisher publisher handle to exclude
125-
* @return option
69+
* @param publisher publisher to exclude
12670
*/
127-
public static PubSubOption excludePublisher(int publisher) {
128-
return new PubSubOption(Kind.excludePublisher, publisher);
129-
}
71+
record ExcludePublisher(Publisher publisher) implements PubSubOption {}
13072

13173
/**
13274
* Don't queue value updates for the given publisher. Only has an effect on subscriptions. Only
13375
* one exclusion may be set.
13476
*
135-
* @param publisher publisher to exclude
136-
* @return option
77+
* @param publisher publisher handle to exclude
13778
*/
138-
public static PubSubOption excludePublisher(Publisher publisher) {
139-
return excludePublisher(publisher != null ? publisher.getHandle() : 0);
140-
}
79+
record ExcludePublisherHandle(int publisher) implements PubSubOption {}
14180

14281
/**
14382
* Don't queue value updates for the internal publisher for an entry. Only has an effect on
14483
* entries.
14584
*
14685
* @param enabled True to enable, false to disable
147-
* @return option
14886
*/
149-
public static PubSubOption excludeSelf(boolean enabled) {
150-
return new PubSubOption(Kind.excludeSelf, enabled);
151-
}
87+
record ExcludeSelf(boolean enabled) implements PubSubOption {}
15288

15389
/**
15490
* For subscriptions, don't share the existence of the subscription with the network. Note this
15591
* means updates will not be received from the network unless another subscription overlaps with
15692
* this one, and the subscription will not appear in metatopics.
15793
*
15894
* @param enabled True to enable, false to disable
95+
*/
96+
record Hidden(boolean enabled) implements PubSubOption {}
97+
98+
/** Indicates only value changes will be sent over the network (default). */
99+
PubSubOption SEND_CHANGES = new SendAll(false);
100+
101+
/** Indicates all value changes will be sent over the network. */
102+
PubSubOption SEND_ALL = new SendAll(true);
103+
104+
/** Indicates both topics and values will be sent over the network (default). */
105+
PubSubOption TOPICS_AND_VALUES = new TopicsOnly(false);
106+
107+
/** Indicates only topics will be sent over the network. */
108+
PubSubOption TOPICS_ONLY = new TopicsOnly(true);
109+
110+
/** Indicates duplicate value changes will be ignored (default). */
111+
PubSubOption IGNORE_DUPLICATES = new KeepDuplicates(false);
112+
113+
/** Indicates duplicate value changes will be preserved. */
114+
PubSubOption KEEP_DUPLICATES = new KeepDuplicates(true);
115+
116+
/** For subscriptions, indicates remote value updates will be queued for readQueue() (default). */
117+
PubSubOption ENABLE_REMOTE = new DisableRemote(false);
118+
119+
/** For subscriptions, indicates remote value updates will not be queued for readQueue() . */
120+
PubSubOption DISABLE_REMOTE = new DisableRemote(true);
121+
122+
/** For subscriptions, indicates local value updates will be queued for readQueue() (default). */
123+
PubSubOption ENABLE_LOCAL = new DisableLocal(false);
124+
125+
/** For subscriptions, indicates local value updates will not be queued for readQueue() . */
126+
PubSubOption DISABLE_LOCAL = new DisableLocal(true);
127+
128+
/**
129+
* For entries, indicates value updates from the internal publisher will be included (default).
130+
*/
131+
PubSubOption INCLUDE_SELF = new ExcludeSelf(false);
132+
133+
/** For entries, indicates value updates from the internal publisher will be excluded. */
134+
PubSubOption EXCLUDE_SELF = new ExcludeSelf(true);
135+
136+
/** For subscriptions, indicates the subscription is visible to the network (default). */
137+
PubSubOption VISIBLE = new Hidden(false);
138+
139+
/** For subscriptions, indicates the subscription is hidden from the network. */
140+
PubSubOption HIDDEN = new Hidden(true);
141+
142+
/**
143+
* How frequently changes will be sent over the network. NetworkTables may send more frequently
144+
* than this (e.g. use a combined minimum period for all values) or apply a restricted range to
145+
* this value. The default if unspecified is 100 ms.
146+
*
147+
* @param period time between updates, in seconds
159148
* @return option
160149
*/
161-
public static PubSubOption hidden(boolean enabled) {
162-
return new PubSubOption(Kind.hidden, enabled);
150+
static PubSubOption periodic(double period) {
151+
return new Periodic(period);
163152
}
164153

165-
final Kind m_kind;
166-
final boolean m_bValue;
167-
final int m_iValue;
168-
final double m_dValue;
154+
/**
155+
* Polling storage for subscription. Specifies the maximum number of updates NetworkTables should
156+
* store between calls to the subscriber's readQueue() function. Defaults to 1 if sendAll is
157+
* false, 20 if sendAll is true.
158+
*
159+
* @param depth number of entries to save for polling.
160+
* @return option
161+
*/
162+
static PubSubOption pollStorage(int depth) {
163+
return new PollStorage(depth);
164+
}
165+
166+
/**
167+
* Don't queue value updates for the given publisher. Only has an effect on subscriptions. Only
168+
* one exclusion may be set.
169+
*
170+
* @param publisher publisher handle to exclude
171+
* @return option
172+
*/
173+
static PubSubOption excludePublisher(int publisher) {
174+
return new ExcludePublisherHandle(publisher);
175+
}
176+
177+
/**
178+
* Don't queue value updates for the given publisher. Only has an effect on subscriptions. Only
179+
* one exclusion may be set.
180+
*
181+
* @param publisher publisher to exclude
182+
* @return option
183+
*/
184+
static PubSubOption excludePublisher(Publisher publisher) {
185+
return new ExcludePublisher(publisher);
186+
}
169187
}

ntcore/src/main/java/org/wpilib/networktables/PubSubOptions.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@ public class PubSubOptions {
1414
*/
1515
public PubSubOptions(PubSubOption... options) {
1616
for (PubSubOption option : options) {
17-
switch (option.m_kind) {
18-
case periodic -> periodic = option.m_dValue;
19-
case sendAll -> sendAll = option.m_bValue;
20-
case topicsOnly -> topicsOnly = option.m_bValue;
21-
case pollStorage -> pollStorage = option.m_iValue;
22-
case keepDuplicates -> keepDuplicates = option.m_bValue;
23-
case disableRemote -> disableRemote = option.m_bValue;
24-
case disableLocal -> disableLocal = option.m_bValue;
25-
case excludePublisher -> excludePublisher = option.m_iValue;
26-
case excludeSelf -> excludeSelf = option.m_bValue;
27-
case hidden -> hidden = option.m_bValue;
28-
default -> {
29-
// NOP
30-
}
17+
switch (option) {
18+
case PubSubOption.Periodic p -> periodic = p.period();
19+
case PubSubOption.SendAll s -> sendAll = s.enabled();
20+
case PubSubOption.TopicsOnly t -> topicsOnly = t.enabled();
21+
case PubSubOption.PollStorage p -> pollStorage = p.depth();
22+
case PubSubOption.KeepDuplicates k -> keepDuplicates = k.enabled();
23+
case PubSubOption.DisableRemote d -> disableRemote = d.disabled();
24+
case PubSubOption.DisableLocal l -> disableLocal = l.disabled();
25+
case PubSubOption.ExcludePublisher e ->
26+
excludePublisher = e.publisher() == null ? 0 : e.publisher().getHandle();
27+
case PubSubOption.ExcludePublisherHandle e -> excludePublisher = e.publisher();
28+
case PubSubOption.ExcludeSelf s -> excludeSelf = s.enabled();
29+
case PubSubOption.Hidden h -> hidden = h.enabled();
3130
}
3231
}
3332
}

wpilibj/src/main/java/org/wpilib/hardware/expansionhub/ExpansionHubMotor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ public ExpansionHubMotor(int usbId, int channel) {
7474

7575
PubSubOption[] options =
7676
new PubSubOption[] {
77-
PubSubOption.sendAll(true),
78-
PubSubOption.keepDuplicates(true),
79-
PubSubOption.periodic(0.005)
77+
PubSubOption.SEND_ALL, PubSubOption.KEEP_DUPLICATES, PubSubOption.periodic(0.005)
8078
};
8179

8280
m_encoderSubscriber =

wpilibj/src/main/java/org/wpilib/hardware/expansionhub/ExpansionHubPidConstants.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ public class ExpansionHubPidConstants {
2828

2929
PubSubOption[] options =
3030
new PubSubOption[] {
31-
PubSubOption.sendAll(true),
32-
PubSubOption.keepDuplicates(true),
33-
PubSubOption.periodic(0.005)
31+
PubSubOption.SEND_ALL, PubSubOption.KEEP_DUPLICATES, PubSubOption.periodic(0.005)
3432
};
3533

3634
String pidType = isVelocityPid ? "velocity" : "position";

wpilibj/src/main/java/org/wpilib/hardware/expansionhub/ExpansionHubServo.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ public ExpansionHubServo(int usbId, int channel) {
6060

6161
PubSubOption[] options =
6262
new PubSubOption[] {
63-
PubSubOption.sendAll(true),
64-
PubSubOption.keepDuplicates(true),
65-
PubSubOption.periodic(0.005)
63+
PubSubOption.SEND_ALL, PubSubOption.KEEP_DUPLICATES, PubSubOption.periodic(0.005)
6664
};
6765

6866
m_pulseWidthPublisher =

0 commit comments

Comments
 (0)