All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.deephacks.tools4j.config.internal.core.query.ConfigQuery Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
package org.deephacks.tools4j.config.internal.core.query;


import com.googlecode.cqengine.attribute.Attribute;
import com.googlecode.cqengine.query.Query;
import org.deephacks.tools4j.config.query.ConfigQueryBuilder.And;
import org.deephacks.tools4j.config.query.ConfigQueryBuilder.Between;
import org.deephacks.tools4j.config.query.ConfigQueryBuilder.Equals;
import org.deephacks.tools4j.config.query.ConfigQueryBuilder.GreaterThan;
import org.deephacks.tools4j.config.query.ConfigQueryBuilder.Has;
import org.deephacks.tools4j.config.query.ConfigQueryBuilder.In;
import org.deephacks.tools4j.config.query.ConfigQueryBuilder.LessThan;
import org.deephacks.tools4j.config.query.ConfigQueryBuilder.LogicalRestriction;
import org.deephacks.tools4j.config.query.ConfigQueryBuilder.Not;
import org.deephacks.tools4j.config.query.ConfigQueryBuilder.Or;
import org.deephacks.tools4j.config.query.ConfigQueryBuilder.PropertyRestriction;
import org.deephacks.tools4j.config.query.ConfigQueryBuilder.Restriction;
import org.deephacks.tools4j.config.query.ConfigQueryBuilder.StringContains;
import org.deephacks.tools4j.config.spi.CacheManager;

import java.util.ArrayList;
import java.util.Collection;

import static com.googlecode.cqengine.query.QueryFactory.*;


public class ConfigQuery implements org.deephacks.tools4j.config.query.ConfigQuery {

    private Query query;
    private ConfigIndexedCollection collection;
    private final CacheManager cacheManager;

    public ConfigQuery(ConfigIndexedCollection collection, CacheManager cacheManager) {
        this.collection = collection;
        this.cacheManager = cacheManager;
    }

    public ConfigQuery(ConfigIndexedCollection collection, Query query, CacheManager cacheManager) {
        this.query = query;
        this.collection = collection;
        this.cacheManager = cacheManager;
    }

    public Query getQuery() {
        return query;
    }

    @Override
    public org.deephacks.tools4j.config.query.ConfigQuery add(Restriction restriction) {
        if(restriction instanceof PropertyRestriction) {
            String property = ((PropertyRestriction) restriction).getProperty();
            Attribute attr = collection.getAttribute(property);
            if (restriction instanceof Equals) {
                Query q = equal(attr, ((Equals) restriction).getValue());
                if(query != null) {
                    q = and(query, q);
                }
                return new ConfigQuery<>(collection, q, cacheManager);
            } else if (restriction instanceof StringContains) {
                Query q = contains(attr, ((StringContains) restriction).getValue());
                if(query != null) {
                    q = and(query, q);
                }
                return new ConfigQuery<>(collection, q, cacheManager);
            } else if (restriction instanceof Between) {
                Between _between = ((Between) restriction);
                Query q = between(attr, _between.getLower(), _between.getUpper());
                if(query != null) {
                    q = and(query, q);
                }
                return new ConfigQuery<>(collection, q, cacheManager);
            } else if (restriction instanceof GreaterThan) {
                Query q = greaterThan(attr, ((GreaterThan) restriction).getValue());
                if(query != null) {
                    q = and(query, q);
                }
                return new ConfigQuery<>(collection, q, cacheManager);
            } else if (restriction instanceof LessThan) {
                LessThan lesser = ((LessThan) restriction);
                Query q = lessThan(attr, lesser.getValue());
                if(query != null) {
                    q = and(query, q);
                }

                return new ConfigQuery<>(collection, q, cacheManager);
            } else if (restriction instanceof Has) {
                Query q = has(attr);
                if(query != null) {
                    q = and(query, q);
                }
                return new ConfigQuery<>(collection, q, cacheManager);
            } else if (restriction instanceof In) {
                In _in = ((In) restriction);
                Query q = in(attr, _in.getValues());
                if(query != null) {
                    q = and(query, q);
                }
                return new ConfigQuery<>(collection, q, cacheManager);
            } else {
                throw new IllegalArgumentException("Could not identify restriction: " + restriction);
            }
        } else if(restriction instanceof LogicalRestriction) {
            if (restriction instanceof And) {
                And and = (And) restriction;
                Collection restrictions = new ArrayList<>();
                for (Restriction res : and.getRestrictions()) {
                    restrictions.add(((ConfigQuery)add(res)).getQuery());
                }
                Query q = new com.googlecode.cqengine.query.logical.And(restrictions);
                if(query != null) {
                    q = and(query, q);
                }
                return new ConfigQuery<>(collection, q, cacheManager);
            } else if (restriction instanceof Or) {
                Or and = (Or) restriction;
                Collection restrictions = new ArrayList<>();
                for (Restriction res : and.getRestrictions()) {
                    restrictions.add(((ConfigQuery) add(res)).getQuery());
                }
                Query q = new com.googlecode.cqengine.query.logical.Or(restrictions, true);
                if(query != null) {
                    q = and(query, q);
                }
                return new ConfigQuery<>(collection, q, cacheManager);
            } else if (restriction instanceof Not) {
                Not not = (Not) restriction;
                Restriction res = not.getRestrictions().get(0);
                Query q = new com.googlecode.cqengine.query.logical.Not(((ConfigQuery)add(res)).getQuery());
                if(query != null) {
                    q = and(query, q);
                }
                return new ConfigQuery<>(collection, q, cacheManager);
            } else {
                throw new IllegalArgumentException("Could not identify restriction: " + restriction);
            }
        }
        throw new IllegalArgumentException("Could not identify restriction: " + restriction);
    }

    @Override
    public org.deephacks.tools4j.config.query.ConfigResultSet retrieve() {
        if(query == null) {
            return new ConfigResultSet(collection.all(), cacheManager);
        }
        com.googlecode.cqengine.resultset.ResultSet resultSet = collection.retrieve(query);
        return (org.deephacks.tools4j.config.query.ConfigResultSet) new ConfigResultSet(resultSet, cacheManager);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy