aboutsummaryrefslogtreecommitdiffstats
path: root/config-provisioning/src/main/java/com/yahoo/config/provision/NetworkPorts.java
blob: 3043958cf8a9a86e2050ca49985d9213096e8b99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.config.provision;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 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 = Objects.requireNonNull(serviceType, "servceType cannot be null");
            this.configId = Objects.requireNonNull(configId, "configId cannot be null");
            this.portSuffix = Objects.requireNonNull(portSuffix, "portSuffix cannot be null");
        }

        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();
        }

        @Override
        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();
        }

        @Override
        public int hashCode() {
            return Objects.hash(port, serviceType, configId, portSuffix);
        }

        @Override
        public boolean equals(Object o) {
            if (o == this) return true;
            if ( ! (o instanceof Allocation)) return false;
            Allocation other = (Allocation)o;
            if (other.port != this.port) return false;
            if ( ! other.serviceType.equals(this.serviceType)) return false;
            if ( ! other.configId.equals(this.configId)) return false;
            if ( ! other.portSuffix.equals(this.portSuffix)) return false;
            return true;
        }

    }

    private final List<Allocation> allocations; // immutable list

    public NetworkPorts(Collection<Allocation> allocations) {
        this.allocations = List.copyOf(allocations);
    }

    /** Returns a read only collection of the port allocations of this */
    public Collection<Allocation> allocations() {
        return this.allocations;
    }

    public int size() { return allocations.size(); }

    @Override
    public int hashCode() { return allocations.hashCode(); }

    @Override
    public boolean equals(Object other) {
        if (other == this) return true;
        if ( ! (other instanceof NetworkPorts)) return false;
        return ((NetworkPorts)other).allocations.equals(this.allocations);
    }

}