Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 261758
Collapse All | Expand All

(-)src/org/eclipse/equinox/internal/provisional/p2/query/MatchQuery.java (-1 / +6 lines)
Lines 11-16 Link Here
11
package org.eclipse.equinox.internal.provisional.p2.query;
11
package org.eclipse.equinox.internal.provisional.p2.query;
12
12
13
import java.util.Iterator;
13
import java.util.Iterator;
14
import org.eclipse.equinox.internal.provisional.p2.query.CompoundQueryable.IMultiQuery;
14
15
15
/**
16
/**
16
 * This class represents the superclass of most of p2's queries.  Every element
17
 * This class represents the superclass of most of p2's queries.  Every element
Lines 24-30 Link Here
24
 * computation, to allow {@link IQueryable} implementations to optimize their
25
 * computation, to allow {@link IQueryable} implementations to optimize their
25
 * execution of the query. 
26
 * execution of the query. 
26
 */
27
 */
27
public abstract class MatchQuery implements IMatchQuery {
28
public abstract class MatchQuery implements IMatchQuery, IMultiQuery {
28
29
29
	/**
30
	/**
30
	 * Returns whether the given object satisfies the parameters of this query.
31
	 * Returns whether the given object satisfies the parameters of this query.
Lines 35-40 Link Here
35
	 */
36
	 */
36
	public abstract boolean isMatch(Object candidate);
37
	public abstract boolean isMatch(Object candidate);
37
38
39
	public Query getCompoundingQuery() {
40
		return this;
41
	}
42
38
	/**
43
	/**
39
	 * Performs this query on the given iterator, passing all objects in the iterator 
44
	 * Performs this query on the given iterator, passing all objects in the iterator 
40
	 * that match the criteria of this query to the given result.
45
	 * that match the criteria of this query to the given result.
(-)src/org/eclipse/equinox/internal/provisional/p2/query/CompoundQueryable.java (-1 / +11 lines)
Lines 20-29 Link Here
20
20
21
	private IQueryable[] queryables;
21
	private IQueryable[] queryables;
22
22
23
	public interface IMultiQuery extends Query {
24
		public Query getCompoundingQuery();
25
	}
26
23
	public CompoundQueryable(IQueryable[] queryables) {
27
	public CompoundQueryable(IQueryable[] queryables) {
24
		this.queryables = queryables;
28
		this.queryables = queryables;
25
	}
29
	}
26
30
31
	private Query getCompoundingQuery(Query q) {
32
		if (q instanceof IMultiQuery)
33
			return ((IMultiQuery) q).getCompoundingQuery();
34
		return new MatchAllQuery();
35
	}
36
27
	public Collector query(Query query, Collector collector, IProgressMonitor monitor) {
37
	public Collector query(Query query, Collector collector, IProgressMonitor monitor) {
28
		if (monitor == null) {
38
		if (monitor == null) {
29
			monitor = new NullProgressMonitor();
39
			monitor = new NullProgressMonitor();
Lines 42-48 Link Here
42
			for (int i = 0; i < queryables.length; i++) {
52
			for (int i = 0; i < queryables.length; i++) {
43
				if (subMonitor.isCanceled())
53
				if (subMonitor.isCanceled())
44
					break;
54
					break;
45
				results = queryables[i].query(query, results, subMonitor.newChild(10));
55
				results = queryables[i].query(getCompoundingQuery(query), results, subMonitor.newChild(10));
46
			}
56
			}
47
57
48
			if (!isMatchQuery) {
58
			if (!isMatchQuery) {
(-)src/org/eclipse/equinox/internal/provisional/p2/query/CompoundQuery.java (-1 / +21 lines)
Lines 12-17 Link Here
12
package org.eclipse.equinox.internal.provisional.p2.query;
12
package org.eclipse.equinox.internal.provisional.p2.query;
13
13
14
import java.util.*;
14
import java.util.*;
15
import org.eclipse.equinox.internal.provisional.p2.query.CompoundQueryable.IMultiQuery;
15
16
16
/**
17
/**
17
 * A query that combines a group of sub-queries.<P>
18
 * A query that combines a group of sub-queries.<P>
Lines 26-32 Link Here
26
 * 
27
 * 
27
 * @noextend This class is not intended to be subclassed by clients.
28
 * @noextend This class is not intended to be subclassed by clients.
28
 */
29
 */
29
public abstract class CompoundQuery implements Query {
30
public abstract class CompoundQuery implements Query, IMultiQuery {
30
	protected Query[] queries;
31
	protected Query[] queries;
31
	protected boolean and;
32
	protected boolean and;
32
33
Lines 123-128 Link Here
123
			}
124
			}
124
			return result;
125
			return result;
125
		}
126
		}
