From 15da5babff51ae902d8316f6a4017ec19717a168 Mon Sep 17 00:00:00 2001 From: Kevin Cooney Date: Sat, 3 Jan 2026 22:34:26 -0800 Subject: [PATCH 1/3] Remove support for Supplier<{Boolean,Integer,Long,Double}> This reduces complexity in the PersistedConfiguration code. Users can still use BooleanSupplier, IntegerSupplier, LongSupplier, DoubleSupplier or Supplier. --- .../preferences/PersistedConfiguration.java | 146 ++++++++---------- .../PersistedConfigurationTest.java | 101 +++--------- 2 files changed, 89 insertions(+), 158 deletions(-) diff --git a/lib/src/main/java/com/team2813/lib2813/preferences/PersistedConfiguration.java b/lib/src/main/java/com/team2813/lib2813/preferences/PersistedConfiguration.java index 203750e8..63dda5d0 100644 --- a/lib/src/main/java/com/team2813/lib2813/preferences/PersistedConfiguration.java +++ b/lib/src/main/java/com/team2813/lib2813/preferences/PersistedConfiguration.java @@ -149,10 +149,10 @@ public final class PersistedConfiguration { *

To be stored in preferences, the type of the record components can be any of the following: * *

@@ -180,10 +180,10 @@ public static T fromPreferences(String preferenceName, T conf *

To be stored in preferences, the type of the record components can be any of the following: * *

    - *
  • {@code boolean} or {@code BooleanSupplier} or {@code Supplier} - *
  • {@code int} or {@code IntSupplier} or {@code Supplier} - *
  • {@code long} or {@code LongSupplier} or {@code Supplier} - *
  • {@code double} or {@code DoubleSupplier} or {@code Supplier} + *
  • {@code boolean} or {@code BooleanSupplier} + *
  • {@code int} or {@code IntSupplier} + *
  • {@code long} or {@code LongSupplier} + *
  • {@code double} or {@code DoubleSupplier} *
  • {@code String} or {@code Supplier} *
  • {@code Record} following the above rules *
@@ -294,13 +294,13 @@ private static T createFromPreferences( types[i] = type; boolean needComponentValue; - PreferenceFactory factory = null; + PreferenceFetcher fetcher = null; boolean isRecordField = Record.class.isAssignableFrom(type); if (isRecordField) { needComponentValue = true; } else { - factory = TYPE_TO_FACTORY.get(type); - if (factory == null) { + fetcher = TYPE_TO_FETCHER.get(type); + if (fetcher == null) { // Cannot get value from Preferences; copy over the value from the input record. needComponentValue = true; } else { @@ -309,23 +309,20 @@ private static T createFromPreferences( } Object componentValue = null; - if (needComponentValue) { - if (configWithDefaults != null) { - Field defaultValueField = clazz.getDeclaredField(name); - defaultValueField.setAccessible(true); - componentValue = defaultValueField.get(configWithDefaults); - } + if (needComponentValue && configWithDefaults != null) { + Field defaultValueField = clazz.getDeclaredField(name); + defaultValueField.setAccessible(true); + componentValue = defaultValueField.get(configWithDefaults); } if (isRecordField) { params[i] = createFromPreferences(key, type, componentValue); - } else if (factory == null) { + } else if (fetcher == null) { warn("Cannot store '%s' in Preferences; type %s is unsupported", name, type); params[i] = componentValue; } else { - // Fetch the value from Preferences params[i] = - factory.create( + fetcher.getValue( component, key, componentValue, /* initializePreference= */ needComponentValue); } i++; @@ -339,7 +336,7 @@ private static T createFromPreferences( * Type-safe functional interface for creating an instance of a type using data in Preferences. */ @FunctionalInterface - private interface GenericPreferenceFactory { + private interface GenericPreferenceFetcher { /** * Gets a value from Preferences for the given component. * @@ -349,7 +346,7 @@ private interface GenericPreferenceFactory { * @param initializePreference Whether the preference should be initialized. * @return The value; will match the type in "component"; */ - T create(RecordComponent component, String key, T defaultValue, boolean initializePreference); + T getValue(RecordComponent component, String key, T defaultValue, boolean initializePreference); } /** @@ -358,7 +355,7 @@ private interface GenericPreferenceFactory { *

Note: this interface exists to avoid ugly casts in the code that uses the reflection APIs. */ @FunctionalInterface - private interface PreferenceFactory { + private interface PreferenceFetcher { /** * Gets a value from Preferences for the given component. * @@ -368,50 +365,41 @@ private interface PreferenceFactory { * @param initializePreference Whether the preference should be initialized. * @return The value; will match the type in "component"; */ - Object create( + Object getValue( RecordComponent component, String key, Object defaultValue, boolean initializePreference); } - private static final Map TYPE_TO_FACTORY = new HashMap<>(); + private static final Map TYPE_TO_FETCHER = new HashMap<>(); /** - * Registers a preference factory with a type. + * Registers a preference fetcher with a type. * * @param type The type to register. - * @param simpleFactory The factory that should be used to create values of the given type. + * @param simpleFetcher The fetcher that should be used to create values of the given type. */ @SuppressWarnings("unchecked") - private static void register(Class type, GenericPreferenceFactory simpleFactory) { - PreferenceFactory factory = + private static void register(Class type, GenericPreferenceFetcher simpleFetcher) { + PreferenceFetcher fetcher = (component, key, defaultValue, initializePreference) -> - simpleFactory.create(component, key, (T) defaultValue, initializePreference); - TYPE_TO_FACTORY.put(type, factory); + simpleFetcher.getValue(component, key, (T) defaultValue, initializePreference); + TYPE_TO_FETCHER.put(type, fetcher); } static { - register(Boolean.TYPE, PersistedConfiguration::booleanFactory); - register(BooleanSupplier.class, PersistedConfiguration::booleanSupplierFactory); - register(Integer.TYPE, PersistedConfiguration::intFactory); - register(IntSupplier.class, PersistedConfiguration::intSupplierFactory); - register(Long.TYPE, PersistedConfiguration::longFactory); - register(LongSupplier.class, PersistedConfiguration::longSupplierFactory); - register(Double.TYPE, PersistedConfiguration::doubleFactory); - register(DoubleSupplier.class, PersistedConfiguration::doubleSupplierFactory); - register(String.class, PersistedConfiguration::stringFactory); - register(Supplier.class, PersistedConfiguration::supplierFactory); + register(Boolean.TYPE, PersistedConfiguration::booleanFetcher); + register(BooleanSupplier.class, PersistedConfiguration::booleanSupplierFetcher); + register(Integer.TYPE, PersistedConfiguration::intFetcher); + register(IntSupplier.class, PersistedConfiguration::intSupplierFetcher); + register(Long.TYPE, PersistedConfiguration::longFetcher); + register(LongSupplier.class, PersistedConfiguration::longSupplierFetcher); + register(Double.TYPE, PersistedConfiguration::doubleFetcher); + register(DoubleSupplier.class, PersistedConfiguration::doubleSupplierFetcher); + register(String.class, PersistedConfiguration::stringFetcher); + register(Supplier.class, PersistedConfiguration::supplierFetcher); } - /** Maps the generic types supported by Preferences to their primitive types. */ - private static final Map SUPPLIER_TYPE_TO_REGISTERED_TYPE = - Map.of( - Boolean.class, Boolean.TYPE, - Integer.class, Integer.TYPE, - Long.class, Long.TYPE, - Double.class, Double.TYPE, - String.class, String.class); - /** Gets a boolean value from Preferences for the given component. */ - private static boolean booleanFactory( + private static boolean booleanFetcher( RecordComponent component, String key, Boolean defaultValue, boolean initialize) { if (initialize) { if (defaultValue == null) { @@ -424,7 +412,7 @@ private static boolean booleanFactory( } /** Gets a BooleanSupplier value from Preferences for the given component. */ - private static BooleanSupplier booleanSupplierFactory( + private static BooleanSupplier booleanSupplierFetcher( RecordComponent component, String key, BooleanSupplier defaultValueSupplier, @@ -440,7 +428,7 @@ private static BooleanSupplier booleanSupplierFactory( } /** Gets an int value from Preferences for the given component. */ - private static int intFactory( + private static int intFetcher( RecordComponent component, String key, Integer defaultValue, boolean initialize) { if (initialize) { if (defaultValue == null) { @@ -453,7 +441,7 @@ private static int intFactory( } /** Gets a IntSupplier value from Preferences for the given component. */ - private static IntSupplier intSupplierFactory( + private static IntSupplier intSupplierFetcher( RecordComponent component, String key, IntSupplier defaultValueSupplier, boolean initialize) { if (initialize) { int defaultValue = defaultValueSupplier != null ? defaultValueSupplier.getAsInt() : 0; @@ -463,7 +451,7 @@ private static IntSupplier intSupplierFactory( } /** Gets a long value from Preferences for the given component. */ - private static long longFactory( + private static long longFetcher( RecordComponent component, String key, Long defaultValue, boolean initialize) { if (initialize) { if (defaultValue == null) { @@ -476,7 +464,7 @@ private static long longFactory( } /** Gets a LongSupplier value from Preferences for the given component. */ - private static LongSupplier longSupplierFactory( + private static LongSupplier longSupplierFetcher( RecordComponent component, String key, LongSupplier defaultValueSupplier, @@ -489,7 +477,7 @@ private static LongSupplier longSupplierFactory( } /** Gets a double value from Preferences for the given component. */ - private static double doubleFactory( + private static double doubleFetcher( RecordComponent component, String key, Double defaultValue, boolean initialize) { if (initialize) { if (defaultValue == null) { @@ -502,7 +490,7 @@ private static double doubleFactory( } /** Gets a DoubleSupplier value from Preferences for the given component. */ - private static DoubleSupplier doubleSupplierFactory( + private static DoubleSupplier doubleSupplierFetcher( RecordComponent component, String key, DoubleSupplier defaultValueSupplier, @@ -515,7 +503,7 @@ private static DoubleSupplier doubleSupplierFactory( } /** Gets a String value from Preferences for the given component. */ - private static String stringFactory( + private static String stringFetcher( RecordComponent component, String key, String defaultValue, boolean initialize) { if (initialize) { if (defaultValue == null) { @@ -528,36 +516,34 @@ private static String stringFactory( } /** - * Gets a Supplier value from Preferences for the given component. Supports String, long, int, - * boolean and float values. + * Gets a Supplier<String> value from Preferences for the given component. Supports String, + * long, int, boolean and float values. */ - private static Supplier supplierFactory( - RecordComponent component, String key, Supplier defaultValueSupplier, boolean initialize) { + private static Supplier supplierFetcher( + RecordComponent component, + String key, + Supplier defaultValueSupplier, + boolean initialize) { Type supplierType = ((ParameterizedType) component.getGenericType()).getActualTypeArguments()[0]; - Type registeredType = SUPPLIER_TYPE_TO_REGISTERED_TYPE.get(supplierType); - if (registeredType == null) { - warn( - "Cannot store '%s' in Preferences; type %s is unsupported", - component.getName(), component.getGenericType()); + if (!String.class.equals(supplierType)) { + String formatString = + initialize + ? "Cannot store '%s' in Preferences; type %s is unsupported" + : "Cannot read '%s' from Preferences; type %s is unsupported"; + warn(formatString, component.getName(), component.getGenericType()); return defaultValueSupplier; } - PreferenceFactory factory = TYPE_TO_FACTORY.get(registeredType); if (initialize) { - Object defaultValue = null; - if (defaultValueSupplier != null) { - defaultValue = defaultValueSupplier.get(); - if (defaultValue == null) { - warn("Cannot store '%s' in Preferences; default value is null", component.getName()); - return defaultValueSupplier; - } + String defaultValue = defaultValueSupplier != null ? defaultValueSupplier.get() : ""; + if (defaultValue == null) { + defaultValue = ""; + warn("Cannot get initial value for '%s'; Supplier returned null", component.getName()); } - factory.create( - component, key, defaultValue, true); // Call Preferences.init{String,Double,etc}() + Preferences.initString(key, defaultValue); } - - return () -> factory.create(component, key, null, false); + return () -> Preferences.getString(key, ""); } /** Deletes Preferences that were created by older versions of this class. */ diff --git a/lib/src/test/java/com/team2813/lib2813/preferences/PersistedConfigurationTest.java b/lib/src/test/java/com/team2813/lib2813/preferences/PersistedConfigurationTest.java index 28e896d3..7be1e470 100644 --- a/lib/src/test/java/com/team2813/lib2813/preferences/PersistedConfigurationTest.java +++ b/lib/src/test/java/com/team2813/lib2813/preferences/PersistedConfigurationTest.java @@ -346,9 +346,7 @@ public static class BooleanPreferencesTest static final String PREFERENCE_NAME = "Booleans"; static final String BOOLEAN_VALUE_KEY = "Booleans/booleanValue"; static final String BOOLEAN_SUPPLIER_KEY = "Booleans/booleanSupplier"; - static final String SUPPLIER_BOOLEAN_KEY = "Booleans/supplierBoolean"; - static final Set ALL_KEYS = - Set.of(BOOLEAN_VALUE_KEY, BOOLEAN_SUPPLIER_KEY, SUPPLIER_BOOLEAN_KEY); + static final Set ALL_KEYS = Set.of(BOOLEAN_VALUE_KEY, BOOLEAN_SUPPLIER_KEY); final boolean defaultValue; @Parameters(name = "defaultValue={0}") @@ -362,12 +360,10 @@ public BooleanPreferencesTest(boolean defaultValue) { } /** Test record for testing classes that contain boolean fields. */ - private record RecordWithBooleans( - boolean booleanValue, BooleanSupplier booleanSupplier, Supplier supplierBoolean) { + private record RecordWithBooleans(boolean booleanValue, BooleanSupplier booleanSupplier) { static RecordWithBooleans withDefaultValue(boolean defaultValue) { - return new RecordWithBooleans( - defaultValue, () -> defaultValue, () -> Boolean.valueOf(defaultValue)); + return new RecordWithBooleans(defaultValue, () -> defaultValue); } } @@ -380,13 +376,11 @@ protected RecordWithBooleans createRecordWithConfiguredDefaults() { protected void assertHasConfiguredDefaults(RecordWithBooleans record) { assertThat(record.booleanValue()).isEqualTo(defaultValue); assertThat(record.booleanSupplier().getAsBoolean()).isEqualTo(defaultValue); - assertThat(record.supplierBoolean().get()).isEqualTo(Boolean.valueOf(defaultValue)); } @Override protected void assertPreferencesHaveConfiguredDefaults() { - assertThat(preferenceKeys()) - .containsExactly(BOOLEAN_VALUE_KEY, BOOLEAN_SUPPLIER_KEY, SUPPLIER_BOOLEAN_KEY); + assertThat(preferenceKeys()).containsExactly(BOOLEAN_VALUE_KEY, BOOLEAN_SUPPLIER_KEY); for (String key : ALL_KEYS) { assertThat(getBooleanValue(key, !defaultValue)).isEqualTo(defaultValue); } @@ -396,13 +390,11 @@ protected void assertPreferencesHaveConfiguredDefaults() { protected void assertHasJavaDefaults(RecordWithBooleans record) { assertThat(record.booleanValue()).isFalse(); assertThat(record.booleanSupplier().getAsBoolean()).isFalse(); - assertThat(record.supplierBoolean().get()).isFalse(); } @Override protected void assertPreferencesHaveJavaDefaults() { - assertThat(preferenceKeys()) - .containsExactly(BOOLEAN_VALUE_KEY, BOOLEAN_SUPPLIER_KEY, SUPPLIER_BOOLEAN_KEY); + assertThat(preferenceKeys()).containsExactly(BOOLEAN_VALUE_KEY, BOOLEAN_SUPPLIER_KEY); for (String key : ALL_KEYS) { assertThat(getBooleanValue(key, true)).isFalse(); } @@ -428,14 +420,12 @@ protected void assertHasUpdatedValues(ValuesKind kind, RecordWithBooleans record boolean expectedValue = getUpdatedValue(kind); assertThat(record.booleanValue()).isEqualTo(expectedValue); assertThat(record.booleanSupplier().getAsBoolean()).isEqualTo(expectedValue); - assertThat(record.supplierBoolean().get()).isEqualTo(Boolean.valueOf(expectedValue)); } @Override protected void assertSuppliersHaveUpdatedValues(RecordWithBooleans record) { boolean expectedValue = getUpdatedValue(ValuesKind.UPDATED_VALUES); assertThat(record.booleanSupplier().getAsBoolean()).isEqualTo(expectedValue); - assertThat(record.supplierBoolean().get()).isEqualTo(Boolean.valueOf(expectedValue)); } } @@ -445,7 +435,6 @@ public static class IntPreferencesTest static final String PREFERENCE_NAME = "Integers"; static final String INT_VALUE_KEY = "Integers/intValue"; static final String INT_SUPPLIER_KEY = "Integers/intSupplier"; - static final String SUPPLIER_INT_KEY = "Integers/supplierInt"; final boolean storeAsDoubles; @Parameters(name = "storeAsDoubles={0}") @@ -459,50 +448,42 @@ public IntPreferencesTest(boolean storeAsDoubles) { } /** Test record for testing classes that contain int fields. */ - private record RecordWithInts( - int intValue, IntSupplier intSupplier, Supplier supplierInt) { + private record RecordWithInts(int intValue, IntSupplier intSupplier) { - static RecordWithInts withDefaultValues( - int intValue, int intSupplierValue, int supplierIntValue) { - return new RecordWithInts(intValue, () -> intSupplierValue, () -> supplierIntValue); + static RecordWithInts withDefaultValues(int intValue, int intSupplierValue) { + return new RecordWithInts(intValue, () -> intSupplierValue); } } @Override protected RecordWithInts createRecordWithConfiguredDefaults() { - return RecordWithInts.withDefaultValues(1, 2, 3); + return RecordWithInts.withDefaultValues(1, 2); } @Override protected void assertHasConfiguredDefaults(RecordWithInts record) { assertThat(record.intValue()).isEqualTo(1); assertThat(record.intSupplier.getAsInt()).isEqualTo(2); - assertThat(record.supplierInt().get()).isEqualTo(Integer.valueOf(3)); } @Override protected void assertPreferencesHaveConfiguredDefaults() { - assertThat(preferenceKeys()) - .containsExactly(INT_VALUE_KEY, INT_SUPPLIER_KEY, SUPPLIER_INT_KEY); + assertThat(preferenceKeys()).containsExactly(INT_VALUE_KEY, INT_SUPPLIER_KEY); assertThat(getIntegerValue(INT_VALUE_KEY)).isEqualTo(1); assertThat(getIntegerValue(INT_SUPPLIER_KEY)).isEqualTo(2); - assertThat(getIntegerValue(SUPPLIER_INT_KEY)).isEqualTo(3); } @Override protected void assertHasJavaDefaults(RecordWithInts record) { assertThat(record.intValue()).isEqualTo(0); assertThat(record.intSupplier.getAsInt()).isEqualTo(0); - assertThat(record.supplierInt().get()).isEqualTo(Integer.valueOf(0)); } @Override protected void assertPreferencesHaveJavaDefaults() { - assertThat(preferenceKeys()) - .containsExactly(INT_VALUE_KEY, INT_SUPPLIER_KEY, SUPPLIER_INT_KEY); + assertThat(preferenceKeys()).containsExactly(INT_VALUE_KEY, INT_SUPPLIER_KEY); assertThat(getIntegerValue(INT_VALUE_KEY)).isEqualTo(0); assertThat(getIntegerValue(INT_SUPPLIER_KEY)).isEqualTo(0); - assertThat(getIntegerValue(SUPPLIER_INT_KEY)).isEqualTo(0); } @Override @@ -512,12 +493,10 @@ protected void updatePreferenceValues(ValuesKind kind) { case INITIAL_VALUES -> { Preferences.setDouble(INT_VALUE_KEY, 101); Preferences.setDouble(INT_SUPPLIER_KEY, 102); - Preferences.setDouble(SUPPLIER_INT_KEY, 103); } case UPDATED_VALUES -> { Preferences.setDouble(INT_VALUE_KEY, 201); Preferences.setDouble(INT_SUPPLIER_KEY, 202); - Preferences.setDouble(SUPPLIER_INT_KEY, 203); } } } else { @@ -525,12 +504,10 @@ protected void updatePreferenceValues(ValuesKind kind) { case INITIAL_VALUES -> { setIntegerValue(INT_VALUE_KEY, 101); setIntegerValue(INT_SUPPLIER_KEY, 102); - setIntegerValue(SUPPLIER_INT_KEY, 103); } case UPDATED_VALUES -> { setIntegerValue(INT_VALUE_KEY, 201); setIntegerValue(INT_SUPPLIER_KEY, 202); - setIntegerValue(SUPPLIER_INT_KEY, 203); } } } @@ -542,12 +519,10 @@ protected void assertHasUpdatedValues(ValuesKind kind, RecordWithInts record) { case INITIAL_VALUES -> { assertThat(record.intValue()).isEqualTo(101); assertThat(record.intSupplier.getAsInt()).isEqualTo(102); - assertThat(record.supplierInt().get()).isEqualTo(Integer.valueOf(103)); } case UPDATED_VALUES -> { assertThat(record.intValue()).isEqualTo(201); assertThat(record.intSupplier.getAsInt()).isEqualTo(202); - assertThat(record.supplierInt().get()).isEqualTo(Integer.valueOf(203)); } } } @@ -555,7 +530,6 @@ protected void assertHasUpdatedValues(ValuesKind kind, RecordWithInts record) { @Override protected void assertSuppliersHaveUpdatedValues(RecordWithInts record) { assertThat(record.intSupplier.getAsInt()).isEqualTo(202); - assertThat(record.supplierInt().get()).isEqualTo(Integer.valueOf(203)); } } @@ -564,57 +538,48 @@ public static class LongPreferencesTest static final String PREFERENCE_NAME = "Longs"; static final String LONG_VALUE_KEY = "Longs/longValue"; static final String LONG_SUPPLIER_KEY = "Longs/longSupplier"; - static final String SUPPLIER_LONG_KEY = "Longs/supplierLong"; public LongPreferencesTest() { super(PREFERENCE_NAME, RecordWithLongs.class); } /** Test record for testing classes that contain long fields. */ - private record RecordWithLongs( - long longValue, LongSupplier longSupplier, Supplier supplierLong) { + private record RecordWithLongs(long longValue, LongSupplier longSupplier) { - static RecordWithLongs withDefaultValues( - long longValue, long longSupplierValue, long supplierLongValue) { - return new RecordWithLongs(longValue, () -> longSupplierValue, () -> supplierLongValue); + static RecordWithLongs withDefaultValues(long longValue, long longSupplierValue) { + return new RecordWithLongs(longValue, () -> longSupplierValue); } } @Override protected RecordWithLongs createRecordWithConfiguredDefaults() { - return RecordWithLongs.withDefaultValues(1, 2, 3); + return RecordWithLongs.withDefaultValues(1, 2); } @Override protected void assertHasConfiguredDefaults(RecordWithLongs record) { assertThat(record.longValue()).isEqualTo(1); assertThat(record.longSupplier.getAsLong()).isEqualTo(2); - assertThat(record.supplierLong().get()).isEqualTo(Long.valueOf(3)); } @Override protected void assertPreferencesHaveConfiguredDefaults() { - assertThat(preferenceKeys()) - .containsExactly(LONG_VALUE_KEY, LONG_SUPPLIER_KEY, SUPPLIER_LONG_KEY); + assertThat(preferenceKeys()).containsExactly(LONG_VALUE_KEY, LONG_SUPPLIER_KEY); assertThat(getIntegerValue(LONG_VALUE_KEY)).isEqualTo(1); assertThat(getIntegerValue(LONG_SUPPLIER_KEY)).isEqualTo(2); - assertThat(getIntegerValue(SUPPLIER_LONG_KEY)).isEqualTo(3); } @Override protected void assertHasJavaDefaults(RecordWithLongs record) { assertThat(record.longValue()).isEqualTo(0); assertThat(record.longSupplier.getAsLong()).isEqualTo(0); - assertThat(record.supplierLong().get()).isEqualTo(Long.valueOf(0)); } @Override protected void assertPreferencesHaveJavaDefaults() { - assertThat(preferenceKeys()) - .containsExactly(LONG_VALUE_KEY, LONG_SUPPLIER_KEY, SUPPLIER_LONG_KEY); + assertThat(preferenceKeys()).containsExactly(LONG_VALUE_KEY, LONG_SUPPLIER_KEY); assertThat(getIntegerValue(LONG_VALUE_KEY)).isEqualTo(0); assertThat(getIntegerValue(LONG_SUPPLIER_KEY)).isEqualTo(0); - assertThat(getIntegerValue(SUPPLIER_LONG_KEY)).isEqualTo(0); } @Override @@ -623,12 +588,10 @@ protected void updatePreferenceValues(ValuesKind kind) { case INITIAL_VALUES -> { Preferences.setLong(LONG_VALUE_KEY, 10); Preferences.setLong(LONG_SUPPLIER_KEY, 20); - Preferences.setLong(SUPPLIER_LONG_KEY, 30); } case UPDATED_VALUES -> { Preferences.setLong(LONG_VALUE_KEY, 100); Preferences.setLong(LONG_SUPPLIER_KEY, 200); - Preferences.setLong(SUPPLIER_LONG_KEY, 300); } } } @@ -639,12 +602,10 @@ protected void assertHasUpdatedValues(ValuesKind kind, RecordWithLongs record) { case INITIAL_VALUES -> { assertThat(record.longValue()).isEqualTo(10); assertThat(record.longSupplier.getAsLong()).isEqualTo(20); - assertThat(record.supplierLong().get()).isEqualTo(Long.valueOf(30)); } case UPDATED_VALUES -> { assertThat(record.longValue()).isEqualTo(100); assertThat(record.longSupplier.getAsLong()).isEqualTo(200); - assertThat(record.supplierLong().get()).isEqualTo(Long.valueOf(300)); } } } @@ -652,7 +613,6 @@ protected void assertHasUpdatedValues(ValuesKind kind, RecordWithLongs record) { @Override protected void assertSuppliersHaveUpdatedValues(RecordWithLongs record) { assertThat(record.longSupplier.getAsLong()).isEqualTo(200); - assertThat(record.supplierLong().get()).isEqualTo(Long.valueOf(300)); } } @@ -661,58 +621,48 @@ public static class DoublePreferencesTest static final String PREFERENCE_NAME = "Doubles"; static final String DOUBLE_VALUE_KEY = "Doubles/doubleValue"; static final String DOUBLE_SUPPLIER_KEY = "Doubles/doubleSupplier"; - static final String SUPPLIER_DOUBLE_KEY = "Doubles/supplierDouble"; public DoublePreferencesTest() { super(PREFERENCE_NAME, RecordWithDoubles.class); } /** Test record for testing classes that contain double fields. */ - private record RecordWithDoubles( - double doubleValue, DoubleSupplier doubleSupplier, Supplier supplierDouble) { + private record RecordWithDoubles(double doubleValue, DoubleSupplier doubleSupplier) { - static RecordWithDoubles withDefaultValues( - double doubleValue, double doubleSupplierValue, double supplierDoubleValue) { - return new RecordWithDoubles( - doubleValue, () -> doubleSupplierValue, () -> supplierDoubleValue); + static RecordWithDoubles withDefaultValues(double doubleValue, double doubleSupplierValue) { + return new RecordWithDoubles(doubleValue, () -> doubleSupplierValue); } } @Override protected RecordWithDoubles createRecordWithConfiguredDefaults() { - return RecordWithDoubles.withDefaultValues(3.14159, 2.71828, 6.28318); + return RecordWithDoubles.withDefaultValues(3.14159, 2.71828); } @Override protected void assertHasConfiguredDefaults(RecordWithDoubles record) { assertThat(record.doubleValue()).isWithin(EPSILON).of(3.14159); assertThat(record.doubleSupplier.getAsDouble()).isWithin(EPSILON).of(2.71828); - assertThat(record.supplierDouble().get()).isWithin(EPSILON).of(6.28318); } @Override protected void assertPreferencesHaveConfiguredDefaults() { - assertThat(preferenceKeys()) - .containsExactly(DOUBLE_VALUE_KEY, DOUBLE_SUPPLIER_KEY, SUPPLIER_DOUBLE_KEY); + assertThat(preferenceKeys()).containsExactly(DOUBLE_VALUE_KEY, DOUBLE_SUPPLIER_KEY); assertThat(getDoubleValue(DOUBLE_VALUE_KEY)).isWithin(EPSILON).of(3.14159); assertThat(getDoubleValue(DOUBLE_SUPPLIER_KEY)).isWithin(EPSILON).of(2.71828); - assertThat(getDoubleValue(SUPPLIER_DOUBLE_KEY)).isWithin(EPSILON).of(6.28318); } @Override protected void assertHasJavaDefaults(RecordWithDoubles record) { assertThat(record.doubleValue()).isWithin(EPSILON).of(0); assertThat(record.doubleSupplier.getAsDouble()).isWithin(EPSILON).of(0); - assertThat(record.supplierDouble().get()).isWithin(EPSILON).of(0); } @Override protected void assertPreferencesHaveJavaDefaults() { - assertThat(preferenceKeys()) - .containsExactly(DOUBLE_VALUE_KEY, DOUBLE_SUPPLIER_KEY, SUPPLIER_DOUBLE_KEY); + assertThat(preferenceKeys()).containsExactly(DOUBLE_VALUE_KEY, DOUBLE_SUPPLIER_KEY); assertThat(getDoubleValue(DOUBLE_VALUE_KEY)).isWithin(EPSILON).of(0); assertThat(getDoubleValue(DOUBLE_SUPPLIER_KEY)).isWithin(EPSILON).of(0); - assertThat(getDoubleValue(SUPPLIER_DOUBLE_KEY)).isWithin(EPSILON).of(0); } @Override @@ -721,12 +671,10 @@ protected void updatePreferenceValues(ValuesKind kind) { case INITIAL_VALUES -> { Preferences.setDouble(DOUBLE_VALUE_KEY, 1.23); Preferences.setDouble(DOUBLE_SUPPLIER_KEY, 4.56); - Preferences.setDouble(SUPPLIER_DOUBLE_KEY, 7.89); } case UPDATED_VALUES -> { Preferences.setDouble(DOUBLE_VALUE_KEY, 10.23); Preferences.setDouble(DOUBLE_SUPPLIER_KEY, 40.56); - Preferences.setDouble(SUPPLIER_DOUBLE_KEY, 70.89); } } } @@ -737,12 +685,10 @@ protected void assertHasUpdatedValues(ValuesKind kind, RecordWithDoubles record) case INITIAL_VALUES -> { assertThat(record.doubleValue()).isWithin(EPSILON).of(1.23); assertThat(record.doubleSupplier.getAsDouble()).isWithin(EPSILON).of(4.56); - assertThat(record.supplierDouble().get()).isWithin(EPSILON).of(7.89); } case UPDATED_VALUES -> { assertThat(record.doubleValue()).isWithin(EPSILON).of(10.23); assertThat(record.doubleSupplier.getAsDouble()).isWithin(EPSILON).of(40.56); - assertThat(record.supplierDouble().get()).isWithin(EPSILON).of(70.89); } } } @@ -750,7 +696,6 @@ protected void assertHasUpdatedValues(ValuesKind kind, RecordWithDoubles record) @Override protected void assertSuppliersHaveUpdatedValues(RecordWithDoubles record) { assertThat(record.doubleSupplier.getAsDouble()).isWithin(EPSILON).of(40.56); - assertThat(record.supplierDouble().get()).isWithin(EPSILON).of(70.89); } } From bcbda35b47e42cee7663626590fdc1ec1f9a4f09 Mon Sep 17 00:00:00 2001 From: Kevin Cooney Date: Sat, 31 Jan 2026 19:07:27 -0800 Subject: [PATCH 2/3] Fix JavaDoc for supplierFetcher(); update copyright --- .../team2813/lib2813/preferences/PersistedConfiguration.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/src/main/java/com/team2813/lib2813/preferences/PersistedConfiguration.java b/lib/src/main/java/com/team2813/lib2813/preferences/PersistedConfiguration.java index 63dda5d0..c4f0ca30 100644 --- a/lib/src/main/java/com/team2813/lib2813/preferences/PersistedConfiguration.java +++ b/lib/src/main/java/com/team2813/lib2813/preferences/PersistedConfiguration.java @@ -515,10 +515,7 @@ private static String stringFetcher( return Preferences.getString(key, ""); } - /** - * Gets a Supplier<String> value from Preferences for the given component. Supports String, - * long, int, boolean and float values. - */ + /** Gets a Supplier<String> value from Preferences for the given component. */ private static Supplier supplierFetcher( RecordComponent component, String key, From cc4eb160f743df86e15d017b6fa9fc33ea89fc4d Mon Sep 17 00:00:00 2001 From: Kevin Cooney Date: Sat, 31 Jan 2026 19:16:03 -0800 Subject: [PATCH 3/3] Increase timeouts for calls to waitForListenerQueue() --- .../com/team2813/lib2813/preferences/IsolatedPreferences.java | 2 +- .../testing/junit/jupiter/IsolatedNetworkTablesExtension.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/test/java/com/team2813/lib2813/preferences/IsolatedPreferences.java b/lib/src/test/java/com/team2813/lib2813/preferences/IsolatedPreferences.java index 9eb6b854..0dc32574 100644 --- a/lib/src/test/java/com/team2813/lib2813/preferences/IsolatedPreferences.java +++ b/lib/src/test/java/com/team2813/lib2813/preferences/IsolatedPreferences.java @@ -42,7 +42,7 @@ protected void before() { @Override protected void after() { - if (!tempInstance.waitForListenerQueue(.2)) { + if (!tempInstance.waitForListenerQueue(.4)) { System.err.println( "Timed out waiting for the NetworkTableInstance listener queue to empty (waited 200ms);" + " JVM may crash"); diff --git a/testing/src/main/java/com/team2813/lib2813/testing/junit/jupiter/IsolatedNetworkTablesExtension.java b/testing/src/main/java/com/team2813/lib2813/testing/junit/jupiter/IsolatedNetworkTablesExtension.java index 52717a09..eb82b005 100644 --- a/testing/src/main/java/com/team2813/lib2813/testing/junit/jupiter/IsolatedNetworkTablesExtension.java +++ b/testing/src/main/java/com/team2813/lib2813/testing/junit/jupiter/IsolatedNetworkTablesExtension.java @@ -59,7 +59,7 @@ public void afterEach(ExtensionContext context) { // This works around a race condition in WPILib where a listener registered by Preferences can // be called after the NetworkTableInstance was closed (see // https://github.com/wpilibsuite/allwpilib/issues/8215). - if (!ntInstance.waitForListenerQueue(.2)) { + if (!ntInstance.waitForListenerQueue(.4)) { System.err.println( "Timed out waiting for the NetworkTableInstance listener queue to empty (waited 200ms);" + " JVM may crash");