Skip to main content
summaryrefslogtreecommitdiffstats
blob: 56ab496fdd19b2dc0a2b2b7f1900b2324a257cb8 (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
96
97
98
/*******************************************************************************
 * Copyright (c) 2009, 2017 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.internal.p2.metadata.expression;

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

/**
 * The context expression is the top expression in context queries. It introduces the
 * variable 'everything' and initialized it with the iterator that represents all
 * available items.
 */
public class ContextExpression<T> extends Unary implements IContextExpression<T> {
	private static final Object[] noParams = new Object[0];
	protected final Object[] parameters;

	public ContextExpression(Expression expression, Object[] parameters) {
		super(expression);
		this.parameters = parameters == null ? noParams : parameters;
	}

	@Override
	public boolean accept(IExpressionVisitor visitor) {
		return super.accept(visitor) && operand.accept(visitor);
	}

	@Override
	public void toString(StringBuffer bld, Variable rootVariable) {
		operand.toString(bld, rootVariable);
	}

	@Override
	public IEvaluationContext createContext(Class<? extends T> elementClass, IIndexProvider<T> indexProvider) {
		Variable everything = ExpressionFactory.EVERYTHING;
		IEvaluationContext context = EvaluationContext.create(parameters, everything);
		context.setValue(everything, new Everything<>(elementClass, indexProvider));
		context.setIndexProvider(indexProvider);
		return context;
	}

	@Override
	public IEvaluationContext createContext(Class<? extends T> elementClass, Iterator<T> iterator) {
		Variable everything = ExpressionFactory.EVERYTHING;
		IEvaluationContext context = EvaluationContext.create(parameters, everything);
		context.setValue(everything, new Everything<>(elementClass, iterator, operand));
		return context;
	}

	@Override
	public Object evaluate(IEvaluationContext context) {
		return operand.evaluate(parameters.length == 0 ? context : EvaluationContext.create(context, parameters));
	}

	@Override
	public int getExpressionType() {
		return 0;
	}

	@Override
	public String getOperator() {
		throw new UnsupportedOperationException();
	}

	@Override
	public int getPriority() {
		return operand.getPriority();
	}

	@Override
	public Object[] getParameters() {
		return parameters;
	}

	@Override
	public int hashCode() {
		return operand.hashCode();
	}

	@Override
	@SuppressWarnings("unchecked")
	public Iterator<T> iterator(IEvaluationContext context) {
		return (Iterator<T>) operand.evaluateAsIterator(context);
	}

	@Override
	public void toString(StringBuffer bld) {
		toString(bld, ExpressionFactory.EVERYTHING);
	}
}

Back to the top