| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009, 2018 Cloudsmith Inc. and others. |
| 3 |
* |
| 4 |
* This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License 2.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: EPL-2.0 |
| 10 |
* |
| 11 |
* Contributors: |
| 12 |
* Cloudsmith Inc. - initial API and implementation |
| 13 |
*******************************************************************************/ |
| 14 |
package org.eclipse.equinox.p2.query; |
| 15 |
|
| 16 |
import java.util.Iterator; |
| 17 |
import org.eclipse.equinox.internal.p2.metadata.expression.ContextExpression; |
| 18 |
import org.eclipse.equinox.internal.p2.metadata.expression.ExpressionFactory; |
| 19 |
import org.eclipse.equinox.internal.p2.metadata.expression.MatchExpression; |
| 20 |
import org.eclipse.equinox.internal.p2.metadata.expression.QueryResult; |
| 21 |
import org.eclipse.equinox.p2.metadata.expression.ExpressionUtil; |
| 22 |
import org.eclipse.equinox.p2.metadata.expression.IContextExpression; |
| 23 |
import org.eclipse.equinox.p2.metadata.expression.IExpression; |
| 24 |
import org.eclipse.equinox.p2.metadata.expression.IExpressionFactory; |
| 25 |
import org.eclipse.equinox.p2.metadata.index.IIndexProvider; |
| 26 |
import org.eclipse.equinox.p2.metadata.index.IQueryWithIndex; |
| 27 |
|
| 28 |
/** |
| 29 |
* A query that evaluates using an iterator as input and produces a new iterator. |
| 30 |
* @since 2.0 |
| 31 |
*/ |
| 32 |
public class ExpressionQuery<T> implements IQueryWithIndex<T> { |
| 33 |
private final IContextExpression<T> expression; |
| 34 |
private final Class<? extends T> elementClass; |
| 35 |
|
| 36 |
public ExpressionQuery(Class<? extends T> elementClass, IExpression expression, Object... parameters) { |
| 37 |
this.elementClass = elementClass; |
| 38 |
this.expression = ExpressionUtil.getFactory().contextExpression(expression, parameters); |
| 39 |
} |
| 40 |
|
| 41 |
public ExpressionQuery(Class<? extends T> matchingClass, String expression, Object... parameters) { |
| 42 |
this(matchingClass, ExpressionUtil.parseQuery(expression), parameters); |
| 43 |
} |
| 44 |
|
| 45 |
public Class<? extends T> getElementClass() { |
| 46 |
return elementClass; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public IQueryResult<T> perform(IIndexProvider<T> indexProvider) { |
| 51 |
return new QueryResult<>(expression.iterator(expression.createContext(elementClass, indexProvider))); |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public IQueryResult<T> perform(Iterator<T> iterator) { |
| 56 |
return new QueryResult<>(expression.iterator(expression.createContext(elementClass, iterator))); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public IContextExpression<T> getExpression() { |
| 61 |
return expression; |
| 62 |
} |
| 63 |
|
| 64 |
public static <T> Class<? extends T> getElementClass(IQuery<T> query) { |
| 65 |
@SuppressWarnings("unchecked") |
| 66 |
Class<? extends T> elementClass = (Class<T>) Object.class; |
| 67 |
if (query instanceof ExpressionMatchQuery<?>) |
| 68 |
elementClass = ((ExpressionMatchQuery<T>) query).getMatchingClass(); |
| 69 |
else if (query instanceof ExpressionQuery<?>) |
| 70 |
elementClass = ((ExpressionQuery<T>) query).getElementClass(); |
| 71 |
return elementClass; |
| 72 |
} |
| 73 |
|
| 74 |
public static <T> IContextExpression<T> createExpression(IQuery<T> query) { |
| 75 |
IExpressionFactory factory = ExpressionUtil.getFactory(); |
| 76 |
IExpression expr = query.getExpression(); |
| 77 |
Object[] parameters; |
| 78 |
if (expr == null) { |
| 79 |
expr = factory.toExpression(query); |
| 80 |
if (query instanceof IMatchQuery<?>) |
| 81 |
expr = factory.select(ExpressionFactory.EVERYTHING, factory.lambda(ExpressionFactory.THIS, expr)); |
| 82 |
parameters = new Object[0]; |
| 83 |
} else { |
| 84 |
if (expr instanceof MatchExpression<?>) { |
| 85 |
MatchExpression<?> matchExpr = (MatchExpression<?>) expr; |
| 86 |
parameters = matchExpr.getParameters(); |
| 87 |
expr = factory.select(ExpressionFactory.EVERYTHING, factory.lambda(ExpressionFactory.THIS, matchExpr.operand)); |
| 88 |
} else if (expr instanceof ContextExpression<?>) { |
| 89 |
ContextExpression<?> contextExpr = (ContextExpression<?>) expr; |
| 90 |
parameters = contextExpr.getParameters(); |
| 91 |
expr = contextExpr.operand; |
| 92 |
} else |
| 93 |
parameters = new Object[0]; |
| 94 |
} |
| 95 |
return factory.contextExpression(expr, parameters); |
| 96 |
} |
| 97 |
|
| 98 |
} |