Skip to content

Commit 331d203

Browse files
committed
Use ConcurrentMap and Collections.unmodifiableMap()
1 parent dc8145f commit 331d203

1 file changed

Lines changed: 27 additions & 17 deletions

File tree

core/src/main/java/com/team2813/lib2813/preferences/PersistedConfiguration.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
import edu.wpi.first.wpilibj.DriverStation;
2828
import edu.wpi.first.wpilibj.Preferences;
2929
import java.lang.reflect.*;
30+
import java.util.Collections;
3031
import java.util.EnumSet;
3132
import java.util.HashMap;
3233
import java.util.Map;
34+
import java.util.concurrent.ConcurrentHashMap;
35+
import java.util.concurrent.ConcurrentMap;
3336
import java.util.concurrent.atomic.AtomicBoolean;
3437
import java.util.concurrent.atomic.AtomicInteger;
3538
import java.util.concurrent.atomic.AtomicLong;
@@ -401,34 +404,40 @@ Object getValue(
401404
ListenerRegistry listenerRegistry);
402405
}
403406

404-
private static final Map<Type, PreferenceFetcher> TYPE_TO_FETCHER = new HashMap<>();
407+
private static final Map<Type, PreferenceFetcher> TYPE_TO_FETCHER = createTypeToFetcherMap();
405408

406409
/**
407410
* Registers a preference fetcher with a type.
408411
*
412+
* @param typeToFetcher The map to update.
409413
* @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.
411415
*/
412416
@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) {
414421
PreferenceFetcher fetcher =
415422
(component, key, defaultValue, initializePreference, listenerConsumers) ->
416-
simpleFetcher.getValue(
423+
genericFetcher.getValue(
417424
component, key, (T) defaultValue, initializePreference, listenerConsumers);
418-
TYPE_TO_FETCHER.put(type, fetcher);
425+
typeToFetcher.put(type, fetcher);
419426
}
420427

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);
432441
}
433442

434443
/** Gets a boolean value from Preferences for the given component. */
@@ -721,7 +730,8 @@ private PersistedConfiguration() {
721730
/** Registry that supports adding Preference value listeners. */
722731
private static class ListenerRegistry {
723732
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<>();
725735
private final NetworkTable preferencesTable;
726736

727737
ListenerRegistry(NetworkTableInstance ntInstance, String preferenceName) {

0 commit comments

Comments
 (0)