package org.studiorailgun.knowledge.query.filter;

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

import org.studiorailgun.knowledge.Node;

/**
 * Node query filters based around names
 */
public class NameQueryFilter {
    
    /**
     * Filters a set of nodes to a singleton based on a name
     */
    public static Node withNodeName(List<Node> nodes, String name){
        List<Node> filtered = nodes.stream().filter(node -> node.getName().equals(name)).collect(Collectors.toList());
        if(filtered.size() != 1){
            String message = "TODO: handle ambiguous case\n" +
            "query: " + name + "\n" +
            filtered.size();
            if(filtered.size() > 0){
                message = message + "found:\n";
                for(Node node : filtered){
                    message = message + "\n" + node.getName();
                }
            }
            throw new UnsupportedOperationException(message);
        }
        return filtered.get(0);
    }

}
