) EMPTY_COLLECTOR;
}
- /**
- * Creates a new collector.
- */
- public Collector() {
- super();
- }
-
/**
* Accepts an object.
*
@@ -96,8 +90,9 @@ public void addAll(IQueryResult queryResult) {
* @return the collection being used to collect results.
*/
protected Collection getCollection() {
- if (collected == null)
+ if (collected == null) {
collected = new HashSet<>();
+ }
return collected;
}
@@ -119,7 +114,7 @@ public boolean isEmpty() {
*/
@Override
public Iterator iterator() {
- return collected == null ? Collections.emptyList().iterator() : collected.iterator();
+ return collected == null ? Collections.emptyIterator() : collected.iterator();
}
/**
@@ -140,18 +135,13 @@ public int size() {
*/
@Override
public T[] toArray(Class clazz) {
- int size = collected == null ? 0 : collected.size();
- @SuppressWarnings("unchecked")
- T[] result = (T[]) Array.newInstance(clazz, size);
- if (size != 0)
- collected.toArray(result);
- return result;
+ return QueryResult.toArray(collected, clazz);
}
/**
* Returns a copy of the collected objects.
*
- * @return An unmodifiable collection of the collected objects
+ * @return An modifiable collection of the collected objects
*/
@Override
public Set toSet() {
@@ -164,8 +154,9 @@ public Set toSet() {
@Override
public IQueryResult query(IQuery query, IProgressMonitor monitor) {
IQueryResult result;
- if (monitor == null)
+ if (monitor == null) {
monitor = new NullProgressMonitor();
+ }
try {
monitor.beginTask(Messages.performing_subquery, 1);
result = query.perform(iterator());
@@ -188,4 +179,9 @@ public Set toUnmodifiableSet() {
}
return Collections.unmodifiableSet(collected);
}
+
+ @Override
+ public Stream stream() {
+ return collected == null ? Stream.empty() : collected.stream();
+ }
}
diff --git a/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/query/IQueryResult.java b/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/query/IQueryResult.java
index 7260a15841..1faf8a7d1f 100644
--- a/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/query/IQueryResult.java
+++ b/bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/query/IQueryResult.java
@@ -14,21 +14,25 @@
******************************************************************************/
package org.eclipse.equinox.p2.query;
+import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
/**
- * An IQueryResult represents the results of a query.
+ * An IQueryResult represents the results of a query.
*
* @since 2.0
*/
public interface IQueryResult extends IQueryable, Iterable {
/**
- * Returns whether this QueryResult is empty.
- * @return true
if this QueryResult has accepted any results,
- * and false
otherwise.
+ * Returns whether this QueryResult is empty.
+ *
+ * @return true
if this QueryResult has accepted any results, and
+ * false
otherwise.
*/
- public boolean isEmpty();
+ boolean isEmpty();
/**
* Returns an iterator on the collected objects.
@@ -36,7 +40,7 @@ public interface IQueryResult extends IQueryable, Iterable {
* @return an iterator of the collected objects.
*/
@Override
- public Iterator iterator();
+ Iterator iterator();
/**
* Returns the collected objects as an array
@@ -46,18 +50,34 @@ public interface IQueryResult extends IQueryable, Iterable {
* @throws ArrayStoreException the runtime type of the specified array is
* not a super-type of the runtime type of every collected object
*/
- public T[] toArray(Class clazz);
+ T[] toArray(Class clazz);
/**
- * Creates a new Set copy with the contents of this query result. The
- * copy can be altered without any side effects on its origin.
+ * Creates a new Set copy with the contents of this query result. The copy can
+ * be altered without any side effects on its origin.
+ *
* @return A detached copy of the result.
*/
- public Set toSet();
+ Set toSet();
/**
* Returns a Set backed by this query result. The set is immutable.
+ *
* @return A Set backed by this query result.
*/
- public Set toUnmodifiableSet();
+ Set toUnmodifiableSet();
+
+ /**
+ * Returns a sequential {@code Stream} of the collected objects.
+ *
+ * @implSpec The default implementation creates a sequential {@code Stream} from
+ * this query-results {@code Spliterator}. Implementations backed by a
+ * {@code Collection} should override this method and call
+ * {@link Collection#stream()}.
+ * @since 2.8
+ */
+ default Stream stream() {
+ return StreamSupport.stream(spliterator(), false);
+ }
+
}