|
27 | 27 | import edu.wpi.first.wpilibj.DriverStation; |
28 | 28 | import edu.wpi.first.wpilibj.Preferences; |
29 | 29 | import java.lang.reflect.*; |
| 30 | +import java.util.Collections; |
30 | 31 | import java.util.EnumSet; |
31 | 32 | import java.util.HashMap; |
32 | 33 | import java.util.Map; |
| 34 | +import java.util.concurrent.ConcurrentHashMap; |
| 35 | +import java.util.concurrent.ConcurrentMap; |
33 | 36 | import java.util.concurrent.atomic.AtomicBoolean; |
34 | 37 | import java.util.concurrent.atomic.AtomicInteger; |
35 | 38 | import java.util.concurrent.atomic.AtomicLong; |
@@ -401,34 +404,40 @@ Object getValue( |
401 | 404 | ListenerRegistry listenerRegistry); |
402 | 405 | } |
403 | 406 |
|
404 | | - private static final Map<Type, PreferenceFetcher> TYPE_TO_FETCHER = new HashMap<>(); |
| 407 | + private static final Map<Type, PreferenceFetcher> TYPE_TO_FETCHER = createTypeToFetcherMap(); |
405 | 408 |
|
406 | 409 | /** |
407 | 410 | * Registers a preference fetcher with a type. |
408 | 411 | * |
| 412 | + * @param typeToFetcher The map to update. |
409 | 413 | * @param type The type to register. |
410 | | - * @param simpleFetcher The fetcher that should be used to create values of the given type. |
| 414 | + * @param genericFetcher The fetcher that should be used to create values of the given type. |
411 | 415 | */ |
412 | 416 | @SuppressWarnings("unchecked") |
413 | | - private static <T> void register(Class<T> type, GenericPreferenceFetcher<T> simpleFetcher) { |
| 417 | + private static <T> void register( |
| 418 | + Map<Type, PreferenceFetcher> typeToFetcher, |
| 419 | + Class<T> type, |
| 420 | + GenericPreferenceFetcher<T> genericFetcher) { |
414 | 421 | PreferenceFetcher fetcher = |
415 | 422 | (component, key, defaultValue, initializePreference, listenerConsumers) -> |
416 | | - simpleFetcher.getValue( |
| 423 | + genericFetcher.getValue( |
417 | 424 | component, key, (T) defaultValue, initializePreference, listenerConsumers); |
418 | | - TYPE_TO_FETCHER.put(type, fetcher); |
| 425 | + typeToFetcher.put(type, fetcher); |
419 | 426 | } |
420 | 427 |
|
421 | | - static { |
422 | | - register(Boolean.TYPE, PersistedConfiguration::booleanFetcher); |
423 | | - register(BooleanSupplier.class, PersistedConfiguration::booleanSupplierFetcher); |
424 | | - register(Integer.TYPE, PersistedConfiguration::intFetcher); |
425 | | - register(IntSupplier.class, PersistedConfiguration::intSupplierFetcher); |
426 | | - register(Long.TYPE, PersistedConfiguration::longFetcher); |
427 | | - register(LongSupplier.class, PersistedConfiguration::longSupplierFetcher); |
428 | | - register(Double.TYPE, PersistedConfiguration::doubleFetcher); |
429 | | - register(DoubleSupplier.class, PersistedConfiguration::doubleSupplierFetcher); |
430 | | - register(String.class, PersistedConfiguration::stringFetcher); |
431 | | - register(Supplier.class, PersistedConfiguration::supplierFetcher); |
| 428 | + private static Map<Type, PreferenceFetcher> createTypeToFetcherMap() { |
| 429 | + Map<Type, PreferenceFetcher> map = new HashMap<>(); |
| 430 | + register(map, Boolean.TYPE, PersistedConfiguration::booleanFetcher); |
| 431 | + register(map, BooleanSupplier.class, PersistedConfiguration::booleanSupplierFetcher); |
| 432 | + register(map, Integer.TYPE, PersistedConfiguration::intFetcher); |
| 433 | + register(map, IntSupplier.class, PersistedConfiguration::intSupplierFetcher); |
| 434 | + register(map, Long.TYPE, PersistedConfiguration::longFetcher); |
| 435 | + register(map, LongSupplier.class, PersistedConfiguration::longSupplierFetcher); |
| 436 | + register(map, Double.TYPE, PersistedConfiguration::doubleFetcher); |
| 437 | + register(map, DoubleSupplier.class, PersistedConfiguration::doubleSupplierFetcher); |
| 438 | + register(map, String.class, PersistedConfiguration::stringFetcher); |
| 439 | + register(map, Supplier.class, PersistedConfiguration::supplierFetcher); |
| 440 | + return Collections.unmodifiableMap(map); |
432 | 441 | } |
433 | 442 |
|
434 | 443 | /** Gets a boolean value from Preferences for the given component. */ |
@@ -721,7 +730,8 @@ private PersistedConfiguration() { |
721 | 730 | /** Registry that supports adding Preference value listeners. */ |
722 | 731 | private static class ListenerRegistry { |
723 | 732 | private static final String PREFERENCE_TABLE_NAME = "Preferences"; |
724 | | - private final Map<Integer, Consumer<NetworkTableValue>> topicToConsumer = new HashMap<>(); |
| 733 | + private final ConcurrentMap<Integer, Consumer<NetworkTableValue>> topicToConsumer = |
| 734 | + new ConcurrentHashMap<>(); |
725 | 735 | private final NetworkTable preferencesTable; |
726 | 736 |
|
727 | 737 | ListenerRegistry(NetworkTableInstance ntInstance, String preferenceName) { |
|
0 commit comments