From 617ff52f18a03086854d0f13355df4e2de9af30f Mon Sep 17 00:00:00 2001 From: Austin Shalit Date: Fri, 4 Aug 2017 22:15:10 -0700 Subject: [PATCH] Use generics --- .../wpi/first/wpilibj/command/CommandGroup.java | 16 ++++++++-------- .../edu/wpi/first/wpilibj/command/Scheduler.java | 14 +++++++------- .../wpi/first/wpilibj/livewindow/LiveWindow.java | 8 ++++---- .../wpilibj/smartdashboard/SmartDashboard.java | 10 ++++------ .../edu/wpi/first/wpilibj/util/SortedVector.java | 7 ++++--- 5 files changed, 27 insertions(+), 28 deletions(-) diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/CommandGroup.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/CommandGroup.java index a33df8de1b..5ab69ce612 100644 --- a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/CommandGroup.java +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/CommandGroup.java @@ -36,14 +36,14 @@ public class CommandGroup extends Command { /** * The commands in this group (stored in entries). */ - private final Vector m_commands = new Vector(); + private final Vector m_commands = new Vector<>(); /* * Intentionally package private */ /** * The active children in this group (stored in entries). */ - final Vector m_children = new Vector(); + final Vector m_children = new Vector<>(); /** * The current command, -1 signifies that none have been run. */ @@ -239,7 +239,7 @@ public class CommandGroup extends Command { } } - entry = (Entry) m_commands.elementAt(m_currentCommandIndex); + entry = m_commands.elementAt(m_currentCommandIndex); cmd = null; switch (entry.m_state) { @@ -268,7 +268,7 @@ public class CommandGroup extends Command { // Run Children for (int i = 0; i < m_children.size(); i++) { - entry = (Entry) m_children.elementAt(i); + entry = m_children.elementAt(i); Command child = entry.m_command; if (entry.isTimedOut()) { child._cancel(); @@ -285,7 +285,7 @@ public class CommandGroup extends Command { // Theoretically, we don't have to check this, but we do if teams override // the isFinished method if (m_currentCommandIndex != -1 && m_currentCommandIndex < m_commands.size()) { - Command cmd = ((Entry) m_commands.elementAt(m_currentCommandIndex)).m_command; + Command cmd = m_commands.elementAt(m_currentCommandIndex).m_command; cmd._cancel(); cmd.removed(); } @@ -346,14 +346,14 @@ public class CommandGroup extends Command { } if (m_currentCommandIndex != -1 && m_currentCommandIndex < m_commands.size()) { - Command cmd = ((Entry) m_commands.elementAt(m_currentCommandIndex)).m_command; + Command cmd = m_commands.elementAt(m_currentCommandIndex).m_command; if (!cmd.isInterruptible()) { return false; } } for (int i = 0; i < m_children.size(); i++) { - if (!((Entry) m_children.elementAt(i)).m_command.isInterruptible()) { + if (!m_children.elementAt(i).m_command.isInterruptible()) { return false; } } @@ -363,7 +363,7 @@ public class CommandGroup extends Command { private void cancelConflicts(Command command) { for (int i = 0; i < m_children.size(); i++) { - Command child = ((Entry) m_children.elementAt(i)).m_command; + Command child = m_children.elementAt(i).m_command; Enumeration requirements = command.getRequirements(); diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/Scheduler.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/Scheduler.java index d078c303e4..4acd189aec 100644 --- a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/Scheduler.java +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/command/Scheduler.java @@ -47,7 +47,7 @@ public class Scheduler implements NamedSendable { /** * A hashtable of active {@link Command Commands} to their {@link LinkedListElement}. */ - private Hashtable m_commandTable = new Hashtable(); + private Hashtable m_commandTable = new Hashtable<>(); /** * The {@link Set} of all {@link Subsystem Subsystems}. */ @@ -71,13 +71,13 @@ public class Scheduler implements NamedSendable { /** * A list of all {@link Command Commands} which need to be added. */ - private Vector m_additions = new Vector(); + private Vector m_additions = new Vector<>(); private ITable m_table; /** * A list of all {@link edu.wpi.first.wpilibj.buttons.Trigger.ButtonScheduler Buttons}. It is * created lazily. */ - private Vector m_buttons; + private Vector m_buttons; private boolean m_runningCommandsChanged; /** @@ -111,7 +111,7 @@ public class Scheduler implements NamedSendable { */ public void addButton(ButtonScheduler button) { if (m_buttons == null) { - m_buttons = new Vector(); + m_buttons = new Vector<>(); } m_buttons.addElement(button); } @@ -195,7 +195,7 @@ public class Scheduler implements NamedSendable { // Get button input (going backwards preserves button priority) if (m_buttons != null) { for (int i = m_buttons.size() - 1; i >= 0; i--) { - ((ButtonScheduler) m_buttons.elementAt(i)).execute(); + m_buttons.elementAt(i).execute(); } } @@ -218,7 +218,7 @@ public class Scheduler implements NamedSendable { // Add the new things for (int i = 0; i < m_additions.size(); i++) { - _add((Command) m_additions.elementAt(i)); + _add(m_additions.elementAt(i)); } m_additions.removeAllElements(); @@ -257,7 +257,7 @@ public class Scheduler implements NamedSendable { if (command == null || !m_commandTable.containsKey(command)) { return; } - LinkedListElement element = (LinkedListElement) m_commandTable.get(command); + LinkedListElement element = m_commandTable.get(command); m_commandTable.remove(command); if (element.equals(m_lastCommand)) { diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java index c386b83900..48eb7ef189 100644 --- a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/livewindow/LiveWindow.java @@ -22,9 +22,9 @@ import edu.wpi.first.wpilibj.tables.ITable; */ public class LiveWindow { - private static Vector sensors = new Vector(); + private static Vector sensors = new Vector<>(); // private static Vector actuators = new Vector(); - private static Hashtable components = new Hashtable(); + private static Hashtable components = new Hashtable<>(); private static ITable livewindowTable; private static ITable statusTable; private static boolean liveWindowEnabled = false; @@ -42,7 +42,7 @@ public class LiveWindow { statusTable = livewindowTable.getSubTable("~STATUS~"); for (Enumeration e = components.keys(); e.hasMoreElements(); ) { LiveWindowSendable component = (LiveWindowSendable) e.nextElement(); - LiveWindowComponent liveWindowComponent = (LiveWindowComponent) components.get(component); + LiveWindowComponent liveWindowComponent = components.get(component); String subsystem = liveWindowComponent.getSubsystem(); String name = liveWindowComponent.getName(); System.out.println("Initializing table for '" + subsystem + "' '" + name + "'"); @@ -170,7 +170,7 @@ public class LiveWindow { private static void updateValues() { // TODO: gross - needs to be sped up for (int i = 0; i < sensors.size(); i++) { - LiveWindowSendable lws = (LiveWindowSendable) sensors.elementAt(i); + LiveWindowSendable lws = sensors.elementAt(i); lws.updateTable(); } // TODO: Add actuators? diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java index 356c95ab58..8fc4a9080f 100644 --- a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.java @@ -15,7 +15,6 @@ import edu.wpi.first.wpilibj.HLUsageReporting; import edu.wpi.first.wpilibj.NamedSendable; import edu.wpi.first.wpilibj.Sendable; import edu.wpi.first.wpilibj.networktables.NetworkTable; -import edu.wpi.first.wpilibj.networktables.NetworkTableKeyNotDefined; import edu.wpi.first.wpilibj.tables.ITable; /** @@ -34,7 +33,7 @@ public class SmartDashboard { * A table linking tables in the SmartDashboard to the {@link Sendable} objects they * came from. */ - private static final Hashtable tablesToData = new Hashtable(); + private static final Hashtable tablesToData = new Hashtable<>(); static { HLUsageReporting.reportSmartDashboard(); @@ -52,7 +51,7 @@ public class SmartDashboard { ITable dataTable = table.getSubTable(key); dataTable.putString("~TYPE~", data.getSmartDashboardType()); data.initTable(dataTable); - tablesToData.put(data, key); + tablesToData.put(dataTable, data); } @@ -75,16 +74,15 @@ public class SmartDashboard { * * @param key the key * @return the value - * @throws NetworkTableKeyNotDefined if there is no value mapped to by the key * @throws IllegalArgumentException if the key is null */ public static Sendable getData(String key) { ITable subtable = table.getSubTable(key); - Object data = tablesToData.get(subtable); + Sendable data = tablesToData.get(subtable); if (data == null) { throw new IllegalArgumentException("SmartDashboard data does not exist: " + key); } else { - return (Sendable) data; + return data; } } diff --git a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/util/SortedVector.java b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/util/SortedVector.java index 010cd702e5..7431e29aec 100644 --- a/wpilibj/src/shared/java/edu/wpi/first/wpilibj/util/SortedVector.java +++ b/wpilibj/src/shared/java/edu/wpi/first/wpilibj/util/SortedVector.java @@ -12,7 +12,7 @@ import java.util.Vector; /** * A vector that is sorted. */ -public class SortedVector extends Vector { +public class SortedVector extends Vector { /** * Interface used to determine the order to place sorted objects. @@ -49,7 +49,7 @@ public class SortedVector extends Vector { * * @param element The element to add to the Vector */ - public void addElement(Object element) { + public void addElement(E element) { int highBound = size(); int lowBound = 0; while (highBound - lowBound > 0) { @@ -70,12 +70,13 @@ public class SortedVector extends Vector { /** * Sort the vector. */ + @SuppressWarnings("unchecked") public void sort() { Object[] array = new Object[size()]; copyInto(array); removeAllElements(); for (int i = 0; i < array.length; i++) { - addElement(array[i]); + addElement((E) array[i]); } } }