Skip to content

Commit

Permalink
Use two separate caches and Pair.of
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold856 committed Jul 19, 2024
1 parent 7a2f181 commit 146b18e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
public class CommandGenericHID {
private final GenericHID m_hid;
private final Map<EventLoop, Map<Integer, Trigger>> m_buttonCache = new HashMap<>();
private final Map<EventLoop, Map<Pair<Integer, Double>, Trigger>> m_axisCache = new HashMap<>();
private final Map<EventLoop, Map<Pair<Integer, Double>, Trigger>> m_axisLessThanCache =
new HashMap<>();
private final Map<EventLoop, Map<Pair<Integer, Double>, Trigger>> m_axisGreaterThanCache =
new HashMap<>();
private final Map<EventLoop, Map<Integer, Trigger>> m_povCache = new HashMap<>();

/**
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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));
}

/**
Expand Down
14 changes: 7 additions & 7 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/GenericHID.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ public static HIDType of(int value) {
private int m_leftRumble;
private int m_rightRumble;
private final Map<EventLoop, Map<Integer, BooleanEvent>> m_buttonCache = new HashMap<>();
private final Map<EventLoop, Map<Pair<Integer, Double>, BooleanEvent>> m_axisCache =
private final Map<EventLoop, Map<Pair<Integer, Double>, BooleanEvent>> m_axisLessThanCache =
new HashMap<>();
private final Map<EventLoop, Map<Pair<Integer, Double>, BooleanEvent>> m_axisGreaterThanCache =
new HashMap<>();
private final Map<EventLoop, Map<Integer, BooleanEvent>> m_povCache = new HashMap<>();

Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand Down

0 comments on commit 146b18e

Please sign in to comment.