From d1b8e25756b370df85cf5b45bfccfdd88bcf3576 Mon Sep 17 00:00:00 2001 From: Klaus Reimer Date: Fri, 12 Apr 2013 22:02:29 +0200 Subject: [PATCH] Cleanup inheritance --- .../de/ailis/usb4java/Usb4JavaDevice.java | 70 +++++++------------ .../java/de/ailis/usb4java/Usb4JavaHub.java | 14 ++-- .../de/ailis/usb4java/Usb4JavaNonHub.java | 65 +++++++++++++++++ .../de/ailis/usb4java/UsbDeviceManager.java | 8 ++- 4 files changed, 102 insertions(+), 55 deletions(-) create mode 100644 src/main/java/de/ailis/usb4java/Usb4JavaNonHub.java diff --git a/src/main/java/de/ailis/usb4java/Usb4JavaDevice.java b/src/main/java/de/ailis/usb4java/Usb4JavaDevice.java index 58070d3..06d15f2 100644 --- a/src/main/java/de/ailis/usb4java/Usb4JavaDevice.java +++ b/src/main/java/de/ailis/usb4java/Usb4JavaDevice.java @@ -41,7 +41,7 @@ import de.ailis.usb4java.support.UsbDeviceListenerList; * * @author Klaus Reimer (k@ailis.de) */ -public class Usb4JavaDevice implements UsbDevice +abstract class Usb4JavaDevice implements UsbDevice { /** The USB device manager. */ private final UsbDeviceManager manager; @@ -157,28 +157,12 @@ public class Usb4JavaDevice implements UsbDevice LibUSB.freeConfigDescriptor(configDescriptor); } - @Override - public int hashCode() - { - return this.id.hashCode(); - } - - @Override - public boolean equals(final Object obj) - { - if (this == obj) return true; - if (obj == null) return false; - if (getClass() != obj.getClass()) return false; - final Usb4JavaDevice other = (Usb4JavaDevice) obj; - return this.id.equals(other.id); - } - /** * Returns the device id. * * @return The device id. */ - public DeviceId getId() + public final DeviceId getId() { return this.id; } @@ -188,7 +172,7 @@ public class Usb4JavaDevice implements UsbDevice * * @return The parent device id or null of there is no parent. */ - public DeviceId getParentId() + public final DeviceId getParentId() { return this.parentId; } @@ -249,7 +233,7 @@ public class Usb4JavaDevice implements UsbDevice } @Override - public UsbPort getParentUsbPort() + public final UsbPort getParentUsbPort() { checkConnected(); return this.port; @@ -305,7 +289,7 @@ public class Usb4JavaDevice implements UsbDevice } @Override - public String getManufacturerString() throws UsbException, + public final String getManufacturerString() throws UsbException, UnsupportedEncodingException { checkConnected(); @@ -315,7 +299,7 @@ public class Usb4JavaDevice implements UsbDevice } @Override - public String getSerialNumberString() throws UsbException, + public final String getSerialNumberString() throws UsbException, UnsupportedEncodingException { checkConnected(); @@ -325,7 +309,7 @@ public class Usb4JavaDevice implements UsbDevice } @Override - public String getProductString() throws UsbException, + public final String getProductString() throws UsbException, UnsupportedEncodingException { checkConnected(); @@ -335,7 +319,7 @@ public class Usb4JavaDevice implements UsbDevice } @Override - public Object getSpeed() + public final Object getSpeed() { switch (this.speed) { @@ -349,25 +333,25 @@ public class Usb4JavaDevice implements UsbDevice } @Override - public List getUsbConfigurations() + public final List getUsbConfigurations() { return this.configurations; } @Override - public Usb4JavaConfiguration getUsbConfiguration(final byte number) + public final Usb4JavaConfiguration getUsbConfiguration(final byte number) { return this.configMapping.get(number); } @Override - public boolean containsUsbConfiguration(final byte number) + public final boolean containsUsbConfiguration(final byte number) { return this.configMapping.containsKey(number); } @Override - public byte getActiveUsbConfigurationNumber() + public final byte getActiveUsbConfigurationNumber() { return this.activeConfigurationNumber; } @@ -482,25 +466,25 @@ public class Usb4JavaDevice implements UsbDevice } @Override - public Usb4JavaConfiguration getActiveUsbConfiguration() + public final Usb4JavaConfiguration getActiveUsbConfiguration() { return getUsbConfiguration(getActiveUsbConfigurationNumber()); } @Override - public boolean isConfigured() + public final boolean isConfigured() { return getActiveUsbConfigurationNumber() != 0; } @Override - public UsbDeviceDescriptor getUsbDeviceDescriptor() + public final UsbDeviceDescriptor getUsbDeviceDescriptor() { return this.id.getDeviceDescriptor(); } @Override - public UsbStringDescriptor getUsbStringDescriptor(final byte index) + public final UsbStringDescriptor getUsbStringDescriptor(final byte index) throws UsbException { checkConnected(); @@ -517,7 +501,7 @@ public class Usb4JavaDevice implements UsbDevice } @Override - public String getString(final byte index) throws UsbException, + public final String getString(final byte index) throws UsbException, UnsupportedEncodingException { return getUsbStringDescriptor(index).getString(); @@ -550,7 +534,7 @@ public class Usb4JavaDevice implements UsbDevice } @Override - public void syncSubmit(final UsbControlIrp irp) throws UsbException + public final void syncSubmit(final UsbControlIrp irp) throws UsbException { if (irp == null) throw new IllegalArgumentException("irp must not be null"); @@ -561,7 +545,7 @@ public class Usb4JavaDevice implements UsbDevice } @Override - public void asyncSubmit(final UsbControlIrp irp) + public final void asyncSubmit(final UsbControlIrp irp) { if (irp == null) throw new IllegalArgumentException("irp must not be null"); @@ -570,7 +554,7 @@ public class Usb4JavaDevice implements UsbDevice } @Override - public void syncSubmit(final List list) throws UsbException + public final void syncSubmit(final List list) throws UsbException { if (list == null) throw new IllegalArgumentException("list must not be null"); @@ -585,7 +569,7 @@ public class Usb4JavaDevice implements UsbDevice } @Override - public void asyncSubmit(final List list) + public final void asyncSubmit(final List list) { if (list == null) throw new IllegalArgumentException("list must not be null"); @@ -600,7 +584,7 @@ public class Usb4JavaDevice implements UsbDevice } @Override - public UsbControlIrp createUsbControlIrp(final byte bmRequestType, + public final UsbControlIrp createUsbControlIrp(final byte bmRequestType, final byte bRequest, final short wValue, final short wIndex) { return new DefaultUsbControlIrp(bmRequestType, bRequest, wValue, @@ -608,13 +592,13 @@ public class Usb4JavaDevice implements UsbDevice } @Override - public void addUsbDeviceListener(final UsbDeviceListener listener) + public final void addUsbDeviceListener(final UsbDeviceListener listener) { this.listeners.add(listener); } @Override - public void removeUsbDeviceListener(final UsbDeviceListener listener) + public final void removeUsbDeviceListener(final UsbDeviceListener listener) { this.listeners.remove(listener); } @@ -624,10 +608,4 @@ public class Usb4JavaDevice implements UsbDevice { return this.id.toString(); } - - @Override - public boolean isUsbHub() - { - return false; - } } diff --git a/src/main/java/de/ailis/usb4java/Usb4JavaHub.java b/src/main/java/de/ailis/usb4java/Usb4JavaHub.java index 9906206..b074a24 100644 --- a/src/main/java/de/ailis/usb4java/Usb4JavaHub.java +++ b/src/main/java/de/ailis/usb4java/Usb4JavaHub.java @@ -24,7 +24,7 @@ public final class Usb4JavaHub extends Usb4JavaDevice implements UsbHub, private final Usb4JavaPorts ports = new Usb4JavaPorts(this); /** - * Constructor. + * Constructs a new USB hub device. * * @param manager * The USB device manager which is responsible for this device. @@ -89,12 +89,6 @@ public final class Usb4JavaHub extends Usb4JavaDevice implements UsbHub, { this.ports.disconnectUsbDevice(device); } - - @Override - public boolean isUsbHub() - { - return true; - } @Override public int hashCode() @@ -111,4 +105,10 @@ public final class Usb4JavaHub extends Usb4JavaDevice implements UsbHub, final Usb4JavaDevice other = (Usb4JavaDevice) obj; return getId().equals(other.getId()); } + + @Override + public boolean isUsbHub() + { + return true; + } } diff --git a/src/main/java/de/ailis/usb4java/Usb4JavaNonHub.java b/src/main/java/de/ailis/usb4java/Usb4JavaNonHub.java new file mode 100644 index 0000000..3a8bbff --- /dev/null +++ b/src/main/java/de/ailis/usb4java/Usb4JavaNonHub.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2013 Klaus Reimer + * See LICENSE.txt for licensing information. + */ + +package de.ailis.usb4java; + +import de.ailis.usb4java.libusb.Device; +import de.ailis.usb4java.libusb.LibUsbException; + +/** + * A non-hub USB device. + * + * @author Klaus Reimer (k@ailis.de) + */ +final class Usb4JavaNonHub extends Usb4JavaDevice +{ + /** + * Constructs a new non-hub USB device. + * + * @param manager + * The USB device manager which is responsible for this device. + * @param id + * The device id. Must not be null. + * @param parentId + * The parent device id. May be null if this device has no parent + * (Because it is a root device). + * @param speed + * The device speed. + * @param device + * The libusb device. This reference is only valid during the + * constructor execution, so don't store it in a property or + * something like that. + * @throws LibUsbException + * When device configuration could not be read. + */ + Usb4JavaNonHub(final UsbDeviceManager manager, final DeviceId id, + final DeviceId parentId, final int speed, final Device device) + throws LibUsbException + { + super(manager, id, parentId, speed, device); + } + + @Override + public final int hashCode() + { + return getId().hashCode(); + } + + @Override + public final boolean equals(final Object obj) + { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + final Usb4JavaNonHub other = (Usb4JavaNonHub) obj; + return getId().equals(other.getId()); + } + + @Override + public final boolean isUsbHub() + { + return false; + } +} diff --git a/src/main/java/de/ailis/usb4java/UsbDeviceManager.java b/src/main/java/de/ailis/usb4java/UsbDeviceManager.java index c6325f8..b3b40fa 100644 --- a/src/main/java/de/ailis/usb4java/UsbDeviceManager.java +++ b/src/main/java/de/ailis/usb4java/UsbDeviceManager.java @@ -137,11 +137,15 @@ public final class UsbDeviceManager final boolean isHub = id.getDeviceDescriptor() .bDeviceClass() == LibUSB.CLASS_HUB; if (isHub) + { device = new Usb4JavaHub(this, id, parentId, speed, libUsbDevice); + } else - device = new Usb4JavaDevice(this, id, parentId, - speed, libUsbDevice); + { + device = new Usb4JavaNonHub(this, id, + parentId, speed, libUsbDevice); + } } found.add(device); }