public static Set<Port> parse(List<String> ports) throws NumberFormatException {
    Set<Port> result = new HashSet<>();

    for (String port : ports) {
      Matcher matcher = portPattern.matcher(port);

      if (!matcher.matches()) {
        throw new NumberFormatException(
            "Invalid port configuration: '"
                + port
                + "'. Make sure the port is a single number or a range of two numbers separated "
                + "with a '-', with or without protocol specified (e.g. '<portNum>/tcp' or "
                + "'<portNum>/udp').");
      }

      // Parse protocol
      int min = Integer.parseInt(matcher.group(1));
      int max = min;
      if (!Strings.isNullOrEmpty(matcher.group(2))) {
        max = Integer.parseInt(matcher.group(2));
      }
      String protocol = matcher.group(3);

      // Error if configured as 'max-min' instead of 'min-max'
      if (min > max) {
        throw new NumberFormatException(
            "Invalid port range '" + port + "'; smaller number must come first.");
      }

      // Warn for possibly invalid port numbers
      if (min < 1 || max > 65535) {
        throw new NumberFormatException(
            "Port number '" + port + "' is out of usual range (1-65535).");
      }

      for (int portNumber = min; portNumber <= max; portNumber++) {
        result.add(Port.parseProtocol(portNumber, protocol));
      }
    }

    return result;
  }
Converts/validates a list of strings representing port ranges to an expanded list of {@link
Port}s.

<p>For example: ["1000", "2000-2002"] will expand to a list of {@link Port}s with the port
numbers [1000, 2000, 2001, 2002]

@param ports the list of port numbers/ranges, with an optional protocol separated by a '/'
    (defaults to TCP if missing).
@return the ports as a list of {@link Port}
@throws NumberFormatException if any of the ports are in an invalid format or out of range
jib-core/src/main/java/com/google/cloud/tools/jib/api/Ports.java