static PortMappingWithName parsePortMapping(final String portSpec) { final Matcher matcher = PATTERN.matcher(portSpec); if (!matcher.matches()) { throw new IllegalArgumentException("Bad port mapping: " + portSpec); } final String name = matcher.group("n"); final String ip = matcher.group("ip"); final int internal = Integer.parseInt(matcher.group("i")); final Integer external = nullOrInteger(matcher.group("e")); final String protocol = fromNullable(matcher.group("p")).or(TCP); return PortMappingWithName.create(name, PortMapping.builder() .ip(ip) .internalPort(internal) .externalPort(external) .protocol(protocol) .build()); }
static Map<String, PortMapping> parsePortMappings(final List<String> portSpecs) { final Map<String, PortMapping> explicitPorts = Maps.newHashMap(); for (final String spec : portSpecs) { final PortMappingWithName portMappingWithName = parsePortMapping(spec); final String name = portMappingWithName.name(); if (explicitPorts.containsKey(name)) { throw new IllegalArgumentException("Duplicate port mapping name: " + name); } explicitPorts.put(name, portMappingWithName.portMapping()); } return ImmutableMap.copyOf(explicitPorts); }
static PortMappingWithName create(final String name, final PortMapping portMapping) { return new PortMappingWithName(name, portMapping); }