MatchAllQuery.java

package org.microspace.table.query;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.microspace.space.SimpleSpace;
import org.microspace.table.Entry;
import org.microspace.table.column.Accessor;
import org.microspace.table.column.ColumnReferences;
import org.microspace.table.column.IndexedMap;
import org.microspace.util.UniqueId;

/**
 * This Query matches everything.
 * @author Gaspar Sinai - {@literal gaspar.sinai@microspace.org}
 * @version 2016-06-26
 * @param <T> is the type of the Table.
 */
public class MatchAllQuery<T> implements TableQuery<T>  {
	
	final Class<T>  tableClass;
	final UniqueId  spaceId;
	final String id;
	
	public MatchAllQuery (Class<T>  tableClass) {
		this(tableClass, null);
	}
	public MatchAllQuery (Class<T>  tableClass, UniqueId spaceId) {
		this.tableClass = tableClass;
		this.spaceId = spaceId;
		if (spaceId == null) {
			id = tableClass.getName() + ": MatchAllQuery";
		} else {
			id = spaceId.toString() + ": " + tableClass.getName() + ": MatchAllQuery";
		}

	}
	@Override
	public IndexedMap<Object, Entry<T>> getMatchingEntries(Accessor<T> accessor,
			IndexedMap<Object, Entry<T>> entries, ColumnReferences<T>[] indexedColumns) {
		return entries;
	}
	@Override
	public List<Entry<T>> sortAndLimit(Accessor<T> accessor,
			IndexedMap<Object, Entry<T>> entries) {
		
		ArrayList<Entry<T>> ret = new ArrayList<Entry<T>> (0);
		if (entries.size() == 0) return ret;
		ret.ensureCapacity (entries.size());
		ret.addAll(entries.values());
		Collections.sort(ret);
		return ret;
	}

	@Override
	public Class<T> getTableClass() {
		return tableClass;
	}

	@Override
	public boolean match(T object, Accessor<T> accessor) {
		return true;
	}

	@Override
	public String getId() {
		return id;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareQuery(SimpleSpace space) {
		
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void finishQuery() {
	}
	@Override
	public UniqueId getSpaceId() {
		return spaceId;
	}

}