aboutsummaryrefslogtreecommitdiffstats
path: root/security-utils/src/main/java/com/yahoo/security/tls/policy/AuthorizedPeers.java
blob: 66621224906e010d3b88c241f7b4aaa2b26e4201 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.security.tls.policy;

import java.util.Collections;
import java.util.Objects;
import java.util.Set;

/**
 * @author bjorncs
 */
public class AuthorizedPeers {

    private final Set<PeerPolicy> peerPolicies;

    public AuthorizedPeers(Set<PeerPolicy> peerPolicies) {
        this.peerPolicies = verifyPeerPolicies(peerPolicies);
    }

    private Set<PeerPolicy> verifyPeerPolicies(Set<PeerPolicy> peerPolicies) {
        long distinctNames = peerPolicies.stream()
                .map(PeerPolicy::policyName)
                .distinct()
                .count();
        if (distinctNames != peerPolicies.size()) {
            throw new IllegalArgumentException("'authorized-peers' contains entries with duplicate names");
        }
        return Collections.unmodifiableSet(peerPolicies);
    }

    public Set<PeerPolicy> peerPolicies() {
        return peerPolicies;
    }

    @Override
    public String toString() {
        return "AuthorizedPeers{" +
                "peerPolicies=" + peerPolicies +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AuthorizedPeers that = (AuthorizedPeers) o;
        return Objects.equals(peerPolicies, that.peerPolicies);
    }

    @Override
    public int hashCode() {
        return Objects.hash(peerPolicies);
    }
}