Skip to content

Commit

Permalink
Use Java 21 language features
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Dec 29, 2023
1 parent 3fd69af commit e7a0e87
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public UsbException(@NotNull String message) {
* @param errorCode the error code
*/
public UsbException(@NotNull String message, int errorCode) {
super(message + " (error code: " + errorCode + ")");
super(STR."\{message} (error code: \{errorCode})");
code = errorCode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,14 @@ protected void waitForTransfer(Transfer transfer, int timeout, UsbDirection dire
if (hasTimedOut && transfer.resultCode() == 0) {
abortTransfers(direction, endpointNumber);
waitNoTimeout(transfer);
throw new UsbTimeoutException(getOperationDescription(direction, endpointNumber)
+ "aborted due to timeout");
throw new UsbTimeoutException(STR."\{getOperationDescription(direction, endpointNumber)}aborted due to timeout");
}
}

// test for error
if (transfer.resultCode() != 0) {
var operation = getOperationDescription(direction, endpointNumber);
throwOSException(transfer.resultCode(), operation + " failed");
throwOSException(transfer.resultCode(), STR."\{operation} failed");
}
}

Expand Down Expand Up @@ -450,7 +449,7 @@ public int hashCode() {

@Override
public String toString() {
return "VID: 0x" + String.format("%04x", vid) + ", PID: 0x" + String.format("%04x", pid) + ", " + "manufacturer: " + manufacturerString + ", product: " + productString + ", serial: " + serialString + ", ID: " + uniqueDeviceId;
return STR."VID: 0x\{String.format("%04x", vid)}, PID: 0x\{String.format("%04x", pid)}, manufacturer: \{manufacturerString}, product: \{productString}, serial: \{serialString}, ID: \{uniqueDeviceId}";
}

public record EndpointInfo(int interfaceNumber, int endpointNumber, byte endpointAddress, int packetSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class UsbInterfaceImpl implements UsbInterface {
public UsbInterfaceImpl(int number, List<UsbAlternateInterface> alternates) {
interfaceNumber = number;
alternateInterfaces = alternates;
currentAlternate = alternates.get(0);
currentAlternate = alternates.getFirst();
alternateInterfaces.sort(Comparator.comparingInt(UsbAlternateInterface::getNumber));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void largeCompositeDescriptor_canBeParsed() {
softly.assertThat(intf.getNumber()).isEqualTo(0);
softly.assertThat(intf.getAlternates()).hasSize(1);
softly.assertThat(intf.getCurrentAlternate().getEndpoints()).hasSize(1);
softly.assertThat(intf.getCurrentAlternate().getEndpoints().get(0)).satisfies(endpoint -> {
softly.assertThat(intf.getCurrentAlternate().getEndpoints().getFirst()).satisfies(endpoint -> {
softly.assertThat(endpoint.getNumber()).isEqualTo(5);
softly.assertThat(endpoint.getDirection()).isEqualTo(UsbDirection.IN);
softly.assertThat(endpoint.getTransferType()).isEqualTo(UsbTransferType.INTERRUPT);
Expand All @@ -88,7 +88,7 @@ void largeCompositeDescriptor_canBeParsed() {
softly.assertThat(intf.getAlternates().get(1)).satisfies(alternate -> {
softly.assertThat(alternate.getNumber()).isEqualTo(1);
softly.assertThat(alternate.getEndpoints()).hasSize(1);
softly.assertThat(alternate.getEndpoints().get(0)).satisfies(endpoint -> {
softly.assertThat(alternate.getEndpoints().getFirst()).satisfies(endpoint -> {
softly.assertThat(endpoint.getNumber()).isEqualTo(1);
softly.assertThat(endpoint.getDirection()).isEqualTo(UsbDirection.IN);
softly.assertThat(endpoint.getTransferType()).isEqualTo(UsbTransferType.ISOCHRONOUS);
Expand All @@ -107,7 +107,7 @@ void largeCompositeDescriptor_canBeParsed() {
softly.assertThat(intf.getAlternates().get(1)).satisfies(alternate -> {
softly.assertThat(alternate.getNumber()).isEqualTo(1);
softly.assertThat(alternate.getEndpoints()).hasSize(1);
softly.assertThat(alternate.getEndpoints().get(0)).satisfies(endpoint -> {
softly.assertThat(alternate.getEndpoints().getFirst()).satisfies(endpoint -> {
softly.assertThat(endpoint.getNumber()).isEqualTo(2);
softly.assertThat(endpoint.getDirection()).isEqualTo(UsbDirection.IN);
softly.assertThat(endpoint.getTransferType()).isEqualTo(UsbTransferType.ISOCHRONOUS);
Expand All @@ -119,10 +119,10 @@ void largeCompositeDescriptor_canBeParsed() {
softly.assertThat(configuration.interfaces().get(3)).satisfies(intf -> {
softly.assertThat(intf.getNumber()).isEqualTo(3);
softly.assertThat(intf.getAlternates()).hasSize(1);
softly.assertThat(intf.getAlternates().get(0)).satisfies(alternate -> {
softly.assertThat(intf.getAlternates().getFirst()).satisfies(alternate -> {
softly.assertThat(alternate.getNumber()).isEqualTo(0);
softly.assertThat(alternate.getEndpoints()).hasSize(1);
softly.assertThat(alternate.getEndpoints().get(0)).satisfies(endpoint -> {
softly.assertThat(alternate.getEndpoints().getFirst()).satisfies(endpoint -> {
softly.assertThat(endpoint.getNumber()).isEqualTo(4);
softly.assertThat(endpoint.getDirection()).isEqualTo(UsbDirection.IN);
softly.assertThat(endpoint.getTransferType()).isEqualTo(UsbTransferType.INTERRUPT);
Expand Down Expand Up @@ -164,7 +164,7 @@ void compositeTestDeviceDescriptor_canBeParsed() {
softly.assertThat(intf.getNumber()).isEqualTo(0);
softly.assertThat(intf.getAlternates()).hasSize(1);
softly.assertThat(intf.getCurrentAlternate().getEndpoints()).hasSize(1);
softly.assertThat(intf.getCurrentAlternate().getEndpoints().get(0)).satisfies(endpoint -> {
softly.assertThat(intf.getCurrentAlternate().getEndpoints().getFirst()).satisfies(endpoint -> {
softly.assertThat(endpoint.getNumber()).isEqualTo(3);
softly.assertThat(endpoint.getDirection()).isEqualTo(UsbDirection.IN);
softly.assertThat(endpoint.getTransferType()).isEqualTo(UsbTransferType.INTERRUPT);
Expand All @@ -175,7 +175,7 @@ void compositeTestDeviceDescriptor_canBeParsed() {
softly.assertThat(configuration.interfaces().get(1)).satisfies(intf -> {
softly.assertThat(intf.getNumber()).isEqualTo(1);
softly.assertThat(intf.getAlternates()).hasSize(1);
softly.assertThat(intf.getAlternates().get(0)).satisfies(alternate -> {
softly.assertThat(intf.getAlternates().getFirst()).satisfies(alternate -> {
softly.assertThat(alternate.getNumber()).isEqualTo(0);
softly.assertThat(alternate.getEndpoints()).hasSize(2);
softly.assertThat(alternate.getEndpoints().get(0)).satisfies(endpoint -> {
Expand All @@ -195,7 +195,7 @@ void compositeTestDeviceDescriptor_canBeParsed() {
softly.assertThat(configuration.interfaces().get(2)).satisfies(intf -> {
softly.assertThat(intf.getNumber()).isEqualTo(2);
softly.assertThat(intf.getAlternates()).hasSize(1);
softly.assertThat(intf.getAlternates().get(0)).satisfies(alternate -> {
softly.assertThat(intf.getAlternates().getFirst()).satisfies(alternate -> {
softly.assertThat(alternate.getNumber()).isEqualTo(0);
softly.assertThat(alternate.getEndpoints()).isEmpty();
});
Expand All @@ -205,7 +205,7 @@ void compositeTestDeviceDescriptor_canBeParsed() {
softly.assertThat(configuration.interfaces().get(3)).satisfies(intf -> {
softly.assertThat(intf.getNumber()).isEqualTo(3);
softly.assertThat(intf.getAlternates()).hasSize(1);
softly.assertThat(intf.getAlternates().get(0)).satisfies(alternate -> {
softly.assertThat(intf.getAlternates().getFirst()).satisfies(alternate -> {
softly.assertThat(alternate.getNumber()).isEqualTo(0);
softly.assertThat(alternate.getEndpoints()).hasSize(2);
softly.assertThat(alternate.getEndpoints().get(0)).satisfies(endpoint -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void alternateInterfaceDescriptor_isCorrect() {
var altIntf = intf.getCurrentAlternate();
assertNotNull(intf.getAlternates());
assertEquals(isLoopbackDevice() ? 2 : 1, intf.getAlternates().size());
assertSame(intf.getAlternates().get(0), altIntf);
assertSame(intf.getAlternates().getFirst(), altIntf);
assertEquals(0, altIntf.getNumber());

assertEquals(0xff, altIntf.getClassCode());
Expand All @@ -92,7 +92,7 @@ void endpointDescriptors_areCorrect() {
assertNotNull(altIntf.getEndpoints());
assertEquals(isLoopbackDevice() ? 4 : 2, altIntf.getEndpoints().size());

var endpoint = altIntf.getEndpoints().get(0);
var endpoint = altIntf.getEndpoints().getFirst();
assertEquals(1, endpoint.getNumber());
assertEquals(UsbDirection.OUT, endpoint.getDirection());
assertEquals(UsbTransferType.BULK, endpoint.getTransferType());
Expand Down Expand Up @@ -121,7 +121,7 @@ void endpointDescriptors_areCorrect() {
altIntf = testDevice.getInterfaces().get(interfaceNumber).getAlternates().get(1);
assertEquals(2, altIntf.getEndpoints().size());

endpoint = altIntf.getEndpoints().get(0);
endpoint = altIntf.getEndpoints().getFirst();
assertEquals(1, endpoint.getNumber());
assertEquals(UsbDirection.OUT, endpoint.getDirection());
assertEquals(UsbTransferType.BULK, endpoint.getTransferType());
Expand All @@ -137,7 +137,8 @@ void endpointDescriptors_areCorrect() {

@Test
void invalidEndpoint_shouldThrow() {
assertThrows(UsbException.class, () -> testDevice.getEndpoint(UsbDirection.IN, 1));
int nonExistentInEndpoint = isLoopbackDevice() ? 1 : 4;
assertThrows(UsbException.class, () -> testDevice.getEndpoint(UsbDirection.IN, nonExistentInEndpoint));
assertThrows(UsbException.class, () -> testDevice.getEndpoint(UsbDirection.OUT, 4));
assertThrows(UsbException.class, () -> testDevice.getEndpoint(UsbDirection.IN, 0));
assertThrows(UsbException.class, () -> testDevice.getEndpoint(UsbDirection.OUT, 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public class MonitorDevices {

public static void main(String[] args) throws IOException {
Usb.setOnDeviceConnected((device) -> {
System.out.println("Connected: " + device.toString());
System.out.println(STR."Connected: \{device.toString()}");
talkToTestDevice(device);
});
Usb.setOnDeviceDisconnected((device) -> System.out.println("Disconnected: " + device.toString()));
Usb.setOnDeviceDisconnected((device) -> System.out.println(STR."Disconnected: \{device.toString()}"));

for (var device : Usb.getDevices()) {
System.out.println("Present: " + device.toString());
System.out.println(STR."Present: \{device.toString()}");
talkToTestDevice(device);
}
System.out.println("Monitoring...");
Expand Down

0 comments on commit e7a0e87

Please sign in to comment.