final ImmutableMap.Builder<String, Integer> allocation = ImmutableMap.builder();
// Allocate static ports
for (Map.Entry<String, PortMapping> entry : mappings.entrySet()) {
final String name = entry.getKey();
final PortMapping portMapping = entry.getValue();
final Integer externalPort = portMapping.getExternalPort();
// Skip dynamic ports
if (externalPort == null) {
continue;
}
// Verify that this port is not in use
if (used.contains(externalPort)) {
return null;
}
used.add(externalPort);
allocation.put(name, externalPort);
}
// Allocate dynamic ports
for (Map.Entry<String, PortMapping> entry : mappings.entrySet()) {
final String name = entry.getKey();
final PortMapping portMapping = entry.getValue();
final Integer externalPort = portMapping.getExternalPort();
// Skip static ports
if (externalPort != null) {
continue;
}