001package org.biopax.paxtools.pattern.constraint;
002
003import org.biopax.paxtools.model.BioPAXElement;
004import org.biopax.paxtools.pattern.Constraint;
005import org.biopax.paxtools.pattern.Match;
006
007import java.util.Collection;
008
009/**
010 * Checks the size of the generated elements of the wrapped constraint.
011 *
012 * @author Ozgun Babur
013 */
014public class Size extends ConstraintAdapter
015{
016        /**
017         * Wrapped generative constraint.
018         */
019        Constraint con;
020
021        /**
022         * Size threshold.
023         */
024        int size;
025
026        /**
027         * Type of (in)equality.
028         */
029        Type type;
030
031        /**
032         * Constructor with parameters.
033         * @param con wrapped generative constraint
034         * @param size size threshold
035         * @param type type of (in)equality
036         */
037        public Size(Constraint con, int size, Type type)
038        {
039                if (!con.canGenerate()) throw new IllegalArgumentException(
040                        "The parameter constraint have to be generative.");
041                
042                this.con = con;
043                this.size = size;
044                this.type = type;
045        }
046
047        /**
048         * Size is one less than the size of wrapped constraint.
049         * @return one less than the size of wrapped constraint
050         */
051        @Override
052        public int getVariableSize()
053        {
054                return con.getVariableSize() - 1;
055        }
056
057        /**
058         * Checks if generated element size is in limits.
059         * @param match current pattern match
060         * @param ind mapped indices
061         * @return true if generated element size is in limits
062         */
063        @Override
064        public boolean satisfies(Match match, int... ind)
065        {
066                Collection<BioPAXElement> set = con.generate(match, ind);
067                
068                switch (type)
069                {
070                        case EQUAL: return set.size() == size;
071                        case GREATER: return set.size() > size;
072                        case GREATER_OR_EQUAL: return set.size() >= size;
073                        case LESS: return set.size() < size;
074                        case LESS_OR_EQUAL: return set.size() <= size;
075                        default: throw new RuntimeException(
076                                "Should not reach here. Did somebody modify Type enum?");
077                }
078        }
079
080        /**
081         * Type of the (in)equality.
082         */
083        public enum Type
084        {
085                EQUAL,
086                GREATER,
087                LESS,
088                GREATER_OR_EQUAL,
089                LESS_OR_EQUAL
090        }
091}