1
2
3
4
5 package oshi.hardware.common;
6
7 import java.util.Collections;
8 import java.util.List;
9 import java.util.Locale;
10
11 import oshi.annotation.concurrent.Immutable;
12 import oshi.hardware.UsbDevice;
13
14
15
16
17 @Immutable
18 public abstract class AbstractUsbDevice implements UsbDevice {
19
20 private final String name;
21 private final String vendor;
22 private final String vendorId;
23 private final String productId;
24 private final String serialNumber;
25 private final String uniqueDeviceId;
26 private final List<UsbDevice> connectedDevices;
27
28 protected AbstractUsbDevice(String name, String vendor, String vendorId, String productId, String serialNumber,
29 String uniqueDeviceId, List<UsbDevice> connectedDevices) {
30 this.name = name;
31 this.vendor = vendor;
32 this.vendorId = vendorId;
33 this.productId = productId;
34 this.serialNumber = serialNumber;
35 this.uniqueDeviceId = uniqueDeviceId;
36 this.connectedDevices = Collections.unmodifiableList(connectedDevices);
37 }
38
39 @Override
40 public String getName() {
41 return this.name;
42 }
43
44 @Override
45 public String getVendor() {
46 return this.vendor;
47 }
48
49 @Override
50 public String getVendorId() {
51 return this.vendorId;
52 }
53
54 @Override
55 public String getProductId() {
56 return this.productId;
57 }
58
59 @Override
60 public String getSerialNumber() {
61 return this.serialNumber;
62 }
63
64 @Override
65 public String getUniqueDeviceId() {
66 return this.uniqueDeviceId;
67 }
68
69 @Override
70 public List<UsbDevice> getConnectedDevices() {
71 return this.connectedDevices;
72 }
73
74 @Override
75 public int compareTo(UsbDevice usb) {
76
77 return getName().compareTo(usb.getName());
78 }
79
80 @Override
81 public String toString() {
82 return indentUsb(this, 1);
83 }
84
85
86
87
88
89
90
91
92 private static String indentUsb(UsbDevice usbDevice, int indent) {
93 String indentFmt = indent > 4 ? String.format(Locale.ROOT, "%%%ds|-- ", indent - 4)
94 : String.format(Locale.ROOT, "%%%ds", indent);
95 StringBuilder sb = new StringBuilder(String.format(Locale.ROOT, indentFmt, ""));
96 sb.append(usbDevice.getName());
97 if (!usbDevice.getVendor().isEmpty()) {
98 sb.append(" (").append(usbDevice.getVendor()).append(')');
99 }
100 if (!usbDevice.getSerialNumber().isEmpty()) {
101 sb.append(" [s/n: ").append(usbDevice.getSerialNumber()).append(']');
102 }
103 for (UsbDevice connected : usbDevice.getConnectedDevices()) {
104 sb.append('\n').append(indentUsb(connected, indent + 4));
105 }
106 return sb.toString();
107 }
108 }