Debian Codegraph @

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

raw
1 /*******************************************************************************
2 * Copyright (c) 2010, 2018 Cloudsmith Inc. 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 * Cloudsmith Inc. - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.equinox.p2.query;
15
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashSet;
19 import java.util.Iterator;
20 import java.util.Set;
21 import java.util.stream.Stream;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.equinox.internal.p2.metadata.expression.QueryResult;
24
25 /**
26 * This class allows to adapt java collections to a p2 a query result and as
27 * such something queryable
28 *
29 * @since 2.0
30 */
31 public class CollectionResult<T> implements IQueryResult<T> {
32 private final Collection<T> collection;
33
34 public CollectionResult(Collection<T> collection) {
35 this.collection = collection == null ? Collections.emptySet() : collection;
36 }
37
38 @Override
39 public IQueryResult<T> query(IQuery<T> query, IProgressMonitor monitor) {
40 return query.perform(iterator());
41 }
42
43 @Override
44 public boolean isEmpty() {
45 return collection.isEmpty();
46 }
47
48 @Override
49 public Iterator<T> iterator() {
50 return collection.iterator();
51 }
52
53 @Override
54 public T[] toArray(Class<T> clazz) {
55 return QueryResult.toArray(collection, clazz);
56 }
57
58 @Override
59 public Set<T> toSet() {
60 return new HashSet<>(collection);
61 }
62
63 @Override
64 public Set<T> toUnmodifiableSet() {
65 return collection instanceof Set<?> ? Collections.unmodifiableSet((Set<T>) collection) : toSet();
66 }
67
68 @Override
69 public String toString() {
70 return collection.toString();
71 }
72
73 @Override
74 public Stream<T> stream() {
75 return collection.stream();
76 }
77 }