Skip to main content
summaryrefslogtreecommitdiffstats
blob: fcfb2ce9dcb032bfb9ccb826e4582f9f9e158fbe (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
/*******************************************************************************
 * 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.IEvaluationContext;

/**
 * An expression representing a variable stack in the current thread.
 */
public class Variable extends Expression {

	private final String name;

	public Variable(String name) {
		this.name = name;
	}

	@Override
	public int compareTo(Expression e) {
		int cmp = super.compareTo(e);
		if (cmp == 0)
			cmp = name.compareTo(((Variable) e).name);
		return cmp;
	}

	@Override
	public boolean equals(Object o) {
		return super.equals(o) && name.equals(((Variable) o).name);
	}

	@Override
	public final Object evaluate(IEvaluationContext context) {
		return context.getValue(this);
	}

	@Override
	public Iterator<?> evaluateAsIterator(IEvaluationContext context) {
		Object value = context.getValue(this);
		if (value instanceof IRepeatableIterator<?>)
			return ((IRepeatableIterator<?>) value).getCopy();

		Iterator<?> itor = RepeatableIterator.create(value);
		setValue(context, itor);
		return itor;
	}

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

	public String getName() {
		return name;
	}

	@Override
	public String getOperator() {
		return "<variable>"; //$NON-NLS-1$
	}

	@Override
	public int getPriority() {
		return PRIORITY_VARIABLE;
	}

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

	public final void setValue(IEvaluationContext context, Object value) {
		context.setValue(this, value);
	}

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

	@Override
	int countAccessToEverything() {
		return this == ExpressionFactory.EVERYTHING ? 1 : 0;
	}
}

Back to the top