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

org.securegraph.query.CompositeGraphQuery Maven / Gradle / Ivy

The newest version!
package org.securegraph.query;

import org.securegraph.Edge;
import org.securegraph.FetchHint;
import org.securegraph.Vertex;
import org.securegraph.util.SelectManyIterable;

import java.util.*;

public class CompositeGraphQuery implements Query {
    private final List queries;

    public CompositeGraphQuery(Query... queries) {
        this(Arrays.asList(queries));
    }

    public CompositeGraphQuery(Collection queries) {
        this.queries = new ArrayList<>(queries);
    }

    @Override
    public Iterable vertices() {
        return vertices(FetchHint.ALL);
    }

    @Override
    public Iterable vertices(final EnumSet fetchHints) {
        final Set seenIds = new HashSet<>();
        return new SelectManyIterable(this.queries) {
            @Override
            public Iterable getIterable(Query query) {
                return query.vertices(fetchHints);
            }

            @Override
            protected boolean isIncluded(Vertex vertex) {
                if (seenIds.contains(vertex.getId())) {
                    return false;
                }
                seenIds.add(vertex.getId());
                return super.isIncluded(vertex);
            }
        };
    }

    @Override
    public Iterable edges() {
        return edges(FetchHint.ALL);
    }

    @Override
    public Iterable edges(final EnumSet fetchHints) {
        final Set seenIds = new HashSet<>();
        return new SelectManyIterable(this.queries) {
            @Override
            public Iterable getIterable(Query query) {
                return query.edges(fetchHints);
            }

            @Override
            protected boolean isIncluded(Edge edge) {
                if (seenIds.contains(edge.getId())) {
                    return false;
                }
                seenIds.add(edge.getId());
                return super.isIncluded(edge);
            }
        };
    }

    @Override
    public Iterable edges(final String label) {
        return edges(label, FetchHint.ALL);
    }

    @Override
    public Iterable edges(final String label, final EnumSet fetchHints) {
        final Set seenIds = new HashSet<>();
        return new SelectManyIterable(this.queries) {
            @Override
            public Iterable getIterable(Query query) {
                return query.edges(label, fetchHints);
            }

            @Override
            protected boolean isIncluded(Edge edge) {
                if (seenIds.contains(edge.getId())) {
                    return false;
                }
                seenIds.add(edge.getId());
                return super.isIncluded(edge);
            }
        };
    }

    @Override
    public  Query range(String propertyName, T startValue, T endValue) {
        for (Query query : queries) {
            query.range(propertyName, startValue, endValue);
        }
        return this;
    }

    @Override
    public  Query has(String propertyName, T value) {
        for (Query query : queries) {
            query.has(propertyName, value);
        }
        return this;
    }

    @Override
    public  Query has(String propertyName, Predicate predicate, T value) {
        for (Query query : queries) {
            query.has(propertyName, predicate, value);
        }
        return this;
    }

    @Override
    public Query skip(int count) {
        for (Query query : queries) {
            query.skip(count);
        }
        return this;
    }

    @Override
    public Query limit(int count) {
        for (Query query : queries) {
            query.limit(count);
        }
        return this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy