aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/node/filter/NodeTypeFilter.java
blob: de091aebb683bae6cf279896e7a32b7eace6bafd (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
package com.yahoo.vespa.hosted.provision.node.filter;

import com.google.common.collect.ImmutableSet;
import com.yahoo.config.provision.HostFilter;
import com.yahoo.config.provision.NodeType;
import com.yahoo.text.StringUtilities;
import com.yahoo.vespa.hosted.provision.Node;

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

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

    private final Set<NodeType> types;
    
    protected NodeTypeFilter(Set<NodeType> types, NodeFilter next) {
        super(next);
        Objects.requireNonNull(types, "Node types cannot be null");
        this.types = ImmutableSet.copyOf(types);
    }

    @Override
    public boolean matches(Node node) {
        if (! types.isEmpty() && ! types.contains(node.type())) return false;
        return nextMatches(node);
    }

    /** Returns a copy of the given filter which only matches for the given type */
    public static NodeTypeFilter from(NodeType type, NodeFilter filter) {
        return new NodeTypeFilter(Collections.singleton(type), filter);
    }

    /** Returns a node filter which matches a comma or space-separated list of types */
    public static NodeTypeFilter from(String types, NodeFilter next) {
        return new NodeTypeFilter(StringUtilities.split(types).stream().map(NodeType::valueOf).collect(Collectors.toSet()), next);
    }

}