33import com .team2813 .lib2813 .util .InputValidation ;
44import java .util .Optional ;
55
6+ /**
7+ * Immutable value class representing the identifying information for a CAN device.
8+ *
9+ * <p>This class encapsulates the essential information needed to uniquely identify
10+ * a device on the CAN bus network: the CAN ID and the specific CAN bus the device
11+ * is connected to. It provides a standardized way to represent device identity
12+ * across the team's control system architecture.
13+ *
14+ * <p>The class distinguishes between devices on the RoboRIO's built-in CAN bus
15+ * (represented by an empty Optional for the canbus) and devices on named CAN buses
16+ * such as CANivore or other CAN bus interfaces.
17+ *
18+ * <p>Key features:
19+ * <ul>
20+ * <li>Immutable design for thread safety and reliable identity</li>
21+ * <li>Input validation for CAN ID range [0, 62]</li>
22+ * <li>Proper equals() and hashCode() implementation for use in collections</li>
23+ * <li>Optional-based canbus representation for null safety</li>
24+ * </ul>
25+ *
26+ * <p>This class is commonly used as a key in device registries and for comparing
27+ * device instances to determine if they represent the same physical hardware.
28+ *
29+ * @author Team 2813
30+ * @since 1.0
31+ */
632public final class DeviceInformation {
7- private int id ;
8- private Optional <String > canbus ;
9-
10- /**
11- * Creates a DeviceInformation for a device on the RoboRIO can loop
12- *
13- * @param id the can ID
14- */
15- public DeviceInformation (int id ) {
16- this (id , null );
17- }
18-
19- /**
20- * Creates a DeviceInformation with a canbus string. If {@code canbus} is {@code null}, method
21- * acts like {@link #DeviceInformation(int)} was called
22- *
23- * @param id the CAN id
24- * @param canbus the canbus string
25- */
26- public DeviceInformation (int id , String canbus ) {
27- this .id = InputValidation .checkCanId (id );
28- this .canbus = Optional .ofNullable (canbus );
29- }
30-
31- /**
32- * Gets the can id of this device
33- *
34- * @return the can id of the device
35- */
36- public int id () {
37- return id ;
38- }
39-
40- /**
41- * Returns the canbus that this device is on, or {@link Optional#empty()} if it is on the RoboRIO
42- * can loop
43- *
44- * @return the canbus that the device is on
45- */
46- public Optional <String > canbus () {
47- return canbus ;
48- }
49-
50- @ Override
51- public boolean equals (Object o ) {
52- if (!(o instanceof DeviceInformation )) return false ;
53- DeviceInformation other = (DeviceInformation ) o ;
54- return other .id == id && other .canbus .equals (canbus );
55- }
56-
57- @ Override
58- public int hashCode () {
59- return id * 31 + canbus .hashCode ();
60- }
61- }
33+
34+ /** The CAN ID of the device, validated to be in range [0, 62] */
35+ private int id ;
36+
37+ /** The CAN bus name, empty if on the RoboRIO's default CAN bus */
38+ private Optional <String > canbus ;
39+
40+ /**
41+ * Creates DeviceInformation for a device on the RoboRIO's default CAN bus.
42+ *
43+ * <p>This constructor is used for devices connected directly to the RoboRIO's
44+ * built-in CAN bus interface. The canbus will be represented as an empty
45+ * Optional to indicate the default bus.
46+ *
47+ * @param id the CAN ID of the device, must be in range [0, 62]
48+ * @throws com.team2813.lib2813.util.InvalidCanIdException if the CAN ID is outside the valid range
49+ */
50+ public DeviceInformation (int id ) {
51+ this (id , null );
52+ }
53+
54+ /**
55+ * Creates DeviceInformation with a specific CAN bus name.
56+ *
57+ * <p>This constructor supports devices on named CAN buses such as CANivore
58+ * devices or other CAN bus interfaces. If {@code canbus} is {@code null},
59+ * this method behaves identically to {@link #DeviceInformation(int)}.
60+ *
61+ * <p>Examples of valid canbus names:
62+ * <ul>
63+ * <li>{@code "canivore"} - For CANivore devices</li>
64+ * <li>{@code "CANivore_12345"} - For CANivore with specific serial number</li>
65+ * <li>{@code null} - For RoboRIO default CAN bus</li>
66+ * </ul>
67+ *
68+ * @param id the CAN ID of the device, must be in range [0, 62]
69+ * @param canbus the CAN bus name, or {@code null} for the RoboRIO default bus
70+ * @throws com.team2813.lib2813.util.InvalidCanIdException if the CAN ID is outside the valid range
71+ */
72+ public DeviceInformation (int id , String canbus ) {
73+ this .id = InputValidation .checkCanId (id );
74+ this .canbus = Optional .ofNullable (canbus );
75+ }
76+
77+ /**
78+ * Gets the CAN ID of this device.
79+ *
80+ * <p>The CAN ID is guaranteed to be in the valid range [0, 62] due to
81+ * validation performed during construction.
82+ *
83+ * @return the CAN ID of the device
84+ */
85+ public int id () {
86+ return id ;
87+ }
88+
89+ /**
90+ * Returns the CAN bus that this device is connected to.
91+ *
92+ * <p>The return value interpretation:
93+ * <ul>
94+ * <li>{@link Optional#empty()} - Device is on the RoboRIO's default CAN bus</li>
95+ * <li>{@code Optional.of("busname")} - Device is on the named CAN bus</li>
96+ * </ul>
97+ *
98+ * <p>This Optional-based approach provides null safety and makes the
99+ * distinction between default and named buses explicit in the API.
100+ *
101+ * @return an Optional containing the CAN bus name, or empty if on the RoboRIO CAN bus
102+ */
103+ public Optional <String > canbus () {
104+ return canbus ;
105+ }
106+
107+ /**
108+ * Determines whether this DeviceInformation is equal to another object.
109+ *
110+ * <p>Two DeviceInformation instances are considered equal if and only if
111+ * they have the same CAN ID and are on the same CAN bus. This allows
112+ * DeviceInformation objects to be used reliably as keys in hash-based
113+ * collections and for device identity comparisons.
114+ *
115+ * @param o the object to compare against
116+ * @return {@code true} if the objects represent the same device,
117+ * {@code false} otherwise
118+ */
119+ @ Override
120+ public boolean equals (Object o ) {
121+ if (!(o instanceof DeviceInformation )) return false ;
122+ DeviceInformation other = (DeviceInformation ) o ;
123+ return other .id == id && other .canbus .equals (canbus );
124+ }
125+
126+ /**
127+ * Returns a hash code value for this DeviceInformation.
128+ *
129+ * <p>The hash code is computed from both the CAN ID and the CAN bus
130+ * information, ensuring that equal DeviceInformation objects have
131+ * equal hash codes. This implementation supports the use of
132+ * DeviceInformation objects in hash-based collections such as
133+ * HashMap and HashSet.
134+ *
135+ * <p>The hash code formula combines the ID and canbus hash using
136+ * a prime multiplier for good distribution properties.
137+ *
138+ * @return a hash code value for this object
139+ */
140+ @ Override
141+ public int hashCode () {
142+ return id * 31 + canbus .hashCode ();
143+ }
144+ }
0 commit comments