// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.config.provision;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * Models an immutable list of network port allocations
 * @author arnej
 */
public class NetworkPorts {

    public static class Allocation {
        public final int port;
        public final String serviceType;
        public final String configId;
        public final String portSuffix;

        public Allocation(int port, String serviceType, String configId, String portSuffix) {
            this.port = port;
            this.serviceType = serviceType;
            this.configId = configId;
            this.portSuffix = portSuffix;
        }
        public String key() {
            StringBuilder buf = new StringBuilder();
            buf.append("t=").append(serviceType);
            buf.append(" cfg=").append(configId);
            buf.append(" suf=").append(portSuffix);
            return buf.toString();
        }
        public String toString() {
            StringBuilder buf = new StringBuilder();
            buf.append("[port=").append(port);
            buf.append(" serviceType=").append(serviceType);
            buf.append(" configId=").append(configId);
            buf.append(" suffix=").append(portSuffix);
            buf.append("]");
            return buf.toString();
        }
    }

    private final List<Allocation> allocations;

    public NetworkPorts(Collection<Allocation> allocations) {
        this.allocations = new ArrayList<>(allocations.size());
        this.allocations.addAll(allocations);
    }

    public Collection<Allocation> allocations() {
        return Collections.unmodifiableList(this.allocations);
    }
}

@@ -148,36 +148,8 @@
 n;%0A%0A
-import java.util.ArrayList;%0A
 impo
@@ -194,69 +194,12 @@
 til.
-Collections;%0Aimport java.util.List;%0Aimport java.util.Optional
+List
 ;%0A%0A/
@@ -1562,76 +1562,19 @@
 s = 
-new ArrayList%3C%3E(allocations.size());%0A        this.allocations.addAll
+List.copyOf
 (all
@@ -1660,37 +1660,8 @@
 urn 
-Collections.unmodifiableList(
 this
@@ -1664,27 +1664,26 @@
 this.allocations
-)
 ;%0A    %7D%0A%7D%0A

simplify list copy