aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/node/filter/NodeTypeFilter.java
blob: 95436dcbf1077849bfc321d8c4d15a3480b3cf02 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.node.filter;

import com.yahoo.config.provision.NodeType;
import com.yahoo.text.StringUtilities;
import com.yahoo.vespa.hosted.provision.Node;

import java.util.EnumSet;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author bratseth
 */
public class NodeTypeFilter {

    private NodeTypeFilter() {}

    private static Predicate<Node> makePredicate(EnumSet<NodeType> types) {
        Objects.requireNonNull(types, "Node types cannot be null");
        if (types.isEmpty()) return node -> true;
        return node -> types.contains(node.type());
    }

    /** Returns a copy of the given filter which only matches for the given type */
    public static Predicate<Node> from(NodeType type) {
        return makePredicate(EnumSet.of(type));
    }

    /** Returns a node filter which matches a comma or space-separated list of types */
    public static Predicate<Node> from(String types) {
        return makePredicate(StringUtilities.split(types).stream()
                .map(NodeType::valueOf)
                .collect(Collectors.toCollection(() -> EnumSet.noneOf(NodeType.class))));
    }

}