[wpiutil] Rename struct constants to all caps

This commit is contained in:
Peter Johnson
2026-03-15 22:37:36 -07:00
parent c913b27a27
commit aa88fa0fcf
48 changed files with 286 additions and 288 deletions

View File

@@ -125,7 +125,7 @@ public final class DynamicStruct {
* @throws ArrayIndexOutOfBoundsException if array index is out of bounds
*/
public boolean getBoolField(StructFieldDescriptor field, int arrIndex) {
if (field.getType() != StructFieldType.kBool) {
if (field.getType() != StructFieldType.BOOL) {
throw new UnsupportedOperationException("field is not bool type");
}
return getFieldImpl(field, arrIndex) != 0;
@@ -157,7 +157,7 @@ public final class DynamicStruct {
* @throws ReadOnlyBufferException if the underlying buffer is read-only
*/
public void setBoolField(StructFieldDescriptor field, boolean value, int arrIndex) {
if (field.getType() != StructFieldType.kBool) {
if (field.getType() != StructFieldType.BOOL) {
throw new UnsupportedOperationException("field is not bool type");
}
setFieldImpl(field, value ? 1 : 0, arrIndex);
@@ -253,7 +253,7 @@ public final class DynamicStruct {
* @throws ArrayIndexOutOfBoundsException if array index is out of bounds
*/
public float getFloatField(StructFieldDescriptor field, int arrIndex) {
if (field.getType() != StructFieldType.kFloat) {
if (field.getType() != StructFieldType.FLOAT) {
throw new UnsupportedOperationException("field is not float type");
}
return Float.intBitsToFloat((int) getFieldImpl(field, arrIndex));
@@ -285,7 +285,7 @@ public final class DynamicStruct {
* @throws ReadOnlyBufferException if the underlying buffer is read-only
*/
public void setFloatField(StructFieldDescriptor field, float value, int arrIndex) {
if (field.getType() != StructFieldType.kFloat) {
if (field.getType() != StructFieldType.FLOAT) {
throw new UnsupportedOperationException("field is not float type");
}
setFieldImpl(field, Float.floatToIntBits(value), arrIndex);
@@ -317,7 +317,7 @@ public final class DynamicStruct {
* @throws ArrayIndexOutOfBoundsException if array index is out of bounds
*/
public double getDoubleField(StructFieldDescriptor field, int arrIndex) {
if (field.getType() != StructFieldType.kDouble) {
if (field.getType() != StructFieldType.DOUBLE) {
throw new UnsupportedOperationException("field is not double type");
}
return Double.longBitsToDouble(getFieldImpl(field, arrIndex));
@@ -349,7 +349,7 @@ public final class DynamicStruct {
* @throws ReadOnlyBufferException if the underlying buffer is read-only
*/
public void setDoubleField(StructFieldDescriptor field, double value, int arrIndex) {
if (field.getType() != StructFieldType.kDouble) {
if (field.getType() != StructFieldType.DOUBLE) {
throw new UnsupportedOperationException("field is not double type");
}
setFieldImpl(field, Double.doubleToLongBits(value), arrIndex);
@@ -380,7 +380,7 @@ public final class DynamicStruct {
*/
@SuppressWarnings({"PMD.CollapsibleIfStatements", "PMD.AvoidDeeplyNestedIfStmts"})
public String getStringField(StructFieldDescriptor field) {
if (field.getType() != StructFieldType.kChar) {
if (field.getType() != StructFieldType.CHAR) {
throw new UnsupportedOperationException("field is not char type");
}
if (!field.getParent().equals(m_desc)) {
@@ -454,7 +454,7 @@ public final class DynamicStruct {
* @throws IllegalStateException if struct descriptor is invalid
*/
public boolean setStringField(StructFieldDescriptor field, String value) {
if (field.getType() != StructFieldType.kChar) {
if (field.getType() != StructFieldType.CHAR) {
throw new UnsupportedOperationException("field is not char type");
}
if (!field.getParent().equals(m_desc)) {
@@ -485,7 +485,7 @@ public final class DynamicStruct {
* @throws ArrayIndexOutOfBoundsException if array index is out of bounds
*/
public DynamicStruct getStructField(StructFieldDescriptor field, int arrIndex) {
if (field.getType() != StructFieldType.kStruct) {
if (field.getType() != StructFieldType.STRUCT) {
throw new UnsupportedOperationException("field is not struct type");
}
if (!field.getParent().equals(m_desc)) {
@@ -528,7 +528,7 @@ public final class DynamicStruct {
* @throws ReadOnlyBufferException if the underlying buffer is read-only
*/
public void setStructField(StructFieldDescriptor field, DynamicStruct value, int arrIndex) {
if (field.getType() != StructFieldType.kStruct) {
if (field.getType() != StructFieldType.STRUCT) {
throw new UnsupportedOperationException("field is not struct type");
}
if (!field.getParent().equals(m_desc)) {
@@ -588,7 +588,7 @@ public final class DynamicStruct {
default -> throw new IllegalStateException("invalid field size");
};
if (field.isUint() || field.getType() == StructFieldType.kBool) {
if (field.isUint() || field.getType() == StructFieldType.BOOL) {
// for unsigned fields, we can simply logical shift and mask
return (val >>> field.m_bitShift) & field.getBitMask();
} else {

View File

@@ -21,25 +21,25 @@ import java.nio.ByteBuffer;
*/
public interface Struct<T> {
/** Serialized size of a "bool" value. */
int kSizeBool = 1;
int BOOL_SIZE = 1;
/** Serialized size of an "int8" or "uint8" value. */
int kSizeInt8 = 1;
int INT8_SIZE = 1;
/** Serialized size of an "int16" or "uint16" value. */
int kSizeInt16 = 2;
int INT16_SIZE = 2;
/** Serialized size of an "int32" or "uint32" value. */
int kSizeInt32 = 4;
int INT32_SIZE = 4;
/** Serialized size of an "int64" or "uint64" value. */
int kSizeInt64 = 8;
int INT64_SIZE = 8;
/** Serialized size of an "float" or "float32" value. */
int kSizeFloat = 4;
int FLOAT_SIZE = 4;
/** Serialized size of an "double" or "float64" value. */
int kSizeDouble = 8;
int DOUBLE_SIZE = 8;
/**
* Gets the Class object for the stored value.

View File

@@ -113,7 +113,7 @@ public class StructDescriptor {
offset += field.m_size * field.m_arraySize;
} else {
int bitWidth = field.getBitWidth();
if (field.getType() == StructFieldType.kBool
if (field.getType() == StructFieldType.BOOL
&& prevBitfieldSize != 0
&& (shift + 1) <= (prevBitfieldSize * 8)) {
// bool takes on size of preceding bitfield type (if it fits)

View File

@@ -47,7 +47,7 @@ public class StructDescriptorDatabase {
// bitfield checks
if (decl.bitWidth != 0) {
// only integer or boolean types are allowed
if (!type.isInt && !type.isUint && type != StructFieldType.kBool) {
if (!type.isInt && !type.isUint && type != StructFieldType.BOOL) {
throw new BadSchemaException(
decl.name, "type " + decl.typeString + " cannot be bitfield");
}
@@ -59,7 +59,7 @@ public class StructDescriptorDatabase {
}
// bit width must be 1 for booleans
if (type == StructFieldType.kBool && decl.bitWidth != 1) {
if (type == StructFieldType.BOOL && decl.bitWidth != 1) {
throw new BadSchemaException(decl.name, "bit width must be 1 for bool type");
}
@@ -71,7 +71,7 @@ public class StructDescriptorDatabase {
// struct handling
StructDescriptor structDesc = null;
if (type == StructFieldType.kStruct) {
if (type == StructFieldType.STRUCT) {
// recursive definitions are not allowed
if (decl.typeString.equals(name)) {
throw new BadSchemaException(decl.name, "recursive struct reference");

View File

@@ -7,31 +7,31 @@ package org.wpilib.util.struct;
/** Known data types for raw struct dynamic fields (see StructFieldDescriptor). */
public enum StructFieldType {
/** bool. */
kBool("bool", false, false, 1),
BOOL("bool", false, false, 1),
/** char. */
kChar("char", false, false, 1),
CHAR("char", false, false, 1),
/** int8. */
kInt8("int8", true, false, 1),
INT8("int8", true, false, 1),
/** int16. */
kInt16("int16", true, false, 2),
INT16("int16", true, false, 2),
/** int32. */
kInt32("int32", true, false, 4),
INT32("int32", true, false, 4),
/** int64. */
kInt64("int64", true, false, 8),
INT64("int64", true, false, 8),
/** uint8. */
kUint8("uint8", false, true, 1),
UINT8("uint8", false, true, 1),
/** uint16. */
kUint16("uint16", false, true, 2),
UINT16("uint16", false, true, 2),
/** uint32. */
kUint32("uint32", false, true, 4),
UINT32("uint32", false, true, 4),
/** uint64. */
kUint64("uint64", false, true, 8),
UINT64("uint64", false, true, 8),
/** float. */
kFloat("float", false, false, 4),
FLOAT("float", false, false, 4),
/** double. */
kDouble("double", false, false, 8),
DOUBLE("double", false, false, 8),
/** struct. */
kStruct("struct", false, false, 0);
STRUCT("struct", false, false, 0);
/** The name of the data type. */
@SuppressWarnings("MemberName")
@@ -74,11 +74,11 @@ public enum StructFieldType {
}
}
if ("float32".equals(str)) {
return kFloat;
return FLOAT;
} else if ("float64".equals(str)) {
return kDouble;
return DOUBLE;
} else {
return kStruct;
return STRUCT;
}
}
}

View File

@@ -28,21 +28,21 @@ public class Lexer {
m_tokenStart = m_pos - 1;
return switch (m_current) {
case '[' -> TokenKind.kLeftBracket;
case ']' -> TokenKind.kRightBracket;
case '{' -> TokenKind.kLeftBrace;
case '}' -> TokenKind.kRightBrace;
case ':' -> TokenKind.kColon;
case ';' -> TokenKind.kSemicolon;
case ',' -> TokenKind.kComma;
case '=' -> TokenKind.kEquals;
case '[' -> TokenKind.LEFT_BRACKET;
case ']' -> TokenKind.RIGHT_BRACKET;
case '{' -> TokenKind.LEFT_BRACE;
case '}' -> TokenKind.RIGHT_BRACE;
case ':' -> TokenKind.COLON;
case ';' -> TokenKind.SEMICOLON;
case ',' -> TokenKind.COMMA;
case '=' -> TokenKind.EQUALS;
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' -> scanInteger();
case '\0' -> TokenKind.kEndOfInput;
case '\0' -> TokenKind.END_OF_INPUT;
default -> {
if (Character.isLetter(m_current) || m_current == '_') {
yield scanIdentifier();
}
yield TokenKind.kUnknown;
yield TokenKind.UNKNOWN;
}
};
}
@@ -73,7 +73,7 @@ public class Lexer {
get();
} while (Character.isDigit(m_current));
unget();
return TokenKind.kInteger;
return TokenKind.INTEGER;
}
private TokenKind scanIdentifier() {
@@ -81,7 +81,7 @@ public class Lexer {
get();
} while (Character.isLetterOrDigit(m_current) || m_current == '_');
unget();
return TokenKind.kIdentifier;
return TokenKind.IDENTIFIER;
}
private void get() {

View File

@@ -28,14 +28,14 @@ public class Parser {
ParsedSchema schema = new ParsedSchema();
do {
getNextToken();
if (m_token == TokenKind.kSemicolon) {
if (m_token == TokenKind.SEMICOLON) {
continue;
}
if (m_token == TokenKind.kEndOfInput) {
if (m_token == TokenKind.END_OF_INPUT) {
break;
}
schema.declarations.add(parseDeclaration());
} while (m_token != TokenKind.kEndOfInput);
} while (m_token != TokenKind.END_OF_INPUT);
return schema;
}
@@ -43,30 +43,30 @@ public class Parser {
ParsedDeclaration decl = new ParsedDeclaration();
// optional enum specification
if (m_token == TokenKind.kIdentifier && "enum".equals(m_lexer.getTokenText())) {
if (m_token == TokenKind.IDENTIFIER && "enum".equals(m_lexer.getTokenText())) {
getNextToken();
expect(TokenKind.kLeftBrace);
expect(TokenKind.LEFT_BRACE);
decl.enumValues = parseEnum();
getNextToken();
} else if (m_token == TokenKind.kLeftBrace) {
} else if (m_token == TokenKind.LEFT_BRACE) {
decl.enumValues = parseEnum();
getNextToken();
}
// type name
expect(TokenKind.kIdentifier);
expect(TokenKind.IDENTIFIER);
decl.typeString = m_lexer.getTokenText();
getNextToken();
// identifier name
expect(TokenKind.kIdentifier);
expect(TokenKind.IDENTIFIER);
decl.name = m_lexer.getTokenText();
getNextToken();
// array or bit field
if (m_token == TokenKind.kLeftBracket) {
if (m_token == TokenKind.LEFT_BRACKET) {
getNextToken();
expect(TokenKind.kInteger);
expect(TokenKind.INTEGER);
String valueStr = m_lexer.getTokenText();
int value;
try {
@@ -81,11 +81,11 @@ public class Parser {
m_lexer.m_pos, "array size '" + valueStr + "' is not a positive integer");
}
getNextToken();
expect(TokenKind.kRightBracket);
expect(TokenKind.RIGHT_BRACKET);
getNextToken();
} else if (m_token == TokenKind.kColon) {
} else if (m_token == TokenKind.COLON) {
getNextToken();
expect(TokenKind.kInteger);
expect(TokenKind.INTEGER);
String valueStr = m_lexer.getTokenText();
int value;
try {
@@ -103,8 +103,8 @@ public class Parser {
}
// declaration must end with EOF or semicolon
if (m_token != TokenKind.kEndOfInput) {
expect(TokenKind.kSemicolon);
if (m_token != TokenKind.END_OF_INPUT) {
expect(TokenKind.SEMICOLON);
}
return decl;
@@ -115,13 +115,13 @@ public class Parser {
// we start with current = '{'
getNextToken();
while (m_token != TokenKind.kRightBrace) {
expect(TokenKind.kIdentifier);
while (m_token != TokenKind.RIGHT_BRACE) {
expect(TokenKind.IDENTIFIER);
final String name = m_lexer.getTokenText();
getNextToken();
expect(TokenKind.kEquals);
expect(TokenKind.EQUALS);
getNextToken();
expect(TokenKind.kInteger);
expect(TokenKind.INTEGER);
String valueStr = m_lexer.getTokenText();
long value;
try {
@@ -131,10 +131,10 @@ public class Parser {
}
map.put(name, value);
getNextToken();
if (m_token == TokenKind.kRightBrace) {
if (m_token == TokenKind.RIGHT_BRACE) {
break;
}
expect(TokenKind.kComma);
expect(TokenKind.COMMA);
getNextToken();
}
return map;

View File

@@ -7,40 +7,40 @@ package org.wpilib.util.struct.parser;
/** A lexed raw struct schema token. */
public enum TokenKind {
/** Unknown. */
kUnknown("unknown"),
UNKNOWN("unknown"),
/** Integer. */
kInteger("integer"),
INTEGER("integer"),
/** Identifier. */
kIdentifier("identifier"),
IDENTIFIER("identifier"),
/** Left square bracket. */
kLeftBracket("'['"),
LEFT_BRACKET("'['"),
/** Right square bracket. */
kRightBracket("']'"),
RIGHT_BRACKET("']'"),
/** Left curly brace. */
kLeftBrace("'{'"),
LEFT_BRACE("'{'"),
/** Right curly brace. */
kRightBrace("'}'"),
RIGHT_BRACE("'}'"),
/** Colon. */
kColon("':'"),
COLON("':'"),
/** Semicolon. */
kSemicolon("';'"),
SEMICOLON("';'"),
/** Comma. */
kComma("','"),
COMMA("','"),
/** Equals. */
kEquals("'='"),
EQUALS("'='"),
/** End of input. */
kEndOfInput("<EOF>");
END_OF_INPUT("<EOF>");
private final String m_name;