content: /*
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
   ~                                                                               ~
   ~ The MIT License (MIT)                                                         ~
   ~                                                                               ~
   ~ Copyright (c) 2015-2025 miaixz.org OSHI and other contributors.               ~
   ~                                                                               ~
   ~ Permission is hereby granted, free of charge, to any person obtaining a copy  ~
   ~ of this software and associated documentation files (the "Software"), to deal ~
   ~ in the Software without restriction, including without limitation the rights  ~
   ~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell     ~
   ~ copies of the Software, and to permit persons to whom the Software is         ~
   ~ furnished to do so, subject to the following conditions:                      ~
   ~                                                                               ~
   ~ The above copyright notice and this permission notice shall be included in    ~
   ~ all copies or substantial portions of the Software.                           ~
   ~                                                                               ~
   ~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    ~
   ~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,      ~
   ~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE   ~
   ~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER        ~
   ~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ~
   ~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN     ~
   ~ THE SOFTWARE.                                                                 ~
   ~                                                                               ~
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  */
  package org.miaixz.bus.health.builtin.hardware.common;
  
  import java.util.Collections;
  import java.util.List;
  import java.util.Locale;
  
  import org.miaixz.bus.core.lang.Normal;
  import org.miaixz.bus.core.lang.Symbol;
  import org.miaixz.bus.core.lang.annotation.Immutable;
  import org.miaixz.bus.health.builtin.hardware.UsbDevice;
  
  /**
   * A USB device
   *
   * @author Kimi Liu
   * @since Java 17+
   */
  @Immutable
  public abstract class AbstractUsbDevice implements UsbDevice {
  
      private final String name;
      private final String vendor;
      private final String vendorId;
      private final String productId;
      private final String serialNumber;
      private final String uniqueDeviceId;
      private final List<UsbDevice> connectedDevices;
  
      protected AbstractUsbDevice(String name, String vendor, String vendorId, String productId, String serialNumber,
              String uniqueDeviceId, List<UsbDevice> connectedDevices) {
          this.name = name;
          this.vendor = vendor;
          this.vendorId = vendorId;
          this.productId = productId;
          this.serialNumber = serialNumber;
          this.uniqueDeviceId = uniqueDeviceId;
          this.connectedDevices = Collections.unmodifiableList(connectedDevices);
      }
  
      /**
       * Helper method for indenting chained USB devices
       *
       * @param usbDevice A USB device to print
       * @param indent    number of spaces to indent
       * @return The device toString, indented
       */
      private static String indentUsb(UsbDevice usbDevice, int indent) {
          String indentFmt = indent > 4 ? String.format(Locale.ROOT, "%%%ds|-- ", indent - 4)
                  : String.format(Locale.ROOT, "%%%ds", indent);
          StringBuilder sb = new StringBuilder(String.format(Locale.ROOT, indentFmt, Normal.EMPTY));
          sb.append(usbDevice.getName());
          if (!usbDevice.getVendor().isEmpty()) {
              sb.append(" (").append(usbDevice.getVendor()).append(Symbol.C_PARENTHESE_RIGHT);
          }
          if (!usbDevice.getSerialNumber().isEmpty()) {
              sb.append(" [s/n: ").append(usbDevice.getSerialNumber()).append(']');
          }
          for (UsbDevice connected : usbDevice.getConnectedDevices()) {
              sb.append('\n').append(indentUsb(connected, indent + 4));
          }
          return sb.toString();
      }
  
      @Override
      public String getName() {
          return this.name;
      }
  
      @Override
      public String getVendor() {
          return this.vendor;
      }
  
      @Override
      public String getVendorId() {
          return this.vendorId;
      }
  
      @Override
      public String getProductId() {
          return this.productId;
      }
  
      @Override
      public String getSerialNumber() {
          return this.serialNumber;
      }
  
      @Override
      public String getUniqueDeviceId() {
          return this.uniqueDeviceId;
      }
  
      @Override
      public List<UsbDevice> getConnectedDevices() {
          return this.connectedDevices;
      }
  
      @Override
      public String toString() {
          return indentUsb(this, 1);
      }
  
      @Override
      public int compareTo(UsbDevice usb) {
          // Naturally sort by device name
          return getName().compareTo(usb.getName());
      }
  
  }
  