Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 94346a9c68cafb8f34d1ae15b5edcef2c014b966 (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
/*******************************************************************************
 * Copyright (c) 2009, 2010 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.*;
import org.eclipse.equinox.p2.metadata.expression.IEvaluationContext;

/**
 * An expression that ensures that the elements of its collection is only returned
 * once throughout the whole query.
 */
final class Unique extends Binary {
	/**
	 * A UniqueIterator that uses a set as a discriminator, ensuring that
	 * no element is returned twice.
	 */
	static class UniqueIterator<T> extends MatchIteratorFilter<T> {
		final Set<T> uniqueSet;

		public UniqueIterator(Iterator<? extends T> iterator, Set<T> uniqueSet) {
			super(iterator);
			this.uniqueSet = uniqueSet;
		}

		protected boolean isMatch(T val) {
			synchronized (uniqueSet) {
				return uniqueSet.add(val);
			}
		}
	}

	Unique(Expression collection, Expression explicitCache) {
		super(collection, explicitCache);
	}

	public Object evaluate(IEvaluationContext context) {
		return evaluateAsIterator(context);
	}

	@SuppressWarnings("unchecked")
	public Iterator<?> evaluateAsIterator(IEvaluationContext context) {
		Object explicitCache = rhs.evaluate(context);
		Set<Object> uniqueSet;
		if (explicitCache == null)
			// No cache, we just ensure that the iteration is unique
			uniqueSet = new HashSet<Object>();
		else {
			if (!(explicitCache instanceof Set<?>))
				throw new IllegalArgumentException("Unique cache must be a java.util.Set"); //$NON-NLS-1$
			uniqueSet = (Set<Object>) explicitCache;
		}
		return new UniqueIterator<Object>(lhs.evaluateAsIterator(context), uniqueSet);
	}

	public int getExpressionType() {
		return TYPE_UNIQUE;
	}

	public void toString(StringBuffer bld, Variable rootVariable) {
		CollectionFilter.appendProlog(bld, rootVariable, lhs, getOperator());
		if (rhs != Literal.NULL_CONSTANT)
			appendOperand(bld, rootVariable, rhs, IExpressionConstants.PRIORITY_COMMA);
		bld.append(')');
	}

	public String getOperator() {
		return KEYWORD_UNIQUE;
	}

	public int getPriority() {
		return PRIORITY_COLLECTION;
	}
}

Back to the top