From e7cf6bf7c58b992664525b1cae822dc4b026ee7f Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Sun, 29 Apr 2018 23:56:00 -0700 Subject: [PATCH] Fixed wpilibj GenericHID.getType() (#969) It was using array indexing to map the return value of DriverStation.getJoystickType() to HIDType when the enum should instead be constructed from the int value. C++ already does this. Fixes #968. --- .../java/edu/wpi/first/wpilibj/GenericHID.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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 11293c3098..ab4dff08f2 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/GenericHID.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/GenericHID.java @@ -8,6 +8,8 @@ package edu.wpi.first.wpilibj; import edu.wpi.first.wpilibj.hal.HAL; +import java.util.HashMap; +import java.util.Map; /** * GenericHID Interface. @@ -41,10 +43,21 @@ public abstract class GenericHID { @SuppressWarnings("MemberName") public final int value; + private static final Map map = new HashMap<>(); HIDType(int value) { this.value = value; } + + static { + for (HIDType hidType : HIDType.values()) { + map.put(hidType.value, hidType); + } + } + + public static HIDType of(int value) { + return (HIDType) map.get(value); + } } /** @@ -197,7 +210,7 @@ public abstract class GenericHID { * @return the type of the HID. */ public HIDType getType() { - return HIDType.values()[m_ds.getJoystickType(m_port)]; + return HIDType.of(m_ds.getJoystickType(m_port)); } /**