// Path: public-java/io/iamcontent-usb/src/main/java/com/iamcontent/io/usb/Usb.java
// public class Usb {
// 
//     public static EasyUsbDevice device(short vendorId, short productId) {
//         return first(devices(vendorId, productId));
//     }
// 
//     public static EasyUsbDevice device(Predicate<UsbDevice> shouldIncludeDevice) {
//         return first(devices(shouldIncludeDevice));
//     }
// 
//     public static List<EasyUsbDevice> devices(short vendorId, short productId) {
//     	return devices(deviceHasVendorIdAndProductId(vendorId, productId));
//     }
// 
//     public static List<EasyUsbDevice> devices(Predicate<UsbDevice> shouldIncludeDevice) {
//         final UsbDeviceFinder deviceFinder = usbDeviceFinder(shouldIncludeDevice);
//         deviceFinder.exploreRootUsbHub();
// 		return deviceFinder.getDevices();
//     }
// 
//     public static UsbHub rootUsbHub() {
//         try {
//             return usbServices().getRootUsbHub();
//         } catch (Exception e) {
//             throw new UsbRuntimeException(e);
//         }
//     }
// 
//     public static UsbServices usbServices() {
//         try {
//             return UsbHostManager.getUsbServices();
//         } catch (Exception e) {
//             throw new UsbRuntimeException(e);
//         }
//     }
// 
//     private static <T> T first(List<T> l) {
//         try {
//             return l.get(0);
//         } catch (IndexOutOfBoundsException e) {
//             throw new UsbRuntimeException("No suitable USB Device found.");
//         }
//     }
//     
//     private Usb() {
//     }
// }


import com.iamcontent.io.usb.Usb;
import java.util.function.Predicate;
import javax.usb.UsbDevice;
import javax.usb.UsbHub;

/**
  IAmContent Public Libraries.
  Copyright (C) 2015-2021 Greg Elderfield
  @author Greg Elderfield, support@jarchitect.co.uk
 
  This program is free software; you can redistribute it and/or modify it under the terms of the
  GNU General Public License as published by the Free Software Foundation; either version 2 of 
  the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along with this program;
  if not, write to the Free Software Foundation, Inc., 
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package com.iamcontent.io.usb.topology;




/**
 * An abstract class that recursively explores the USB devices in a USB topology, starting at 
 * a specific device, or the root USB hub.
 * @author Greg Elderfield
 */
public abstract class UsbTopologyExplorer {
	
	private final Predicate<UsbDevice> shouldVisit;

	/**
	 * Creates an instance.
	 * @param shouldVisit Indicates whether the {@link #visit(UsbDevice)} method should be invoked for a given device.
	 */
	public UsbTopologyExplorer(Predicate<UsbDevice> shouldVisit) {
		this.shouldVisit = shouldVisit;
	}

	/**
	 * Explore the USB topology, starting at the root USB hub. 
	 */
	public void exploreRootUsbHub() {
		explore(Usb.rootUsbHub());
	}
	
	/**
	 * Explore the USB topology, starting at the given device. 
	 */
	public void explore(UsbDevice usbDevice) {
        if (shouldVisit(usbDevice))
            visit(usbDevice);


// Path: public-java/io/iamcontent-usb/src/main/java/com/iamcontent/io/usb/Usb.java
// public class Usb {
// 
//     public static EasyUsbDevice device(short vendorId, short productId) {
//         return first(devices(vendorId, productId));
//     }
// 
//     public static EasyUsbDevice device(Predicate<UsbDevice> shouldIncludeDevice) {
//         return first(devices(shouldIncludeDevice));
//     }
// 
//     public static List<EasyUsbDevice> devices(short vendorId, short productId) {
//     	return devices(deviceHasVendorIdAndProductId(vendorId, productId));
//     }
// 
//     public static List<EasyUsbDevice> devices(Predicate<UsbDevice> shouldIncludeDevice) {
//         final UsbDeviceFinder deviceFinder = usbDeviceFinder(shouldIncludeDevice);
//         deviceFinder.exploreRootUsbHub();
// 		return deviceFinder.getDevices();
//     }
// 
//     public static UsbHub rootUsbHub() {
//         try {
//             return usbServices().getRootUsbHub();
//         } catch (Exception e) {
//             throw new UsbRuntimeException(e);
//         }
//     }
// 
//     public static UsbServices usbServices() {
//         try {
//             return UsbHostManager.getUsbServices();
//         } catch (Exception e) {
//             throw new UsbRuntimeException(e);
//         }
//     }
// 
//     private static <T> T first(List<T> l) {
//         try {
//             return l.get(0);
//         } catch (IndexOutOfBoundsException e) {
//             throw new UsbRuntimeException("No suitable USB Device found.");
//         }
//     }
//     
//     private Usb() {
//     }
// }


// Path: public-java/io/iamcontent-usb/src/main/java/com/iamcontent/io/usb/topology/UsbTopologyExplorer.java
import com.iamcontent.io.usb.Usb;
import java.util.function.Predicate;
import javax.usb.UsbDevice;
import javax.usb.UsbHub;

/**
  IAmContent Public Libraries.
  Copyright (C) 2015-2021 Greg Elderfield
  @author Greg Elderfield, support@jarchitect.co.uk
 
  This program is free software; you can redistribute it and/or modify it under the terms of the
  GNU General Public License as published by the Free Software Foundation; either version 2 of 
  the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along with this program;
  if not, write to the Free Software Foundation, Inc., 
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package com.iamcontent.io.usb.topology;




/**
 * An abstract class that recursively explores the USB devices in a USB topology, starting at 
 * a specific device, or the root USB hub.
 * @author Greg Elderfield
 */
public abstract class UsbTopologyExplorer {
	
	private final Predicate<UsbDevice> shouldVisit;

	/**
	 * Creates an instance.
	 * @param shouldVisit Indicates whether the {@link #visit(UsbDevice)} method should be invoked for a given device.
	 */
	public UsbTopologyExplorer(Predicate<UsbDevice> shouldVisit) {
		this.shouldVisit = shouldVisit;
	}

	/**
	 * Explore the USB topology, starting at the root USB hub. 
	 */
	public void exploreRootUsbHub() {
		explore(Usb.rootUsbHub());
	}
	
	/**
	 * Explore the USB topology, starting at the given device. 
	 */
	public void explore(UsbDevice usbDevice) {
        if (shouldVisit(usbDevice))
            visit(usbDevice);


        if (usbDevice.isUsbHub())
