package net.ranides.assira.collection.query.support;

import lombok.experimental.UtilityClass;

import net.ranides.assira.collection.query.CQuery;
import net.ranides.assira.collection.query.CQueryFeatures;

/**
 * It is helper class which offers representing query features as an int.
 */
@UtilityClass
public class BaseFeatures {

    /**
     * Converts provided features into int bitmask
     *
     * @param that that
     * @return int
     */
    public static int flags(CQueryFeatures that) {
        int out = 0;
        if(that.hasFastEach()) out |= CQueryFeatures.EACH;
        if(that.hasFastStream()) out |= CQueryFeatures.STREAM;
        if(that.hasFastIterator()) out |= CQueryFeatures.ITERATOR;
        if(that.hasFastLength()) out |= CQueryFeatures.LENGTH;
        if(that.hasFastList()) out |= CQueryFeatures.LIST;
        if(that.isParallel()) out |= CQueryFeatures.PARALLEL;
        return out;
    }

    /**
     * Creates features bitmask which contains features supported by both "a" and "b"
     * @param a a
     * @param b b
     * @return int
     */
    public static int and(CQuery<?> a, CQuery<?> b) {
        return and(a.features(), b.features());
    }

    /**
     * Creates features bitmask which contains features supported by both "a" and "b"
     * @param a a
     * @param b b
     * @return int
     */
    public static int and(CQueryFeatures a, CQueryFeatures b) {
        return a.flags() & b.flags();
    }

}
