Skip to main content
summaryrefslogtreecommitdiffstats
blob: 77f1a3376ae2da6b417c12ab6270543f9326b1be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*******************************************************************************
 * Copyright (c) 2009, 2018 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.Iterator;
import org.eclipse.equinox.internal.p2.metadata.expression.ContextExpression;
import org.eclipse.equinox.internal.p2.metadata.expression.ExpressionFactory;
import org.eclipse.equinox.internal.p2.metadata.expression.MatchExpression;
import org.eclipse.equinox.internal.p2.metadata.expression.QueryResult;
import org.eclipse.equinox.p2.metadata.expression.ExpressionUtil;
import org.eclipse.equinox.p2.metadata.expression.IContextExpression;
import org.eclipse.equinox.p2.metadata.expression.IExpression;
import org.eclipse.equinox.p2.metadata.expression.IExpressionFactory;
import org.eclipse.equinox.p2.metadata.index.IIndexProvider;
import org.eclipse.equinox.p2.metadata.index.IQueryWithIndex;

/**
 * A query that evaluates using an iterator as input and produces a new iterator.
 * @since 2.0
 */
public class ExpressionQuery<T> implements IQueryWithIndex<T> {
	private final IContextExpression<T> expression;
	private final Class<? extends T> elementClass;

	public ExpressionQuery(Class<? extends T> elementClass, IExpression expression, Object... parameters) {
		this.elementClass = elementClass;
		this.expression = ExpressionUtil.getFactory().contextExpression(expression, parameters);
	}

	public ExpressionQuery(Class<? extends T> matchingClass, String expression, Object... parameters) {
		this(matchingClass, ExpressionUtil.parseQuery(expression), parameters);
	}

	public Class<? extends T> getElementClass() {
		return elementClass;
	}

	@Override
	public IQueryResult<T> perform(IIndexProvider<T> indexProvider) {
		return new QueryResult<>(expression.iterator(expression.createContext(elementClass, indexProvider)));
	}

	@Override
	public IQueryResult<T> perform(Iterator<T> iterator) {
		return new QueryResult<>(expression.iterator(expression.createContext(elementClass, iterator)));
	}

	@Override
	public IContextExpression<T> getExpression() {
		return expression;
	}

	public static <T> Class<? extends T> getElementClass(IQuery<T> query) {
		@SuppressWarnings("unchecked")
		Class<? extends T> elementClass = (Class<T>) Object.class;
		if (query instanceof ExpressionMatchQuery<?>)
			elementClass = ((ExpressionMatchQuery<T>) query).getMatchingClass();
		else if (query instanceof ExpressionQuery<?>)
			elementClass = ((ExpressionQuery<T>) query).getElementClass();
		return elementClass;
	}

	public static <T> IContextExpression<T> createExpression(IQuery<T> query) {
		IExpressionFactory factory = ExpressionUtil.getFactory();
		IExpression expr = query.getExpression();
		Object[] parameters;
		if (expr == null) {
			expr = factory.toExpression(query);
			if (query instanceof IMatchQuery<?>)
				expr = factory.select(ExpressionFactory.EVERYTHING, factory.lambda(ExpressionFactory.THIS, expr));
			parameters = new Object[0];
		} else {
			if (expr instanceof MatchExpression<?>) {
				MatchExpression<?> matchExpr = (MatchExpression<?>) expr;
				parameters = matchExpr.getParameters();
				expr = factory.select(ExpressionFactory.EVERYTHING, factory.lambda(ExpressionFactory.THIS, matchExpr.operand));
			} else if (expr instanceof ContextExpression<?>) {
				ContextExpression<?> contextExpr = (ContextExpression<?>) expr;
				parameters = contextExpr.getParameters();
				expr = contextExpr.operand;
			} else
				parameters = new Object[0];
		}
		return factory.contextExpression(expr, parameters);
	}

}

Back to the top