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

org.deephacks.tools4j.config.internal.core.query.ConfigIndexedCollection 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.CQEngine;
import com.googlecode.cqengine.IndexedCollection;
import com.googlecode.cqengine.attribute.Attribute;
import com.googlecode.cqengine.index.navigable.NavigableIndex;
import com.googlecode.cqengine.query.Query;
import com.googlecode.cqengine.resultset.ResultSet;
import org.deephacks.tools4j.config.model.Bean;
import org.deephacks.tools4j.config.model.Bean.BeanId;

import java.util.Iterator;

public class ConfigIndexedCollection {
    final IndexedCollection collection = CQEngine.newInstance();
    private final ConfigIndex index;

    public ConfigIndexedCollection(ConfigIndex index) {
        this.index = index;
        for(Attribute a : index.get()) {
            collection.addIndex(NavigableIndex.onAttribute(a));
        }
    }

    public void add(Bean bean) {
        ConfigIndexFields fields = new ConfigIndexFields(bean);
        collection.add(fields);
    }

    public void remove(BeanId beanId) {
        ConfigIndexFields id = new ConfigIndexFields(beanId);
        collection.remove(id);
    }

    Attribute getAttribute(String prop) {
        Attribute attr = index.get(prop);
        if(attr == null) {
            throw new IllegalArgumentException("Field ["+prop+"] is not indexed.");
        }
        return attr;
    }

    public ResultSet retrieve(Query query) {
        return collection.retrieve(query);
    }

    public ResultSet all() {
        return new ResultSet() {
            @Override
            public Iterator iterator() {
                return collection.iterator();
            }

            @Override
            public boolean contains(ConfigIndexFields object) {
                return collection.contains(object);
            }

            @Override
            public int getRetrievalCost() {
                throw new UnsupportedOperationException();
            }

            @Override
            public int getMergeCost() {
                throw new UnsupportedOperationException();
            }

            @Override
            public int size() {
                return collection.size();
            }
        };
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy