| 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.HashSet; |
| 17 |
import java.util.Iterator; |
| 18 |
import java.util.List; |
| 19 |
import org.eclipse.equinox.internal.p2.metadata.expression.Expression; |
| 20 |
import org.eclipse.equinox.internal.p2.metadata.expression.ExpressionFactory; |
| 21 |
import org.eclipse.equinox.internal.p2.metadata.expression.MatchExpression; |
| 22 |
import org.eclipse.equinox.internal.p2.metadata.expression.QueryResult; |
| 23 |
import org.eclipse.equinox.internal.p2.metadata.expression.RepeatableIterator; |
| 24 |
import org.eclipse.equinox.p2.metadata.expression.ExpressionUtil; |
| 25 |
import org.eclipse.equinox.p2.metadata.expression.IEvaluationContext; |
| 26 |
import org.eclipse.equinox.p2.metadata.expression.IExpression; |
| 27 |
import org.eclipse.equinox.p2.metadata.expression.IMatchExpression; |
| 28 |
import org.eclipse.equinox.p2.metadata.index.IIndex; |
| 29 |
import org.eclipse.equinox.p2.metadata.index.IIndexProvider; |
| 30 |
import org.eclipse.equinox.p2.metadata.index.IQueryWithIndex; |
| 31 |
|
| 32 |
/** |
| 33 |
* A query that matches candidates against an expression. |
| 34 |
* @since 2.0 |
| 35 |
*/ |
| 36 |
public class ExpressionMatchQuery<T> implements IMatchQuery<T>, IQueryWithIndex<T> { |
| 37 |
private final IMatchExpression<T> expression; |
| 38 |
private final Class<? extends T> matchingClass; |
| 39 |
private final IEvaluationContext context; |
| 40 |
private final List<String> indexedMembers; |
| 41 |
|
| 42 |
public ExpressionMatchQuery(Class<? extends T> matchingClass, IExpression expression, Object... parameters) { |
| 43 |
this.matchingClass = matchingClass; |
| 44 |
this.expression = ExpressionUtil.getFactory().matchExpression(expression, parameters); |
| 45 |
this.context = this.expression.createContext(); |
| 46 |
this.indexedMembers = Expression.getIndexCandidateMembers(matchingClass, ExpressionFactory.THIS, (Expression) expression); |
| 47 |
} |
| 48 |
|
| 49 |
public ExpressionMatchQuery(Class<? extends T> matchingClass, String expression, Object... parameters) { |
| 50 |
this(matchingClass, ExpressionUtil.parse(expression), parameters); |
| 51 |
} |
| 52 |
|
| 53 |
public IEvaluationContext getContext() { |
| 54 |
return context; |
| 55 |
} |
| 56 |
|
| 57 |
public Class<? extends T> getMatchingClass() { |
| 58 |
return matchingClass; |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public IQueryResult<T> perform(IIndexProvider<T> indexProvider) { |
| 63 |
if (((MatchExpression<T>) expression).operand == ExpressionUtil.TRUE_EXPRESSION) |
| 64 |
return new QueryResult<>(RepeatableIterator.create(indexProvider)); |
| 65 |
Iterator<T> iterator = null; |
| 66 |
int top = indexedMembers.size(); |
| 67 |
for (int idx = 0; idx < top; ++idx) { |
| 68 |
IIndex<T> index = indexProvider.getIndex(indexedMembers.get(idx)); |
| 69 |
if (index != null) { |
| 70 |
iterator = index.getCandidates(context, ExpressionFactory.THIS, expression); |
| 71 |
if (iterator != null) |
| 72 |
break; |
| 73 |
} |
| 74 |
} |
| 75 |
if (iterator == null) |
| 76 |
iterator = RepeatableIterator.create(indexProvider); |
| 77 |
context.setIndexProvider(indexProvider); |
| 78 |
return perform(iterator); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public IQueryResult<T> perform(Iterator<T> iterator) { |
| 83 |
if (((MatchExpression<T>) expression).operand == ExpressionUtil.TRUE_EXPRESSION) |
| 84 |
return new QueryResult<>(iterator); |
| 85 |
|
| 86 |
HashSet<T> result = null; |
| 87 |
while (iterator.hasNext()) { |
| 88 |
T value = iterator.next(); |
| 89 |
if (isMatch(value)) { |
| 90 |
if (result == null) |
| 91 |
result = new HashSet<>(); |
| 92 |
result.add(value); |
| 93 |
} |
| 94 |
} |
| 95 |
return result == null ? Collector.emptyCollector() : new CollectionResult<>(result); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public boolean isMatch(T candidate) { |
| 100 |
if (!matchingClass.isInstance(candidate)) |
| 101 |
return false; |
| 102 |
ExpressionFactory.THIS.setValue(context, candidate); |
| 103 |
return Boolean.TRUE == expression.evaluate(context); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public IMatchExpression<T> getExpression() { |
| 108 |
return expression; |
| 109 |
} |
| 110 |
|
| 111 |
public void setIndexProvider(IIndexProvider<T> indexProvider) { |
| 112 |
context.setIndexProvider(indexProvider); |
| 113 |
} |
| 114 |
|
| 115 |
public void prePerform() { // |
| 116 |
} |
| 117 |
|
| 118 |
public void postPerform() { // |
| 119 |
} |
| 120 |
} |