|
5 | 5 | package org.wpilib.networktables; |
6 | 6 |
|
7 | 7 | /** 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 { |
43 | 9 | /** |
44 | 10 | * How frequently changes will be sent over the network. NetworkTables may send more frequently |
45 | 11 | * than this (e.g. use a combined minimum period for all values) or apply a restricted range to |
46 | 12 | * this value. The default if unspecified is 100 ms. |
47 | 13 | * |
48 | 14 | * @param period time between updates, in seconds |
49 | | - * @return option |
50 | 15 | */ |
51 | | - public static PubSubOption periodic(double period) { |
52 | | - return new PubSubOption(Kind.periodic, period); |
53 | | - } |
| 16 | + record Periodic(double period) implements PubSubOption {} |
54 | 17 |
|
55 | 18 | /** |
56 | 19 | * If enabled, sends all value changes over the network. This option defaults to disabled. |
57 | 20 | * |
58 | 21 | * @param enabled True to enable, false to disable |
59 | | - * @return option |
60 | 22 | */ |
61 | | - public static PubSubOption sendAll(boolean enabled) { |
62 | | - return new PubSubOption(Kind.sendAll, enabled); |
63 | | - } |
| 23 | + record SendAll(boolean enabled) implements PubSubOption {} |
64 | 24 |
|
65 | 25 | /** |
66 | 26 | * If enabled on a subscription, does not request value changes. This option defaults to disabled. |
67 | 27 | * |
68 | 28 | * @param enabled True to enable, false to disable |
69 | | - * @return option |
70 | 29 | */ |
71 | | - public static PubSubOption topicsOnly(boolean enabled) { |
72 | | - return new PubSubOption(Kind.topicsOnly, enabled); |
73 | | - } |
| 30 | + record TopicsOnly(boolean enabled) implements PubSubOption {} |
74 | 31 |
|
75 | 32 | /** |
76 | 33 | * If enabled, preserves duplicate value changes (rather than ignoring them). This option defaults |
77 | 34 | * to disabled. |
78 | 35 | * |
79 | 36 | * @param enabled True to enable, false to disable |
80 | | - * @return option |
81 | 37 | */ |
82 | | - public static PubSubOption keepDuplicates(boolean enabled) { |
83 | | - return new PubSubOption(Kind.keepDuplicates, enabled); |
84 | | - } |
| 38 | + record KeepDuplicates(boolean enabled) implements PubSubOption {} |
85 | 39 |
|
86 | 40 | /** |
87 | 41 | * Polling storage for subscription. Specifies the maximum number of updates NetworkTables should |
88 | 42 | * store between calls to the subscriber's readQueue() function. Defaults to 1 if sendAll is |
89 | 43 | * false, 20 if sendAll is true. |
90 | 44 | * |
91 | 45 | * @param depth number of entries to save for polling. |
92 | | - * @return option |
93 | 46 | */ |
94 | | - public static PubSubOption pollStorage(int depth) { |
95 | | - return new PubSubOption(Kind.pollStorage, depth); |
96 | | - } |
| 47 | + record PollStorage(int depth) implements PubSubOption {} |
97 | 48 |
|
98 | 49 | /** |
99 | 50 | * For subscriptions, specify whether remote value updates should not be queued for readQueue(). |
100 | 51 | * See also disableLocal(). Defaults to false (remote value updates are queued). |
101 | 52 | * |
102 | 53 | * @param disabled True to disable, false to enable |
103 | | - * @return option |
104 | 54 | */ |
105 | | - public static PubSubOption disableRemote(boolean disabled) { |
106 | | - return new PubSubOption(Kind.disableRemote, disabled); |
107 | | - } |
| 55 | + record DisableRemote(boolean disabled) implements PubSubOption {} |
108 | 56 |
|
109 | 57 | /** |
110 | 58 | * For subscriptions, specify whether local value updates should not be queued for readQueue(). |
111 | 59 | * See also disableRemote(). Defaults to false (local value updates are queued). |
112 | 60 | * |
113 | 61 | * @param disabled True to disable, false to enable |
114 | | - * @return option |
115 | 62 | */ |
116 | | - public static PubSubOption disableLocal(boolean disabled) { |
117 | | - return new PubSubOption(Kind.disableLocal, disabled); |
118 | | - } |
| 63 | + record DisableLocal(boolean disabled) implements PubSubOption {} |
119 | 64 |
|
120 | 65 | /** |
121 | 66 | * Don't queue value updates for the given publisher. Only has an effect on subscriptions. Only |
122 | 67 | * one exclusion may be set. |
123 | 68 | * |
124 | | - * @param publisher publisher handle to exclude |
125 | | - * @return option |
| 69 | + * @param publisher publisher to exclude |
126 | 70 | */ |
127 | | - public static PubSubOption excludePublisher(int publisher) { |
128 | | - return new PubSubOption(Kind.excludePublisher, publisher); |
129 | | - } |
| 71 | + record ExcludePublisher(Publisher publisher) implements PubSubOption {} |
130 | 72 |
|
131 | 73 | /** |
132 | 74 | * Don't queue value updates for the given publisher. Only has an effect on subscriptions. Only |
133 | 75 | * one exclusion may be set. |
134 | 76 | * |
135 | | - * @param publisher publisher to exclude |
136 | | - * @return option |
| 77 | + * @param publisher publisher handle to exclude |
137 | 78 | */ |
138 | | - public static PubSubOption excludePublisher(Publisher publisher) { |
139 | | - return excludePublisher(publisher != null ? publisher.getHandle() : 0); |
140 | | - } |
| 79 | + record ExcludePublisherHandle(int publisher) implements PubSubOption {} |
141 | 80 |
|
142 | 81 | /** |
143 | 82 | * Don't queue value updates for the internal publisher for an entry. Only has an effect on |
144 | 83 | * entries. |
145 | 84 | * |
146 | 85 | * @param enabled True to enable, false to disable |
147 | | - * @return option |
148 | 86 | */ |
149 | | - public static PubSubOption excludeSelf(boolean enabled) { |
150 | | - return new PubSubOption(Kind.excludeSelf, enabled); |
151 | | - } |
| 87 | + record ExcludeSelf(boolean enabled) implements PubSubOption {} |
152 | 88 |
|
153 | 89 | /** |
154 | 90 | * For subscriptions, don't share the existence of the subscription with the network. Note this |
155 | 91 | * means updates will not be received from the network unless another subscription overlaps with |
156 | 92 | * this one, and the subscription will not appear in metatopics. |
157 | 93 | * |
158 | 94 | * @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 |
159 | 148 | * @return option |
160 | 149 | */ |
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); |
163 | 152 | } |
164 | 153 |
|
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 | + } |
169 | 187 | } |
0 commit comments