// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision;

import com.google.common.collect.ImmutableList;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.NodeType;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.collectingAndThen;

/**
 * A filterable node list
 *
 * @author bratseth
 * @author mpolden
 */
public class NodeList {

    private final List<Node> nodes;

    public NodeList(List<Node> nodes) {
        this.nodes = ImmutableList.copyOf(nodes);
    }

    /** Returns the subset of nodes which are retired */
    public NodeList retired() {
        return new NodeList(nodes.stream().filter(node -> node.allocation().get().membership().retired()).collect(Collectors.toList()));
    }

    /** Returns the subset of nodes which are not retired */
    public NodeList nonretired() {
        return new NodeList(nodes.stream().filter(node -> ! node.allocation().get().membership().retired()).collect(Collectors.toList()));
    }

    /** Returns the subset of nodes of the given flavor */
    public NodeList flavor(String flavor) {
        return new NodeList(nodes.stream().filter(node -> node.flavor().name().equals(flavor)).collect(Collectors.toList()));
    }

    /** Returns the subset of nodes which does not have the given flavor */
    public NodeList notFlavor(String flavor) {
        return new NodeList(nodes.stream().filter(node ->  ! node.flavor().name().equals(flavor)).collect(Collectors.toList()));
    }

    /** Returns the subset of nodes assigned to the given cluster type */
    public NodeList type(ClusterSpec.Type type) {
        return new NodeList(nodes.stream().filter(node -> node.allocation().get().membership().cluster().type().equals(type)).collect(Collectors.toList()));
    }

    /** Returns the subset of nodes owned by the given application */
    public NodeList owner(ApplicationId application) {
        return nodes.stream()
                    .filter(node -> node.allocation().map(a -> a.owner().equals(application)).orElse(false))
                    .collect(collectingAndThen(Collectors.toList(), NodeList::new));
    }

    /** Returns the subset of nodes matching the given node type */
    public NodeList nodeType(NodeType nodeType) {
        return nodes.stream()
                    .filter(node -> node.type() == nodeType)
                    .collect(collectingAndThen(Collectors.toList(), NodeList::new));
    }

    /** Returns the parent nodes of the given child nodes */
    public NodeList parentsOf(Collection<Node> children) {
        return children.stream()
                       .map(Node::parentHostname)
                       .filter(Optional::isPresent)
                       .map(Optional::get)
                       .map(hostName -> nodes.stream().filter(node -> node.hostname().equals(hostName)).findFirst())
                       .filter(Optional::isPresent)
                       .map(Optional::get)
                       .collect(collectingAndThen(Collectors.toList(), NodeList::new));
    }

    /** Returns the subset of nodes that are parents */
    public NodeList parents() {
        return nodes.stream()
                    .filter(n -> !n.parentHostname().isPresent())
                    .collect(collectingAndThen(Collectors.toList(), NodeList::new));
    }

    /** Returns the child nodes of the given parent node */
    public NodeList childrenOf(String hostname) {
        return nodes.stream()
                    .filter(n -> n.parentHostname()
                                  .map(hostName -> hostName.equals(hostname))
                                  .orElse(false))
                    .collect(collectingAndThen(Collectors.toList(), NodeList::new));
    }

    public NodeList childrenOf(Node parent) {
        return childrenOf(parent.hostname());
    }

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

    /** Returns the immutable list of nodes in this */
    public List<Node> asList() { return nodes; }

}

@@ -423,16 +423,53 @@
 tional;%0A
+import java.util.function.Predicate;%0A
 import j
@@ -902,36 +902,8 @@
 urn 
-new NodeList(nodes.stream().
 filt
@@ -964,38 +964,8 @@
 d())
-.collect(Collectors.toList()))
 ;%0A  
@@ -1084,36 +1084,8 @@
 urn 
-new NodeList(nodes.stream().
 filt
@@ -1148,38 +1148,8 @@
 d())
-.collect(Collectors.toList()))
 ;%0A  
@@ -1275,36 +1275,8 @@
 urn 
-new NodeList(nodes.stream().
 filt
@@ -1326,38 +1326,8 @@
 or))
-.collect(Collectors.toList()))
 ;%0A  
@@ -1473,36 +1473,8 @@
 urn 
-new NodeList(nodes.stream().
 filt
@@ -1527,38 +1527,8 @@
 or))
-.collect(Collectors.toList()))
 ;%0A  
@@ -1675,36 +1675,8 @@
 urn 
-new NodeList(nodes.stream().
 filt
@@ -1757,38 +1757,8 @@
 pe))
-.collect(Collectors.toList()))
 ;%0A  
@@ -1906,44 +1906,8 @@
 urn 
-nodes.stream()%0A                    .
 filt
@@ -1993,92 +1993,8 @@
 se))
-%0A                    .collect(collectingAndThen(Collectors.toList(), NodeList::new))
 ;%0A  
@@ -2135,52 +2135,167 @@
 urn 
-nodes.stream()
+filter(node -%3E node.type() == nodeType);
 %0A    
+%7D%0A%0A
     
-            .
+/** Returns the subset of nodes that are parents */%0A    public NodeList parents() %7B%0A        return 
 filter(n
 ode 
@@ -2294,121 +2294,339 @@
 er(n
-ode
  -%3E n
-ode.type() == nodeType)
+.parentHostname().isEmpty());
 %0A    
+%7D%0A%0A
     
-            .collect(collectingAndThen(Collectors.toList(), NodeList::new
+/** Returns the child nodes of the given parent node */%0A    public NodeList childrenOf(String hostname) %7B%0A        return filter(n -%3E n.parentHostname().map(hostname::equals).orElse(false));%0A    %7D%0A%0A    public NodeList childrenOf(Node parent) %7B%0A        return childrenOf(parent.hostname(
 ));%0A
@@ -2624,32 +2624,33 @@
 name());%0A    %7D%0A%0A
+%0A
     /** Returns 
@@ -3246,940 +3246,329 @@
     
-/** Returns the subset of nodes that are parents */%0A    public NodeList parents() %7B%0A        return nodes.stream()%0A                    .filter(n -%3E !n.parentHostname().isPresent())%0A                    .collect(collectingAndThen(Collectors.toList(), NodeList::new));%0A    %7D%0A%0A    /** Returns the child nodes of the given parent node */%0A    public NodeList childrenOf(String hostname) %7B%0A        return nodes.stream()%0A                    .filter(n -%3E n.parentHostname()%0A                                  .map(hostName -%3E hostName.equals(hostname))%0A                                  .orElse(false))%0A                    .collect(collectingAndThen(Collectors.toList(), NodeList::new));%0A    %7D%0A%0A    public NodeList childrenOf(Node parent) %7B%0A        return childrenOf(parent.hostname());%0A    %7D%0A%0A    public int size() %7B return nodes.size(); %7D%0A%0A    /** Returns the immutable list of nodes in this */%0A    public List%3CNode%3E asList() %7B return nodes;
+public int size() %7B return nodes.size(); %7D%0A%0A    /** Returns the immutable list of nodes in this */%0A    public List%3CNode%3E asList() %7B return nodes; %7D%0A%0A    private NodeList filter(Predicate%3CNode%3E predicate) %7B%0A        return nodes.stream().filter(predicate).collect(collectingAndThen(Collectors.toList(), NodeList::new));%0A   
  %7D%0A%0A

Simplify NodeList