001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package edu.wpi.first.networktables; 006 007/** Network table data types. */ 008public enum NetworkTableType { 009 /** Unassigned data type. */ 010 kUnassigned(0, ""), 011 /** Boolean data type. */ 012 kBoolean(0x01, "boolean"), 013 /** Double precision floating-point data type. */ 014 kDouble(0x02, "double"), 015 /** String data type. */ 016 kString(0x04, "string"), 017 /** Raw data type. */ 018 kRaw(0x08, "raw"), 019 /** Boolean array data type. */ 020 kBooleanArray(0x10, "boolean[]"), 021 /** Double precision floating-point array data type. */ 022 kDoubleArray(0x20, "double[]"), 023 /** String array data type. */ 024 kStringArray(0x40, "string[]"), 025 /** Integer data type. */ 026 kInteger(0x100, "int"), 027 /** Single precision floating-point data type. */ 028 kFloat(0x200, "float"), 029 /** Integer array data type. */ 030 kIntegerArray(0x400, "int[]"), 031 /** Single precision floating-point array data type. */ 032 kFloatArray(0x800, "float[]"); 033 034 private final int m_value; 035 private final String m_valueStr; 036 037 NetworkTableType(int value, String valueStr) { 038 m_value = value; 039 m_valueStr = valueStr; 040 } 041 042 /** 043 * Returns the NetworkTable type value. 044 * 045 * @return The NetworkTable type value. 046 */ 047 public int getValue() { 048 return m_value; 049 } 050 051 /** 052 * Returns the NetworkTable type value as as string. 053 * 054 * @return The NetworkTable type value as a string. 055 */ 056 public String getValueStr() { 057 return m_valueStr; 058 } 059 060 /** 061 * Convert from the numerical representation of type to an enum type. 062 * 063 * @param value The numerical representation of kind 064 * @return The kind 065 */ 066 public static NetworkTableType getFromInt(int value) { 067 return switch (value) { 068 case 0x01 -> kBoolean; 069 case 0x02 -> kDouble; 070 case 0x04 -> kString; 071 case 0x08 -> kRaw; 072 case 0x10 -> kBooleanArray; 073 case 0x20 -> kDoubleArray; 074 case 0x40 -> kStringArray; 075 case 0x100 -> kInteger; 076 case 0x200 -> kFloat; 077 case 0x400 -> kIntegerArray; 078 case 0x800 -> kFloatArray; 079 default -> kUnassigned; 080 }; 081 } 082 083 /** 084 * Convert from a type string to an enum type. 085 * 086 * @param typeString type string 087 * @return The kind 088 */ 089 public static NetworkTableType getFromString(String typeString) { 090 return switch (typeString) { 091 case "boolean" -> kBoolean; 092 case "double" -> kDouble; 093 case "float" -> kFloat; 094 case "int" -> kInteger; 095 case "string", "json" -> kString; 096 case "boolean[]" -> kBooleanArray; 097 case "double[]" -> kDoubleArray; 098 case "float[]" -> kFloatArray; 099 case "int[]" -> kIntegerArray; 100 case "string[]" -> kStringArray; 101 case "" -> kUnassigned; 102 default -> kRaw; 103 }; 104 } 105 106 /** 107 * Gets string from generic data value. 108 * 109 * @param data the data to check 110 * @return type string of the data, or empty string if no match 111 */ 112 public static String getStringFromObject(Object data) { 113 if (data instanceof Boolean) { 114 return "boolean"; 115 } else if (data instanceof Float) { 116 return "float"; 117 } else if (data instanceof Long) { 118 // Checking Long because NT supports 64-bit integers 119 return "int"; 120 } else if (data instanceof Double || data instanceof Number) { 121 // If typeof Number class, return "double" as the type. Functions as a "catch-all". 122 return "double"; 123 } else if (data instanceof String) { 124 return "string"; 125 } else if (data instanceof boolean[] || data instanceof Boolean[]) { 126 return "boolean[]"; 127 } else if (data instanceof float[] || data instanceof Float[]) { 128 return "float[]"; 129 } else if (data instanceof long[] || data instanceof Long[]) { 130 return "int[]"; 131 } else if (data instanceof double[] || data instanceof Double[] || data instanceof Number[]) { 132 // If typeof Number class, return "double[]" as the type. Functions as a "catch-all". 133 return "double[]"; 134 } else if (data instanceof String[]) { 135 return "string[]"; 136 } else if (data instanceof byte[] || data instanceof Byte[]) { 137 return "raw"; 138 } else { 139 return ""; 140 } 141 } 142}