127
128
		public Query getCompoundingQuery() {
129
			return MatchCompoundQuery.this;
130
		}
126
	}
131
	}
127
132
128
	/**
133
	/**
Lines 181-185 Link Here
181
				gatherResults = result.accept(resultIterator.next());
186
				gatherResults = result.accept(resultIterator.next());
182
			return result;
187
			return result;
183
		}
188
		}
189
190
		/* (non-Javadoc)
191
		 * @see org.eclipse.equinox.internal.provisional.p2.query.CompoundQueryable.IMultiQuery#getCompoundingQuery()
192
		 */
193
		public Query getCompoundingQuery() {
194
			Query[] parallelQueries = new Query[this.queries.length];
195
			for (int i = 0; i < this.queries.length; i++) {
196
				if (queries[i] instanceof IMultiQuery)
197
					parallelQueries[i] = ((IMultiQuery) queries[i]).getCompoundingQuery();
198
				else
199
					parallelQueries[i] = new MatchAllQuery();
200
			}
201
			return CompoundQuery.createCompoundQuery(parallelQueries, false);
202
203
		}
184
	}
204
	}
185
}
205
}
(-)src/org/eclipse/equinox/internal/provisional/p2/query/CompositeQuery.java (-2 / +15 lines)
Lines 10-28 Link Here
10
package org.eclipse.equinox.internal.provisional.p2.query;
10
package org.eclipse.equinox.internal.provisional.p2.query;
11
11
12
import java.util.Iterator;
12
import java.util.Iterator;
13
import org.eclipse.equinox.internal.provisional.p2.query.CompoundQueryable.IMultiQuery;
13
14
14
/**
15
/**
15
 * A Composite Query is an aggregate query in which each sub-query
16
 * A Composite Query is an aggregate query in which each sub-query
16
 * is executed in succession.  The results from the ith sub-query
17
 * is executed in succession.  The results from the ith sub-query
17
 * are piped as input into the i+1th sub-query.
18
 * are piped as input into the i+1th sub-query.
18
 */
19
 */
19
public class CompositeQuery implements Query {
20
public class CompositeQuery implements Query, IMultiQuery {
20
	protected Query[] queries;
21
	protected Query[] queries;
21
22
22
	public CompositeQuery(Query[] queries) {
23
	public CompositeQuery(Query[] queries) {
23
		this.queries = queries;
24
		this.queries = queries;
24
	}
25
	}
25
26
	
26
	/**
27
	/**
27
	 * Set the queries of this composite.  This is needed to allow subclasses of 
28
	 * Set the queries of this composite.  This is needed to allow subclasses of 
28
	 * CompsiteQuery to set the queries in a constructor
29
	 * CompsiteQuery to set the queries in a constructor
Lines 45-48 Link Here
45
			gatherResults = result.accept(iter.next());
46
			gatherResults = result.accept(iter.next());
46
		return result;
47
		return result;
47
	}
48
	}
49
50
	public Query getCompoundingQuery() {
51
		Query[] parallelQueries = new Query[this.queries.length];
52
		for (int i = 0; i < this.queries.length; i++) {
53
			if (queries[i] instanceof IMultiQuery)
54
				parallelQueries[i] = ((IMultiQuery) queries[i]).getCompoundingQuery();
55
			else
56
				parallelQueries[i] = new MatchAllQuery();
57
		}
58
		return new CompositeQuery(parallelQueries);
59
	}
60
48
}
61
}
(-)src/org/eclipse/equinox/internal/provisional/p2/query/MatchAllQuery.java (+20 lines)
Added Link Here
1
/******************************************************************************* 
2
* Copyright (c) 2009 EclipseSource and others. All rights reserved. This
3
* program and the accompanying materials are made available under the terms of
4
* the Eclipse Public License v1.0 which accompanies this distribution, and is
5
* available at http://www.eclipse.org/legal/epl-v10.html
6
*
7
* Contributors:
8
*   EclipseSource - initial API and implementation
9
******************************************************************************/
10
package org.eclipse.equinox.internal.provisional.p2.query;
11
12
/**
13
 * A query that matches all elements.
14
 */
15
public class MatchAllQuery extends MatchQuery {
16
17
	public boolean isMatch(Object candidate) {
18
		return true;
19
	}
20
}
(-)src/org/eclipse/equinox/p2/tests/core/CompoundQueryableTest.java (+113 lines)
Lines 14-19 Link Here
14
import org.eclipse.core.runtime.IProgressMonitor;
14
import org.eclipse.core.runtime.IProgressMonitor;
15
import org.eclipse.core.tests.harness.TestProgressMonitor;
15
import org.eclipse.core.tests.harness.TestProgressMonitor;
16
import org.eclipse.equinox.internal.provisional.p2.query.*;
16
import org.eclipse.equinox.internal.provisional.p2.query.*;
17
import org.eclipse.equinox.internal.provisional.p2.query.CompoundQueryable.IMultiQuery;
17
18
18
/**
19
/**
19
 * Tests the compound queryable
20
 * Tests the compound queryable
Lines 324-327 Link Here
324
		assertTrue("1.0", monitor.isDone());
325
		assertTrue("1.0", monitor.isDone());
325
		assertTrue("1.1", monitor.isWorkDone());
326
		assertTrue("1.1", monitor.isWorkDone());
326
	}
327
	}
328
329
	class Greatest2ndNumberQuery extends ContextQuery implements IMultiQuery {
330
331
		public Query getCompoundingQuery() {
332
			return new ContextQuery() {
333
334
				public Collector perform(Iterator iterator, Collector result) {
335
					int greatest = Integer.MIN_VALUE;
336
					int second = Integer.MIN_VALUE;
337
					while (iterator.hasNext()) {
338
						int item = ((Integer) iterator.next()).intValue();
339
						if (item > greatest) {
340
							second = greatest;
341
							greatest = item;
342
						} else if (item > second && item < greatest) {
343
							second = item;
344
						}
345
346
					}
347
					if (greatest == Integer.MIN_VALUE)
348
						return result;
349
					result.accept(second);
350
					result.accept(greatest);
351
					return result;
352
				}
353
354
			};
355
356
		}
357
358
		public Collector perform(Iterator iterator, Collector result) {
359
			int greatest = Integer.MIN_VALUE;
360
			int second = Integer.MIN_VALUE;
361
			while (iterator.hasNext()) {
362
				int item = ((Integer) iterator.next()).intValue();
363
				if (item > greatest) {
364
					second = greatest;
365
					greatest = item;
366
				} else if (item > second && item < greatest) {
367
					second = item;
368
				}
369
370
			}
371
			if (greatest == Integer.MIN_VALUE)
372
				return result;
373
			result.accept(second);
374
			return result;
375
		}
376
	}
377
378
	Greatest2ndNumberQuery greatest2ndNumberQuery = new Greatest2ndNumberQuery();
379
380
	public void testCompoundContextOrQuery() {
381
		CompoundQueryable cQueryable = new CompoundQueryable(new IQueryable[] {queryable1, queryable2});
382
		CompoundQueryTestProgressMonitor monitor = new CompoundQueryTestProgressMonitor();
383
		Collector collector = cQueryable.query(CompoundQuery.createCompoundQuery(new Query[] {contextQuery, greatest2ndNumberQuery}, false), new Collector(), monitor);
384
		assertEquals("1.0", 6, collector.size());
385
		Collection collection = collector.toCollection();
386
		assertTrue("1.2", collection.contains(2));
387
		assertTrue("1.2", collection.contains(4));
388
		assertTrue("1.2", collection.contains(6));
389
		assertTrue("1.4", collection.contains(8));
390
		assertTrue("1.2", collection.contains(10));
391
		assertTrue("1.6", collection.contains(12));
392
	}
393
394
	public void testDoubleCompoundContextAndQuery2() {
395
		CompoundQueryable cQueryable1 = new CompoundQueryable(new IQueryable[] {queryable3, queryable2});
396
		CompoundQueryable cQueryable = new CompoundQueryable(new IQueryable[] {cQueryable1, queryable1});
397
		CompoundQueryTestProgressMonitor monitor = new CompoundQueryTestProgressMonitor();
398
		Collector collector = cQueryable.query(CompoundQuery.createCompoundQuery(new Query[] {greatest2ndNumberQuery, greatestNumberQuery}, true), new Collector(), monitor);
399
		assertEquals("1.0", 0, collector.size());
400
		assertTrue("1.0", monitor.isDone());
401
		assertTrue("1.1", monitor.isWorkDone());
402
	}
403
404
	public void testDoubleCompoundContextAndQuery() {
405
		CompoundQueryable cQueryable1 = new CompoundQueryable(new IQueryable[] {queryable3, queryable2});
406
		CompoundQueryable cQueryable = new CompoundQueryable(new IQueryable[] {cQueryable1, queryable1});
407
		CompoundQueryTestProgressMonitor monitor = new CompoundQueryTestProgressMonitor();
408
		Collector collector = cQueryable.query(CompoundQuery.createCompoundQuery(new Query[] {contextQuery, greatestNumberQuery}, true), new Collector(), monitor);
409
		assertEquals("1.0", 0, collector.size());
410
		assertTrue("1.0", monitor.isDone());
411
		assertTrue("1.1", monitor.isWorkDone());
412
	}
413
414
	public void testCompositeQuery() {
415
		CompoundQueryable cQueryable = new CompoundQueryable(new IQueryable[] {queryable1, queryable2});
416
		CompoundQueryTestProgressMonitor monitor = new CompoundQueryTestProgressMonitor();
417
		Collector collector = cQueryable.query(new CompositeQuery(new Query[] {contextQuery, greatest2ndNumberQuery}), new Collector(), monitor);
418
		assertEquals("1.0", 1, collector.size());
419
		Collection collection = collector.toCollection();
420
		assertTrue("1.2", collection.contains(10));
421
	}
422
423
	public void testCompoundContextAndQuery() {
424
		CompoundQueryable cQueryable = new CompoundQueryable(new IQueryable[] {queryable1, queryable2});
425
		CompoundQueryTestProgressMonitor monitor = new CompoundQueryTestProgressMonitor();
426
		Collector collector = cQueryable.query(CompoundQuery.createCompoundQuery(new Query[] {contextQuery, greatest2ndNumberQuery}, true), new Collector(), monitor);
427
		assertEquals("1.0", 1, collector.size());
428
		Collection collection = collector.toCollection();
429
		assertTrue("1.2", collection.contains(10));
430
	}
431
432
	public void testMultipleContextQueries2() {
433
		CompoundQueryable cQueryable = new CompoundQueryable(new IQueryable[] {queryable1, queryable2});
434
		CompoundQueryTestProgressMonitor monitor = new CompoundQueryTestProgressMonitor();
435
		Collector collector = cQueryable.query(greatest2ndNumberQuery, new Collector(), monitor);
436
		assertEquals("1.0", 1, collector.size());
437
		Collection collection = collector.toCollection();
438
		assertTrue("1.1", collection.contains(10));
439
	}
327
}
440
}

Return to bug 261758