Skip to content

Commit d552503

Browse files
committed
More consistent instance variable names
1 parent 08a47ef commit d552503

File tree

15 files changed

+268
-216
lines changed

15 files changed

+268
-216
lines changed

java-does-usb/src/main/java/net/codecrete/usb/USB.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
import java.util.List;
1616
import java.util.function.Consumer;
17-
import java.util.stream.Collectors;
17+
18+
import static java.util.stream.Collectors.toList;
1819

1920
/**
2021
* Provides access to USB devices.
@@ -38,14 +39,14 @@ private static USBDeviceRegistry createInstance() {
3839
return impl;
3940
}
4041

41-
private static USBDeviceRegistry _instance = null;
42+
private static USBDeviceRegistry singletonInstance = null;
4243

4344
private static synchronized USBDeviceRegistry instance() {
44-
if (_instance == null) {
45-
_instance = createInstance();
46-
_instance.start();
45+
if (singletonInstance == null) {
46+
singletonInstance = createInstance();
47+
singletonInstance.start();
4748
}
48-
return _instance;
49+
return singletonInstance;
4950
}
5051

5152
// Private, so no instance can be created
@@ -67,43 +68,43 @@ public static List<USBDevice> getAllDevices() {
6768
}
6869

6970
/**
70-
* Gets a list of connected USB devices matching the specified filter.
71+
* Gets a list of connected USB devices matching the specified predicate.
7172
*
72-
* @param filter device filter
73+
* @param predicate device predicate/filter
7374
* @return list of USB devices
7475
*/
75-
public static List<USBDevice> getDevices(USBDeviceFilter filter) {
76-
return instance().getAllDevices().stream().filter(filter::matches).collect(Collectors.toList());
76+
public static List<USBDevice> getDevices(USBDevicePredicate predicate) {
77+
return instance().getAllDevices().stream().filter(predicate::matches).collect(toList());
7778
}
7879

7980
/**
80-
* Gets a list of connected USB devices matching any of the specified filters.
81+
* Gets a list of connected USB devices matching any of the specified predicates/filters.
8182
*
82-
* @param filters list of device filters
83+
* @param predicates list of device predicates/filters
8384
* @return list of USB devices
8485
*/
85-
public static List<USBDevice> getDevices(List<USBDeviceFilter> filters) {
86-
return instance().getAllDevices().stream().filter(dev -> USBDeviceFilter.matchesAny(dev, filters)).collect(Collectors.toList());
86+
public static List<USBDevice> getDevices(List<USBDevicePredicate> predicates) {
87+
return instance().getAllDevices().stream().filter(dev -> USBDevicePredicate.matchesAny(dev, predicates)).collect(toList());
8788
}
8889

8990
/**
90-
* Gets the first connected USB device matching the specified filter.
91+
* Gets the first connected USB device matching the specified predicate.
9192
*
92-
* @param filter device filter
93+
* @param predicate device predicate/filter
9394
* @return USB device, or {@code null} if no device matches
9495
*/
95-
public static USBDevice getDevice(USBDeviceFilter filter) {
96-
return instance().getAllDevices().stream().filter(filter::matches).findFirst().orElse(null);
96+
public static USBDevice getDevice(USBDevicePredicate predicate) {
97+
return instance().getAllDevices().stream().filter(predicate::matches).findFirst().orElse(null);
9798
}
9899

99100
/**
100-
* Gets the first connected USB device matching any of the specified filters.
101+
* Gets the first connected USB device matching any of the specified predicates.
101102
*
102-
* @param filters list of device filters
103+
* @param predicates list of device predicates/filters
103104
* @return USB device, or {@code null} if no device matches
104105
*/
105-
public static USBDevice getDevice(List<USBDeviceFilter> filters) {
106-
return instance().getAllDevices().stream().filter(dev -> USBDeviceFilter.matchesAny(dev, filters)).findFirst().orElse(null);
106+
public static USBDevice getDevice(List<USBDevicePredicate> predicates) {
107+
return instance().getAllDevices().stream().filter(dev -> USBDevicePredicate.matchesAny(dev, predicates)).findFirst().orElse(null);
107108
}
108109

109110
/**

java-does-usb/src/main/java/net/codecrete/usb/USBDeviceFilter.java

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
* product ID and serial number is globally unique.
2121
* </p>
2222
*/
23-
public class USBDeviceFilter {
24-
private Integer vendorId_;
25-
private Integer productId_;
26-
private Integer classCode_;
27-
private Integer subclassCode_;
28-
private Integer protocolCode_;
29-
private String serialNumber_;
23+
public class USBDeviceFilter implements USBDevicePredicate {
24+
private Integer vid;
25+
private Integer pid;
26+
private Integer deviceClass;
27+
private Integer deviceSubclass;
28+
private Integer deviceProtocol;
29+
private String serialString;
3030

3131
/**
3232
* Creates a new instance.
@@ -41,8 +41,8 @@ public USBDeviceFilter() {
4141
* @param productId product ID
4242
*/
4343
public USBDeviceFilter(int vendorId, int productId) {
44-
vendorId_ = vendorId;
45-
productId_ = productId;
44+
vid = vendorId;
45+
pid = productId;
4646
}
4747

4848
/**
@@ -53,9 +53,9 @@ public USBDeviceFilter(int vendorId, int productId) {
5353
* @param serialNumber serial number
5454
*/
5555
public USBDeviceFilter(int vendorId, int productId, String serialNumber) {
56-
vendorId_ = vendorId;
57-
productId_ = productId;
58-
serialNumber_ = serialNumber;
56+
vid = vendorId;
57+
pid = productId;
58+
serialString = serialNumber;
5959
}
6060

6161
/**
@@ -64,7 +64,7 @@ public USBDeviceFilter(int vendorId, int productId, String serialNumber) {
6464
* @return vendor ID, or {@code null} if the vendor ID is not relevant for matching
6565
*/
6666
public Integer vendorId() {
67-
return vendorId_;
67+
return vid;
6868
}
6969

7070
/**
@@ -73,7 +73,7 @@ public Integer vendorId() {
7373
* @param vendorId vendor ID, or {@code null} if the vendor ID is not relevant for matching
7474
*/
7575
public void setVendorId(Integer vendorId) {
76-
vendorId_ = vendorId;
76+
vid = vendorId;
7777
}
7878

7979
/**
@@ -82,7 +82,7 @@ public void setVendorId(Integer vendorId) {
8282
* @return product ID, or {@code null} if the product ID is not relevant for matching
8383
*/
8484
public Integer productId() {
85-
return productId_;
85+
return pid;
8686
}
8787

8888
/**
@@ -91,7 +91,7 @@ public Integer productId() {
9191
* @param productId product ID, or {@code null} if the product ID is not relevant for matching
9292
*/
9393
public void setProductId(Integer productId) {
94-
productId_ = productId;
94+
pid = productId;
9595
}
9696

9797
/**
@@ -100,7 +100,7 @@ public void setProductId(Integer productId) {
100100
* @return class code, or {@code null} if the class code is not relevant for matching
101101
*/
102102
public Integer classCode() {
103-
return classCode_;
103+
return deviceClass;
104104
}
105105

106106
/**
@@ -109,7 +109,7 @@ public Integer classCode() {
109109
* @param classCode class code, or {@code null} if the class code is not relevant for matching
110110
*/
111111
public void setClassCode(Integer classCode) {
112-
classCode_ = classCode;
112+
deviceClass = classCode;
113113
}
114114

115115
/**
@@ -118,7 +118,7 @@ public void setClassCode(Integer classCode) {
118118
* @return subclass code, or {@code null} if the subclass code is not relevant for matching
119119
*/
120120
public Integer subclassCode() {
121-
return subclassCode_;
121+
return deviceSubclass;
122122
}
123123

124124
/**
@@ -127,7 +127,7 @@ public Integer subclassCode() {
127127
* @param subclassCode subclass code, or {@code null} if the subclass code is not relevant for matching
128128
*/
129129
public void setSubclassCode(Integer subclassCode) {
130-
subclassCode_ = subclassCode;
130+
deviceSubclass = subclassCode;
131131
}
132132

133133
/**
@@ -136,16 +136,16 @@ public void setSubclassCode(Integer subclassCode) {
136136
* @return protocol code, or {@code null} if the protocol code is not relevant for matching
137137
*/
138138
public Integer protocolCode() {
139-
return protocolCode_;
139+
return deviceProtocol;
140140
}
141141

142142
/**
143143
* Sets the USB device protocol code.
144144
*
145145
* @param protocolCode protocol code, or {@code null} if the protocol code is not relevant for matching
146146
*/
147-
public void setProtocolCode_(Integer protocolCode) {
148-
protocolCode_ = protocolCode;
147+
public void setProtocolCode(Integer protocolCode) {
148+
deviceProtocol = protocolCode;
149149
}
150150

151151
/**
@@ -154,7 +154,7 @@ public void setProtocolCode_(Integer protocolCode) {
154154
* @return serial number, or {@code null} if the serial number is not relevant for matching
155155
*/
156156
public String serialNumber() {
157-
return serialNumber_;
157+
return serialString;
158158
}
159159

160160
/**
@@ -163,7 +163,7 @@ public String serialNumber() {
163163
* @param serialNumber serial number, or {@code null} if the serial number is not relevant for matching
164164
*/
165165
public void setSerialNumber(String serialNumber) {
166-
serialNumber_ = serialNumber;
166+
serialString = serialNumber;
167167
}
168168

169169
/**
@@ -173,27 +173,16 @@ public void setSerialNumber(String serialNumber) {
173173
* @return {@code true} if it matches, {@code false} otherwise
174174
*/
175175
public boolean matches(USBDevice device) {
176-
if (vendorId_ != null && device.vendorId() != vendorId_)
176+
if (vid != null && device.vendorId() != vid)
177177
return false;
178-
if (productId_ != null && device.productId() != productId_)
178+
if (pid != null && device.productId() != pid)
179179
return false;
180-
if (serialNumber_ != null && !serialNumber_.equals(device.serialNumber()))
180+
if (serialString != null && !serialString.equals(device.serialNumber()))
181181
return false;
182-
if (classCode_ != null && device.classCode() != classCode_)
182+
if (deviceClass != null && device.classCode() != deviceClass)
183183
return false;
184-
if (subclassCode_ != null && device.subclassCode() != subclassCode_)
184+
if (deviceSubclass != null && device.subclassCode() != deviceSubclass)
185185
return false;
186-
return protocolCode_ == null || device.protocolCode() == protocolCode_;
187-
}
188-
189-
/**
190-
* Test if the USB devices matches any of the filter conditions.
191-
*
192-
* @param device the USB device
193-
* @param filters a list of filter conditions
194-
* @return {@code true} if it matches, {@code false} otherwise
195-
*/
196-
public static boolean matchesAny(USBDevice device, List<USBDeviceFilter> filters) {
197-
return filters.stream().anyMatch(filter -> filter.matches(device));
186+
return deviceProtocol == null || device.protocolCode() == deviceProtocol;
198187
}
199188
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// Java Does USB
3+
// Copyright (c) 2022 Manuel Bleichenbacher
4+
// Licensed under MIT License
5+
// https://opensource.org/licenses/MIT
6+
//
7+
8+
package net.codecrete.usb;
9+
10+
import java.util.List;
11+
12+
/**
13+
* Represents a predicate (boolean-valued function) of one argument, evaluated for a given USB device.
14+
* <p>
15+
* This is a functional interface whose functional method is {@link #matches(USBDevice)}.
16+
* </p>
17+
*/
18+
@FunctionalInterface
19+
public interface USBDevicePredicate {
20+
/**
21+
* Evaluates this predicate on the given USB device.
22+
*
23+
* @param device the USB device
24+
* @return {@code true} of the devices matches the predicate, otherwise {@code false}
25+
*/
26+
boolean matches(USBDevice device);
27+
28+
/**
29+
* Test if the USB devices matches any of the filter conditions.
30+
*
31+
* @param device the USB device
32+
* @param predicates a list of filter predicates
33+
* @return {@code true} if it matches, {@code false} otherwise
34+
*/
35+
public static boolean matchesAny(USBDevice device, List<USBDevicePredicate> predicates) {
36+
return predicates.stream().anyMatch(predicate -> predicate.matches(device));
37+
}
38+
}

java-does-usb/src/main/java/net/codecrete/usb/USBException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class USBException extends RuntimeException {
1515
/**
1616
* Error code.
1717
*/
18-
private int errorCode_ = -1;
18+
private int code = -1;
1919

2020
/**
2121
* Creates a new instance with a message.
@@ -34,7 +34,7 @@ public USBException(String message) {
3434
*/
3535
public USBException(String message, int errorCode) {
3636
super(message + " (error code: " + errorCode + ")");
37-
errorCode_ = errorCode;
37+
code = errorCode;
3838
}
3939

4040
/**
@@ -53,6 +53,6 @@ public USBException(String message, Throwable cause) {
5353
* @return the error code
5454
*/
5555
public int errorCode() {
56-
return errorCode_;
56+
return code;
5757
}
5858
}

0 commit comments

Comments
 (0)