Debian Codegraph @

bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/query/IQueryable.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2007, 2023 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 * Hannes Wellmann - Add IQueryable.contains(T) method and implement overrides where suitable
14 *******************************************************************************/
15 package org.eclipse.equinox.p2.query;
16
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Objects;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.equinox.p2.metadata.expression.ExpressionUtil;
22 import org.eclipse.equinox.p2.metadata.expression.IExpression;
23
24 /**
25 * An IQueryable contains objects, and is able to perform queries on those
26 * objects.
27 * <p>
28 * This interface may be implemented by clients.
29 *
30 * @since 2.0
31 */
32 public interface IQueryable<T> {
33 /**
34 * Performs a query, passing any objects that satisfy the query to the provided
35 * collector.
36 * <p>
37 * This method is long-running; progress and cancellation are provided by the
38 * given progress monitor.
39 * </p>
40 *
41 * @param query The query to perform
42 * @param monitor a progress monitor, or <code>null</code> if progress reporting
43 * is not desired
44 * @return The collector argument
45 */
46 public IQueryResult<T> query(IQuery<T> query, IProgressMonitor monitor);
47
48 /**
49 * Returns true if this queryable contains the given element, else false.
50 *
51 * @param element the element to query for
52 * @return true if the given element is found in this queryable
53 * @since 2.8
54 */
55 default boolean contains(T element) {
56 return !query(new IQuery<>() {
57
58 @Override
59 public IQueryResult<T> perform(Iterator<T> iterator) {
60 while (iterator.hasNext()) {
61 T t = iterator.next();
62 if (Objects.equals(t, element)) {
63 return new CollectionResult<>(List.of(t));
64 }
65 }
66 return new CollectionResult<>(List.of());
67 }
68
69 @Override
70 public IExpression getExpression() {
71 return ExpressionUtil.TRUE_EXPRESSION;
72 }
73 }, null).isEmpty();
74 }
75 }