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

org.eclipse.equinox.p2.query.ExpressionMatchQuery Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2009, 2010 Cloudsmith Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.query;

import java.util.*;
import org.eclipse.equinox.internal.p2.metadata.expression.*;
import org.eclipse.equinox.p2.metadata.expression.*;
import org.eclipse.equinox.p2.metadata.index.*;

/**
 * A query that matches candidates against an expression.
 * @since 2.0
 */
public class ExpressionMatchQuery implements IMatchQuery, IQueryWithIndex {
	private final IMatchExpression expression;
	private final Class matchingClass;
	private final IEvaluationContext context;
	private final List indexedMembers;

	public ExpressionMatchQuery(Class matchingClass, IExpression expression, Object... parameters) {
		this.matchingClass = matchingClass;
		this.expression = ExpressionUtil.getFactory(). matchExpression(expression, parameters);
		this.context = this.expression.createContext();
		this.indexedMembers = Expression.getIndexCandidateMembers(matchingClass, ExpressionFactory.THIS, (Expression) expression);
	}

	public ExpressionMatchQuery(Class matchingClass, String expression, Object... parameters) {
		this(matchingClass, ExpressionUtil.parse(expression), parameters);
	}

	public IEvaluationContext getContext() {
		return context;
	}

	public Class getMatchingClass() {
		return matchingClass;
	}

	public IQueryResult perform(IIndexProvider indexProvider) {
		if (((MatchExpression) expression).operand == ExpressionUtil.TRUE_EXPRESSION)
			return new QueryResult(RepeatableIterator.create(indexProvider));
		Iterator iterator = null;
		int top = indexedMembers.size();
		for (int idx = 0; idx < top; ++idx) {
			IIndex index = indexProvider.getIndex(indexedMembers.get(idx));
			if (index != null) {
				iterator = index.getCandidates(context, ExpressionFactory.THIS, expression);
				if (iterator != null)
					break;
			}
		}
		if (iterator == null)
			iterator = RepeatableIterator.create(indexProvider);
		context.setIndexProvider(indexProvider);
		return perform(iterator);
	}

	public IQueryResult perform(Iterator iterator) {
		if (((MatchExpression) expression).operand == ExpressionUtil.TRUE_EXPRESSION)
			return new QueryResult(iterator);

		HashSet result = null;
		while (iterator.hasNext()) {
			T value = iterator.next();
			if (isMatch(value)) {
				if (result == null)
					result = new HashSet();
				result.add(value);
			}
		}
		return result == null ? Collector. emptyCollector() : new CollectionResult(result);
	}

	public boolean isMatch(T candidate) {
		if (!matchingClass.isInstance(candidate))
			return false;
		ExpressionFactory.THIS.setValue(context, candidate);
		return Boolean.TRUE == expression.evaluate(context);
	}

	public IMatchExpression getExpression() {
		return expression;
	}

	public void setIndexProvider(IIndexProvider indexProvider) {
		context.setIndexProvider(indexProvider);
	}

	public void prePerform() { //
	}

	public void postPerform() { //
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy