Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 594b994da712a5386e140601f43383aed34de595 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*******************************************************************************
 * Copyright (c) 2009 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.ql.expression;

import org.eclipse.equinox.p2.query.IQueryResult;

import java.util.*;
import org.eclipse.equinox.internal.p2.ql.IRepeatableIterator;
import org.eclipse.equinox.internal.p2.ql.RepeatableIterator;
import org.eclipse.equinox.internal.p2.ql.parser.IParserConstants;
import org.eclipse.equinox.p2.ql.*;

/**
 * The base class of the expression tree.
 */
abstract class Expression implements IExpression, IParserConstants {

	static final Expression[] emptyArray = new Expression[0];

	static Set<?> asSet(Object val, boolean forcePrivateCopy) {
		if (val == null)
			throw new IllegalArgumentException("Cannot convert null into an set"); //$NON-NLS-1$

		if (val instanceof IRepeatableIterator<?>) {
			Object provider = ((IRepeatableIterator<?>) val).getIteratorProvider();
			if (!forcePrivateCopy) {
				if (provider instanceof Set<?>)
					return (Set<?>) provider;
				if (provider instanceof IQueryResult<?>)
					return ((IQueryResult<?>) provider).unmodifiableSet();
			}

			if (provider instanceof Collection<?>)
				val = provider;
		} else {
			if (!forcePrivateCopy) {
				if (val instanceof Set<?>)
					return (Set<?>) val;
				if (val instanceof IQueryResult<?>)
					return ((IQueryResult<?>) val).unmodifiableSet();
			}
		}

		HashSet<Object> result;
		if (val instanceof Collection<?>)
			result = new HashSet<Object>((Collection<?>) val);
		else {
			result = new HashSet<Object>();
			Iterator<?> iterator = RepeatableIterator.create(val);
			while (iterator.hasNext())
				result.add(iterator.next());
		}
		return result;
	}

	/**
	 * Let the visitor visit this instance and all expressions that this
	 * instance contains.
	 * @param visitor The visiting visitor.
	 * @return <code>true</code> if the visitor should continue visiting, <code>false</code> otherwise.
	 */
	public boolean accept(IExpressionVisitor visitor) {
		return visitor.accept(this);
	}

	/**
	 * Evaluate this expression with given context and variables.
	 * @param context The evaluation context
	 * @return The result of the evaluation.
	 */
	public abstract Object evaluate(IEvaluationContext context);

	public Iterator<?> evaluateAsIterator(IEvaluationContext context) {
		Object value = evaluate(context);
		if (!(value instanceof Iterator<?>))
			value = RepeatableIterator.create(value);
		return (Iterator<?>) value;
	}

	/**
	 * Checks if the expression will make repeated requests for the 'everything' iterator.
	 * @return <code>true</code> if repeated requests will be made, <code>false</code> if not.
	 */
	public boolean needsRepeatedIterations() {
		return countReferenceToEverything() > 1;
	}

	public final boolean needsTranslations() {
		final boolean[] translationSupportNeeded = new boolean[] {false};
		accept(new IExpressionVisitor() {
			public boolean accept(IExpression expr) {
				if (((Expression) expr).isReferencingTranslations()) {
					translationSupportNeeded[0] = true;
					return false;
				}
				return true;
			}
		});
		return translationSupportNeeded[0];
	}

	public String toString() {
		StringBuffer bld = new StringBuffer();
		toString(bld);
		return bld.toString();
	}

	public abstract void toString(StringBuffer bld);

	static void appendOperand(StringBuffer bld, Expression operand, int priority) {
		if (priority < operand.getPriority()) {
			bld.append('(');
			operand.toString(bld);
			bld.append(')');
		} else
			operand.toString(bld);
	}

	void assertNotCollection(Expression expr, String usage) {
		if (expr.isCollection())
			throw new IllegalArgumentException("A collection cannot be used as " + usage + " in a " + getOperator()); //$NON-NLS-1$//$NON-NLS-2$
	}

	void assertNotBoolean(Expression expr, String usage) {
		if (expr.isBoolean())
			throw new IllegalArgumentException("A boolean cannot be used as " + usage + " in a " + getOperator()); //$NON-NLS-1$//$NON-NLS-2$
	}

	int countReferenceToEverything() {
		return 0;
	}

	abstract String getOperator();

	abstract int getPriority();

	boolean isBoolean() {
		return false;
	}

	boolean isCollection() {
		return false;
	}

	boolean isElementBoolean() {
		return false;
	}

	boolean isReferencingTranslations() {
		return false;
	}

	boolean isPipeable() {
		// TODO Auto-generated method stub
		return false;
	}

	Expression pipeFrom(Expression nxt) {
		// TODO Auto-generated method stub
		return null;
	}

	boolean isElementCollection() {
		// TODO Auto-generated method stub
		return false;
	}
}

Back to the top