From be724e7d7ee030b2d83f7b755ca2e7c9ff2e8500 Mon Sep 17 00:00:00 2001 From: Daniel Chen Date: Wed, 11 Dec 2024 17:13:33 -0500 Subject: [PATCH] java format --- .../wpi/first/util/struct/StructFetcher.java | 107 +++++++++--------- .../util/struct/StructGeneratorTest.java | 9 +- 2 files changed, 56 insertions(+), 60 deletions(-) diff --git a/wpiutil/src/main/java/edu/wpi/first/util/struct/StructFetcher.java b/wpiutil/src/main/java/edu/wpi/first/util/struct/StructFetcher.java index 2546052f83d..7c310a824db 100644 --- a/wpiutil/src/main/java/edu/wpi/first/util/struct/StructFetcher.java +++ b/wpiutil/src/main/java/edu/wpi/first/util/struct/StructFetcher.java @@ -3,60 +3,59 @@ import java.util.Optional; /** - * A utility class for fetching the assigned struct of existing classes. - * These are usually public, static, and final properties with - * the Struct type. + * A utility class for fetching the assigned struct of existing classes. These are usually public, + * static, and final properties with the Struct type. */ public final class StructFetcher { - private StructFetcher() { - throw new UnsupportedOperationException("This is a utility class!"); - } - - /** - * Returns a {@link Struct} for the given {@link StructSerializable} marked class. Due to the - * non-contractual nature of the marker this can fail. If the {@code struct} field could not be - * accessed for any reason, an empty {@link Optional} is returned. - * - * @param The type of the class. - * @param clazz The class object to extract the struct from. - * @return An optional containing the struct if it could be extracted. - */ - @SuppressWarnings("unchecked") - public static Optional> fetchStruct( - Class clazz) { - try { - var possibleField = Optional.ofNullable(clazz.getDeclaredField("struct")); - return possibleField.flatMap( - field -> { - if (Struct.class.isAssignableFrom(field.getType())) { - try { - return Optional.ofNullable((Struct) field.get(null)); - } catch (IllegalAccessException e) { - return Optional.empty(); - } - } else { - return Optional.empty(); - } - }); - } catch (NoSuchFieldException e) { - return Optional.empty(); - } - } - - /** - * Returns a {@link Struct} for the given class. This does not do compile time checking that the - * class is a {@link StructSerializable}. Whenever possible it is reccomended to use {@link - * #fetchStruct(Class)}. - * - * @param clazz The class object to extract the struct from. - * @return An optional containing the struct if it could be extracted. - */ - @SuppressWarnings("unchecked") - public static Optional> fetchStructDynamic(Class clazz) { - if (StructSerializable.class.isAssignableFrom(clazz)) { - return fetchStruct((Class) clazz).map(struct -> struct); - } else { - return Optional.empty(); - } - } + private StructFetcher() { + throw new UnsupportedOperationException("This is a utility class!"); + } + + /** + * Returns a {@link Struct} for the given {@link StructSerializable} marked class. Due to the + * non-contractual nature of the marker this can fail. If the {@code struct} field could not be + * accessed for any reason, an empty {@link Optional} is returned. + * + * @param The type of the class. + * @param clazz The class object to extract the struct from. + * @return An optional containing the struct if it could be extracted. + */ + @SuppressWarnings("unchecked") + public static Optional> fetchStruct( + Class clazz) { + try { + var possibleField = Optional.ofNullable(clazz.getDeclaredField("struct")); + return possibleField.flatMap( + field -> { + if (Struct.class.isAssignableFrom(field.getType())) { + try { + return Optional.ofNullable((Struct) field.get(null)); + } catch (IllegalAccessException e) { + return Optional.empty(); + } + } else { + return Optional.empty(); + } + }); + } catch (NoSuchFieldException e) { + return Optional.empty(); + } + } + + /** + * Returns a {@link Struct} for the given class. This does not do compile time checking that the + * class is a {@link StructSerializable}. Whenever possible it is reccomended to use {@link + * #fetchStruct(Class)}. + * + * @param clazz The class object to extract the struct from. + * @return An optional containing the struct if it could be extracted. + */ + @SuppressWarnings("unchecked") + public static Optional> fetchStructDynamic(Class clazz) { + if (StructSerializable.class.isAssignableFrom(clazz)) { + return fetchStruct((Class) clazz).map(struct -> struct); + } else { + return Optional.empty(); + } + } } diff --git a/wpiutil/src/test/java/edu/wpi/first/util/struct/StructGeneratorTest.java b/wpiutil/src/test/java/edu/wpi/first/util/struct/StructGeneratorTest.java index 95bb3010567..c5a1eea25be 100644 --- a/wpiutil/src/test/java/edu/wpi/first/util/struct/StructGeneratorTest.java +++ b/wpiutil/src/test/java/edu/wpi/first/util/struct/StructGeneratorTest.java @@ -95,8 +95,7 @@ public final int hashCode() { @SuppressWarnings("unchecked") private void testStructRoundTrip(S value) { - Struct struct = - StructFetcher.fetchStruct((Class) value.getClass()).get(); + Struct struct = StructFetcher.fetchStruct((Class) value.getClass()).get(); ByteBuffer buffer = ByteBuffer.allocate(struct.getSize()); buffer.order(ByteOrder.LITTLE_ENDIAN); struct.pack(buffer, value); @@ -108,8 +107,7 @@ private void testStructRoundTrip(S value) { @SuppressWarnings("unchecked") private void testStructDoublePack(S value) { - Struct struct = - StructFetcher.fetchStruct((Class) value.getClass()).get(); + Struct struct = StructFetcher.fetchStruct((Class) value.getClass()).get(); ByteBuffer buffer = ByteBuffer.allocate(struct.getSize()); buffer.order(ByteOrder.LITTLE_ENDIAN); struct.pack(buffer, value); @@ -123,8 +121,7 @@ private void testStructDoublePack(S value) { @SuppressWarnings("unchecked") private void testStructDoubleUnpack(S value) { - Struct struct = - StructFetcher.fetchStruct((Class) value.getClass()).get(); + Struct struct = StructFetcher.fetchStruct((Class) value.getClass()).get(); ByteBuffer buffer = ByteBuffer.allocate(struct.getSize()); buffer.order(ByteOrder.LITTLE_ENDIAN); struct.pack(buffer, value);