5package org.wpilib.networktables;
7import java.util.ArrayList;
8import java.util.EnumSet;
9import java.util.HashSet;
11import java.util.Objects;
13import java.util.concurrent.ConcurrentHashMap;
14import java.util.concurrent.ConcurrentMap;
15import java.util.function.Consumer;
16import org.wpilib.util.protobuf.Protobuf;
17import org.wpilib.util.struct.Struct;
18import us.hebi.quickbuf.ProtoMessage;
21public final class NetworkTable {
25 private final String m_path;
26 private final String m_pathWithSep;
41 return key.substring(slash + 1);
59 public static String
normalizeKey(String key,
boolean withLeadingSlash) {
61 if (withLeadingSlash) {
70 normalized = normalized.substring(1);
98 List<String> hierarchy =
new ArrayList<>();
99 if (normal.length() == 1) {
100 hierarchy.add(normal);
106 hierarchy.add(normal);
109 hierarchy.add(normal.substring(0, i));
132 public String toString() {
133 return "NetworkTable: " + m_path;
143 return m_inst.getTopic(m_pathWithSep + name);
153 return m_inst.getBooleanTopic(m_pathWithSep + name);
163 return m_inst.getIntegerTopic(m_pathWithSep + name);
173 return m_inst.getFloatTopic(m_pathWithSep + name);
183 return m_inst.getDoubleTopic(m_pathWithSep + name);
193 return m_inst.getStringTopic(m_pathWithSep + name);
203 return m_inst.getRawTopic(m_pathWithSep + name);
213 return m_inst.getBooleanArrayTopic(m_pathWithSep + name);
223 return m_inst.getIntegerArrayTopic(m_pathWithSep + name);
233 return m_inst.getFloatArrayTopic(m_pathWithSep + name);
243 return m_inst.getDoubleArrayTopic(m_pathWithSep + name);
253 return m_inst.getStringArrayTopic(m_pathWithSep + name);
265 public <T, MessageType extends ProtoMessage<?>>
ProtobufTopic<T> getProtobufTopic(
267 return m_inst.getProtobufTopic(m_pathWithSep + name, proto);
278 public <T> StructTopic<T> getStructTopic(String name, Struct<T> struct) {
279 return m_inst.getStructTopic(m_pathWithSep + name, struct);
290 public <T> StructArrayTopic<T> getStructArrayTopic(String name, Struct<T> struct) {
291 return m_inst.getStructArrayTopic(m_pathWithSep + name, struct);
294 private final ConcurrentMap<String, NetworkTableEntry> m_entries =
new ConcurrentHashMap<>();
305 entry = m_inst.getEntry(m_pathWithSep + key);
307 if (oldEntry !=
null) {
322 return new NetworkTable(m_inst, m_pathWithSep + key);
344 return topics.length != 0;
354 List<TopicInfo> infos =
new ArrayList<>();
355 int prefixLen = m_path.length() + 1;
356 for (
TopicInfo info : m_inst.getTopicInfo(m_pathWithSep, types)) {
357 String relativeKey = info.name.substring(prefixLen);
382 List<Topic> topics =
new ArrayList<>();
383 int prefixLen = m_path.length() + 1;
384 for (
TopicInfo info : m_inst.getTopicInfo(m_pathWithSep, types)) {
385 String relativeKey = info.name.substring(prefixLen);
389 topics.add(info.getTopic());
410 Set<String> keys =
new HashSet<>();
411 int prefixLen = m_path.length() + 1;
412 for (
TopicInfo info : m_inst.getTopicInfo(m_pathWithSep, types)) {
413 String relativeKey = info.name.substring(prefixLen);
417 keys.add(relativeKey);
437 Set<String> keys =
new HashSet<>();
438 int prefixLen = m_path.length() + 1;
439 for (
TopicInfo info : m_inst.getTopicInfo(m_pathWithSep, 0)) {
440 String relativeKey = info.name.substring(prefixLen);
442 if (endSubTable == -1) {
445 keys.add(relativeKey.substring(0, endSubTable));
512 final int prefixLen = m_path.length() + 1;
513 return m_inst.addListener(
514 new String[] {m_pathWithSep},
517 String topicName =
null;
518 if (event.topicInfo !=
null) {
519 topicName =
event.topicInfo.name;
520 }
else if (event.valueData !=
null) {
521 topicName =
event.valueData.getTopic().getName();
523 if (topicName ==
null) {
526 String relativeKey = topicName.substring(prefixLen);
531 listener.
accept(
this, relativeKey, event);
544 String key, EnumSet<NetworkTableEvent.Kind> eventKinds,
TableEventListener listener) {
546 return m_inst.addListener(entry, eventKinds, event -> listener.
accept(
this, key, event));
559 void tableCreated(NetworkTable parent, String name, NetworkTable table);
570 final int prefixLen = m_path.length() + 1;
571 final NetworkTable parent =
this;
573 return m_inst.addListener(
574 new String[] {m_pathWithSep},
577 final Set<String> m_notifiedTables = new HashSet<>();
580 public void accept(NetworkTableEvent event) {
581 if (event.topicInfo == null) {
584 String relativeKey = event.topicInfo.name.substring(prefixLen);
585 int endSubTable = relativeKey.indexOf(PATH_SEPARATOR);
586 if (endSubTable == -1) {
589 String subTableKey = relativeKey.substring(0, endSubTable);
590 if (m_notifiedTables.contains(subTableKey)) {
593 m_notifiedTables.add(subTableKey);
594 listener.tableCreated(parent, subTableKey, parent.getSubTable(subTableKey));
605 m_inst.removeListener(listener);
609 public boolean equals(Object other) {
611 && m_inst.equals(ntOther.m_inst)
612 && m_path.equals(ntOther.m_path);
616 public int hashCode() {
617 return Objects.hash(m_inst, m_path);
boolean setValue(Object value)
NetworkTableValue getValue()
boolean setDefaultValue(Object defaultValue)
static final char PATH_SEPARATOR
int addSubTableListener(SubTableListener listener)
boolean containsKey(String key)
NetworkTableValue getValue(String key)
Set< String > getKeys(int types)
BooleanTopic getBooleanTopic(String name)
List< Topic > getTopics(int types)
NetworkTable getSubTable(String key)
FloatTopic getFloatTopic(String name)
static List< String > getHierarchy(String key)
StringArrayTopic getStringArrayTopic(String name)
DoubleArrayTopic getDoubleArrayTopic(String name)
IntegerTopic getIntegerTopic(String name)
IntegerArrayTopic getIntegerArrayTopic(String name)
DoubleTopic getDoubleTopic(String name)
boolean containsSubTable(String key)
BooleanArrayTopic getBooleanArrayTopic(String name)
void removeListener(int listener)
boolean putValue(String key, NetworkTableValue value)
List< TopicInfo > getTopicInfo()
List< TopicInfo > getTopicInfo(int types)
NetworkTableInstance getInstance()
int addListener(EnumSet< NetworkTableEvent.Kind > eventKinds, TableEventListener listener)
FloatArrayTopic getFloatArrayTopic(String name)
static String normalizeKey(String key, boolean withLeadingSlash)
StringTopic getStringTopic(String name)
int addListener(String key, EnumSet< NetworkTableEvent.Kind > eventKinds, TableEventListener listener)
NetworkTableEntry getEntry(String key)
Set< String > getSubTables()
RawTopic getRawTopic(String name)
static String basenameKey(String key)
List< Topic > getTopics()
Topic getTopic(String name)
boolean setDefaultValue(String key, NetworkTableValue defaultValue)
static String normalizeKey(String key)
void tableCreated(NetworkTable parent, String name, NetworkTable table)
void accept(NetworkTable table, String key, NetworkTableEvent event)