package org.studiorailgun.knowledge.query.filter;

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

import org.studiorailgun.knowledge.Node;
import org.studiorailgun.knowledge.query.PossessionQuery;

/**
 * Query filters based on possession relationships
 */
public class PossessionQueryFilter {
    
    /**
     * Filters a set of nodes to a singleton based on a name
     */
    public static Node withPossessor(List<Node> nodes, Node possessor){
        List<Node> possessions = PossessionQuery.getPossessions(possessor);
        List<Node> filtered = nodes.stream().filter(node -> {
            return possessions.contains(node);
        }).collect(Collectors.toList());
        if(filtered.size() != 1){
            throw new UnsupportedOperationException("TODO: handle ambiguous case");
        }
        return filtered.get(0);
    }

}
