From 84a76e8aa6585af573d084aea8a23912c12517be Mon Sep 17 00:00:00 2001 From: Gold856 <117957790+Gold856@users.noreply.github.com> Date: Sun, 16 Jun 2024 15:12:49 -0400 Subject: [PATCH] Use two separate caches and Pair.of --- .../wpilibj2/command/button/CommandGenericHID.java | 13 ++++++++----- .../java/edu/wpi/first/wpilibj/GenericHID.java | 14 +++++++------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/CommandGenericHID.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/CommandGenericHID.java index e0919d96e36..49311577252 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/CommandGenericHID.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/CommandGenericHID.java @@ -19,7 +19,10 @@ public class CommandGenericHID { private final GenericHID m_hid; private final Map> m_buttonCache = new HashMap<>(); - private final Map, Trigger>> m_axisCache = new HashMap<>(); + private final Map, Trigger>> m_axisLessThanCache = + new HashMap<>(); + private final Map, Trigger>> m_axisGreaterThanCache = + new HashMap<>(); private final Map> m_povCache = new HashMap<>(); /** @@ -222,9 +225,9 @@ public Trigger axisLessThan(int axis, double threshold) { * threshold. */ public Trigger axisLessThan(int axis, double threshold, EventLoop loop) { - var cache = m_axisCache.computeIfAbsent(loop, k -> new HashMap<>()); + var cache = m_axisLessThanCache.computeIfAbsent(loop, k -> new HashMap<>()); return cache.computeIfAbsent( - new Pair<>(axis, threshold), k -> new Trigger(loop, () -> getRawAxis(axis) < threshold)); + Pair.of(axis, threshold), k -> new Trigger(loop, () -> getRawAxis(axis) < threshold)); } /** @@ -252,9 +255,9 @@ public Trigger axisGreaterThan(int axis, double threshold) { * threshold. */ public Trigger axisGreaterThan(int axis, double threshold, EventLoop loop) { - var cache = m_axisCache.computeIfAbsent(loop, k -> new HashMap<>()); + var cache = m_axisGreaterThanCache.computeIfAbsent(loop, k -> new HashMap<>()); return cache.computeIfAbsent( - new Pair<>(axis, threshold), k -> new Trigger(loop, () -> getRawAxis(axis) > threshold)); + Pair.of(axis, threshold), k -> new Trigger(loop, () -> getRawAxis(axis) > threshold)); } /** diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/GenericHID.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/GenericHID.java index c5992de9a92..fd3f0f3bf2e 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/GenericHID.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/GenericHID.java @@ -99,7 +99,9 @@ public static HIDType of(int value) { private int m_leftRumble; private int m_rightRumble; private final Map> m_buttonCache = new HashMap<>(); - private final Map, BooleanEvent>> m_axisCache = + private final Map, BooleanEvent>> m_axisLessThanCache = + new HashMap<>(); + private final Map, BooleanEvent>> m_axisGreaterThanCache = new HashMap<>(); private final Map> m_povCache = new HashMap<>(); @@ -344,10 +346,9 @@ public BooleanEvent povCenter(EventLoop loop) { * @return an event instance that is true when the axis value is less than the provided threshold. */ public BooleanEvent axisLessThan(int axis, double threshold, EventLoop loop) { - var cache = m_axisCache.computeIfAbsent(loop, k -> new HashMap<>()); + var cache = m_axisLessThanCache.computeIfAbsent(loop, k -> new HashMap<>()); return cache.computeIfAbsent( - new Pair<>(axis, -threshold), - k -> new BooleanEvent(loop, () -> getRawAxis(axis) < threshold)); + Pair.of(axis, threshold), k -> new BooleanEvent(loop, () -> getRawAxis(axis) < threshold)); } /** @@ -361,10 +362,9 @@ public BooleanEvent axisLessThan(int axis, double threshold, EventLoop loop) { * threshold. */ public BooleanEvent axisGreaterThan(int axis, double threshold, EventLoop loop) { - var cache = m_axisCache.computeIfAbsent(loop, k -> new HashMap<>()); + var cache = m_axisGreaterThanCache.computeIfAbsent(loop, k -> new HashMap<>()); return cache.computeIfAbsent( - new Pair<>(axis, threshold), - k -> new BooleanEvent(loop, () -> getRawAxis(axis) > threshold)); + Pair.of(axis, threshold), k -> new BooleanEvent(loop, () -> getRawAxis(axis) > threshold)); } /**