| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007, 2018 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 |
* EclipseSource - ongoing development |
| 14 |
*******************************************************************************/ |
| 15 |
package org.eclipse.equinox.p2.query; |
| 16 |
|
| 17 |
import java.util.Collection; |
| 18 |
import java.util.Collections; |
| 19 |
import java.util.HashSet; |
| 20 |
import java.util.Iterator; |
| 21 |
import java.util.Set; |
| 22 |
import java.util.stream.Stream; |
| 23 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 24 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 25 |
import org.eclipse.equinox.internal.p2.metadata.Messages; |
| 26 |
import org.eclipse.equinox.internal.p2.metadata.expression.QueryResult; |
| 27 |
|
| 28 |
/** |
| 29 |
* A collector is a generic visitor that collects objects passed to it, and can |
| 30 |
* then express the result of the visit in various forms. The collector can also |
| 31 |
* short-circuit a traversal by returning <code>false</code> from its |
| 32 |
* {@link #accept(Object)} method. |
| 33 |
* <p> |
| 34 |
* This default collector just accepts all objects passed to it. Clients may |
| 35 |
* subclass to perform different processing on the objects passed to it. |
| 36 |
* |
| 37 |
* @param <T> The type of object accepted by this collector |
| 38 |
* @since 2.0 |
| 39 |
*/ |
| 40 |
public class Collector<T> implements IQueryResult<T> { |
| 41 |
private Set<T> collected = null; |
| 42 |
|
| 43 |
public static final Collector<?> EMPTY_COLLECTOR = new Collector<>() { |
| 44 |
@Override |
| 45 |
public boolean accept(Object val) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
@SuppressWarnings("unchecked") |
| 51 |
public static final <T> Collector<T> emptyCollector() { |
| 52 |
return (Collector<T>) EMPTY_COLLECTOR; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Accepts an object. |
| 57 |
* <p> |
| 58 |
* This default implementation adds the objects to a list. Clients may override |
| 59 |
* this method to perform additional filtering, add different objects to the |
| 60 |
* list, short-circuit the traversal, or process the objects directly without |
| 61 |
* collecting them. |
| 62 |
* |
| 63 |
* @param object the object to collect or visit |
| 64 |
* @return <code>true</code> if the traversal should continue, or |
| 65 |
* <code>false</code> to indicate the traversal should stop. |
| 66 |
*/ |
| 67 |
public boolean accept(T object) { |
| 68 |
getCollection().add(object); |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Adds the elements from one collector to this collector |
| 74 |
* |
| 75 |
* @param queryResult The collector from which the elements should be retrieved |
| 76 |
*/ |
| 77 |
public void addAll(IQueryResult<T> queryResult) { |
| 78 |
boolean keepGoing = true; |
| 79 |
for (Iterator<T> iter = queryResult.iterator(); iter.hasNext() && keepGoing;) { |
| 80 |
keepGoing = accept(iter.next()); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns the collection that is being used to collect results. Unlike |
| 86 |
* {@linkplain #toSet()}, this returns the actual modifiable collection that is |
| 87 |
* being used to store results. The return value is only intended to be used |
| 88 |
* within subclasses and should not be exposed outside of a collection class. |
| 89 |
* |
| 90 |
* @return the collection being used to collect results. |
| 91 |
*/ |
| 92 |
protected Collection<T> getCollection() { |
| 93 |
if (collected == null) { |
| 94 |
collected = new HashSet<>(); |
| 95 |
} |
| 96 |
return collected; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns whether this collector is empty. |
| 101 |
* |
| 102 |
* @return <code>true</code> if this collector has accepted any results, and |
| 103 |
* <code>false</code> otherwise. |
| 104 |
*/ |
| 105 |
@Override |
| 106 |
public boolean isEmpty() { |
| 107 |
return collected == null || collected.isEmpty(); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Returns an iterator on the collected objects. |
| 112 |
* |
| 113 |
* @return an iterator of the collected objects. |
| 114 |
*/ |
| 115 |
@Override |
| 116 |
public Iterator<T> iterator() { |
| 117 |
return collected == null ? Collections.emptyIterator() : collected.iterator(); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Returns the number of collected objects. |
| 122 |
*/ |
| 123 |
public int size() { |
| 124 |
return collected == null ? 0 : collected.size(); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns the collected objects as an array |
| 129 |
* |
| 130 |
* @param clazz The type of array to return |
| 131 |
* @return The array of results |
| 132 |
* @throws ArrayStoreException the runtime type of the specified array is not a |
| 133 |
* super-type of the runtime type of every collected |
| 134 |
* object |
| 135 |
*/ |
| 136 |
@Override |
| 137 |
public T[] toArray(Class<T> clazz) { |
| 138 |
return QueryResult.toArray(collected, clazz); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Returns a copy of the collected objects. |
| 143 |
* |
| 144 |
* @return An modifiable collection of the collected objects |
| 145 |
*/ |
| 146 |
@Override |
| 147 |
public Set<T> toSet() { |
| 148 |
return collected == null ? new HashSet<>() : new HashSet<>(collected); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Performs a query on this results of this collector. |
| 153 |
*/ |
| 154 |
@Override |
| 155 |
public IQueryResult<T> query(IQuery<T> query, IProgressMonitor monitor) { |
| 156 |
IQueryResult<T> result; |
| 157 |
if (monitor == null) { |
| 158 |
monitor = new NullProgressMonitor(); |
| 159 |
} |
| 160 |
try { |
| 161 |
monitor.beginTask(Messages.performing_subquery, 1); |
| 162 |
result = query.perform(iterator()); |
| 163 |
monitor.worked(1); |
| 164 |
} finally { |
| 165 |
monitor.done(); |
| 166 |
} |
| 167 |
return result; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Returns the collected objects as an immutable collection. |
| 172 |
* |
| 173 |
* @return An unmodifiable collection of the collected objects |
| 174 |
*/ |
| 175 |
@Override |
| 176 |
public Set<T> toUnmodifiableSet() { |
| 177 |
if (collected == null) { |
| 178 |
return Collections.emptySet(); |
| 179 |
} |
| 180 |
return Collections.unmodifiableSet(collected); |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public Stream<T> stream() { |
| 185 |
return collected == null ? Stream.empty() : collected.stream(); |
| 186 |
} |
| 187 |
} |