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

io.katharsis.queryspec.QuerySpec Maven / Gradle / Ivy

There is a newer version: 2.6.3
Show newest version
package io.katharsis.queryspec;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.katharsis.utils.CompareUtils;

public class QuerySpec {

	private Class resourceClass;
	private Long limit = null;
	private long offset = 0;
	private List filters = new ArrayList();
	private List sort = new ArrayList();
	private List includedFields = new ArrayList();
	private List includedRelations = new ArrayList();
	private Map, QuerySpec> relatedSpecs = new HashMap();

	public QuerySpec(Class resourceClass) {
		this.resourceClass = resourceClass;
	}

	public Class getResourceClass() {
		return resourceClass;
	}

	/**
	 * Evaluates this querySpec against the provided list in memory. It supports
	 * sorting, filter and paging. 
	 * 
	 * TODO currently ignores relations and inclusions, has room for improvements
	 * 
	 * @param resources resources
	 * @return sorted, filtered list.
	 */
	public  List apply(List resources) {
		InMemoryEvaluator eval = new InMemoryEvaluator();
		return eval.eval(resources, this);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((filters == null) ? 0 : filters.hashCode());
		result = prime * result + ((includedFields == null) ? 0 : includedFields.hashCode());
		result = prime * result + ((includedRelations == null) ? 0 : includedRelations.hashCode());
		result = prime * result + ((limit == null) ? 0 : limit.hashCode());
		result = prime * result + Long.valueOf(offset).hashCode();
		result = prime * result + ((relatedSpecs == null) ? 0 : relatedSpecs.hashCode());
		result = prime * result + ((sort == null) ? 0 : sort.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null || getClass() != obj.getClass())
			return false;
		QuerySpec other = (QuerySpec) obj;
		return CompareUtils.isEquals(filters, other.filters) // NOSONAR
				&& CompareUtils.isEquals(includedFields, other.includedFields)
				&& CompareUtils.isEquals(includedRelations, other.includedRelations)
				&& CompareUtils.isEquals(limit, other.limit) && CompareUtils.isEquals(offset, other.offset)
				&& CompareUtils.isEquals(relatedSpecs, other.relatedSpecs) && CompareUtils.isEquals(sort, other.sort);
	}

	public Long getLimit() {
		return limit;
	}

	public void setLimit(Long limit) {
		this.limit = limit;
	}

	public long getOffset() {
		return offset;
	}

	public void setOffset(long offset) {
		this.offset = offset;
	}

	public List getFilters() {
		return filters;
	}

	public void setFilters(List filters) {
		this.filters = filters;
	}

	public List getSort() {
		return sort;
	}

	public void setSort(List sort) {
		this.sort = sort;
	}

	public List getIncludedFields() {
		return includedFields;
	}

	public void setIncludedFields(List includedFields) {
		this.includedFields = includedFields;
	}

	public List getIncludedRelations() {
		return includedRelations;
	}

	public void setIncludedRelations(List includedRelations) {
		this.includedRelations = includedRelations;
	}

	public Map, QuerySpec> getRelatedSpecs() {
		return relatedSpecs;
	}

	public void setRelatedSpecs(Map, QuerySpec> relatedSpecs) {
		this.relatedSpecs = relatedSpecs;
	}

	public void addFilter(FilterSpec filterSpec) {
		this.filters.add(filterSpec);
	}

	public void addSort(SortSpec sortSpec) {
		this.sort.add(sortSpec);
	}

	public void includeField(List attrPath) {
		this.includedFields.add(new IncludeFieldSpec(attrPath));
	}

	public void includeRelation(List attrPath) {
		this.includedRelations.add(new IncludeRelationSpec(attrPath));
	}

	/**
	 * @param resourceClass resource class
	 * @return QuerySpec for the given class, either the root QuerySpec or any
	 *         related QuerySpec.
	 */
	public QuerySpec getQuerySpec(Class resourceClass) {
		if (resourceClass.equals(this.resourceClass))
			return this;
		return relatedSpecs.get(resourceClass);
	}

	public QuerySpec getOrCreateQuerySpec(Class resourceClass) {
		QuerySpec querySpec = getQuerySpec(resourceClass);
		if (querySpec == null) {
			querySpec = new QuerySpec(resourceClass);
			relatedSpecs.put(resourceClass, querySpec);
		}
		return querySpec;
	}

	public void putRelatedSpec(Class relatedResourceClass, QuerySpec relatedSpec) {
		if (relatedResourceClass.equals(resourceClass)) {
			throw new IllegalArgumentException("cannot set related spec with root resourceClass");
		}
		relatedSpecs.put(relatedResourceClass, relatedSpec);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy