aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/LeafGroups.java
blob: e644cfde0da7e0c4c7eb9295b599f87be5fc3403 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core;

import com.yahoo.vdslib.distribution.Group;

import java.util.ArrayList;
import java.util.List;

public class LeafGroups {

    /**
     * Return a list of all groups that do not themselves have any child groups,
     * i.e. only the groups that contain nodes.
     *
     * The output order is not defined.
     */
    public static List<Group> enumerateFrom(Group root) {
        List<Group> leaves = new ArrayList<>();
        visitNode(root, leaves);
        return leaves;
    }

    private static void visitNode(Group node, List<Group> leaves) {
        if (node.isLeafGroup()) {
            leaves.add(node);
        } else {
            node.getSubgroups().forEach((idx, g) -> visitNode(g, leaves));
        }
    }

}