001package org.biopax.paxtools.pattern.constraint;
002
003import org.biopax.paxtools.pattern.MappedConst;
004import org.biopax.paxtools.pattern.Match;
005import org.biopax.paxtools.model.BioPAXElement;
006
007import java.util.Collection;
008import java.util.HashSet;
009
010/**
011 * Used for getting logical AND of a set of constraints.
012 *
013 * @author Ozgun Babur
014 */
015public class AND extends OR
016{
017        /**
018         * Constructor with the mapped constraints.
019         * @param con mapped constraints
020         */
021        public AND(MappedConst... con)
022        {
023                super(con);
024        }
025
026        /**
027         * Checks if all the constraints satisfy.
028         * @param match match to validate
029         * @param ind mapped indices
030         * @return true if all satisfy
031         */
032        @Override
033        public boolean satisfies(Match match, int... ind)
034        {
035                for (MappedConst mc : con)
036                {
037                        if (!mc.satisfies(match, ind)) return false;
038                }
039                return true;
040        }
041
042        /**
043         * Gets intersection of the generated elements by the member constraints.
044         * @param match match to process
045         * @param ind mapped indices
046         * @return satisfying elements
047         */
048        @Override
049        public Collection<BioPAXElement> generate(Match match, int... ind)
050        {
051                Collection<BioPAXElement> gen = new HashSet<BioPAXElement> (
052                        con[0].generate(match, ind));
053
054                for (int i = 1; i < con.length; i++)
055                {
056                        if (gen.isEmpty()) break;
057
058                        gen.retainAll(con[i].generate(match, ind));
059                }
060                return gen;
061        }
062}