use of com.google.common.collect.ImmutableSetMultimap in project buck by facebook.
the class DefaultClassUsageFileWriter method relativizeMap.
private static ImmutableSetMultimap<Path, Path> relativizeMap(ImmutableSetMultimap<Path, Path> classUsageMap, ProjectFilesystem filesystem) {
final ImmutableSetMultimap.Builder<Path, Path> builder = ImmutableSetMultimap.builder();
// Ensure deterministic ordering.
builder.orderKeysBy(Comparator.naturalOrder());
builder.orderValuesBy(Comparator.naturalOrder());
for (Map.Entry<Path, Collection<Path>> jarClassesEntry : classUsageMap.asMap().entrySet()) {
Path jarAbsolutePath = jarClassesEntry.getKey();
Optional<Path> jarRelativePath = filesystem.getPathRelativeToProjectRoot(jarAbsolutePath);
// Don't include jars that are outside of the filesystem
if (!jarRelativePath.isPresent()) {
// outside the project are coming from a build tool (e.g. JDK or Android SDK).
continue;
}
builder.putAll(jarRelativePath.get(), jarClassesEntry.getValue());
}
return builder.build();
}
use of com.google.common.collect.ImmutableSetMultimap in project bazel by bazelbuild.
the class ProtobufSupport method getInputsToOutputsMap.
private ImmutableSetMultimap<ImmutableSet<Artifact>, Artifact> getInputsToOutputsMap() {
Iterable<ObjcProtoProvider> objcProtoProviders = getObjcProtoProviders();
Iterable<ProtoSourcesProvider> protoProviders = getProtoSourcesProviders();
ImmutableList.Builder<NestedSet<Artifact>> protoSets = new ImmutableList.Builder<NestedSet<Artifact>>();
// all the transitive groups of proto.
for (ObjcProtoProvider objcProtoProvider : objcProtoProviders) {
protoSets.addAll(objcProtoProvider.getProtoGroups());
}
for (ProtoSourcesProvider protoProvider : protoProviders) {
protoSets.add(protoProvider.getTransitiveProtoSources());
}
HashMap<Artifact, Set<Artifact>> protoToGroupMap = new HashMap<>();
// group will be considered the smallest input group with which the proto can be generated.
for (NestedSet<Artifact> nestedProtoSet : protoSets.build()) {
ImmutableSet<Artifact> protoSet = ImmutableSet.copyOf(nestedProtoSet.toSet());
for (Artifact proto : protoSet) {
// generated with the runtime library.
if (attributes.isProtoWellKnown(proto)) {
continue;
}
if (!protoToGroupMap.containsKey(proto)) {
protoToGroupMap.put(proto, protoSet);
} else {
protoToGroupMap.put(proto, Sets.intersection(protoSet, protoToGroupMap.get(proto)));
}
}
}
// Now that we have the smallest proto inputs groups for each proto to be generated, we reverse
// that map into a multimap to take advantage of the fact that multiple protos can be generated
// with the same inputs, to avoid having multiple generation actions with the same inputs and
// different ouputs. This only applies for the generation actions, as the compilation actions
// compile one generated file at a time.
// It's OK to use ImmutableSet<Artifact> as the key, since Artifact caches it's hashCode, and
// ImmutableSet calculates it's hashCode in O(n).
ImmutableSetMultimap.Builder<ImmutableSet<Artifact>, Artifact> inputsToOutputsMapBuilder = ImmutableSetMultimap.builder();
for (Artifact proto : protoToGroupMap.keySet()) {
inputsToOutputsMapBuilder.put(ImmutableSet.copyOf(protoToGroupMap.get(proto)), proto);
}
return inputsToOutputsMapBuilder.build();
}
use of com.google.common.collect.ImmutableSetMultimap in project auto by google.
the class BasicAnnotationProcessor method validElements.
/**
* Returns the valid annotated elements contained in all of the deferred elements. If none are
* found for a deferred element, defers it again.
*/
private ImmutableSetMultimap<Class<? extends Annotation>, Element> validElements(ImmutableMap<String, Optional<? extends Element>> deferredElements, RoundEnvironment roundEnv) {
ImmutableSetMultimap.Builder<Class<? extends Annotation>, Element> deferredElementsByAnnotationBuilder = ImmutableSetMultimap.builder();
for (Entry<String, Optional<? extends Element>> deferredTypeElementEntry : deferredElements.entrySet()) {
Optional<? extends Element> deferredElement = deferredTypeElementEntry.getValue();
if (deferredElement.isPresent()) {
findAnnotatedElements(deferredElement.get(), getSupportedAnnotationClasses(), deferredElementsByAnnotationBuilder);
} else {
deferredElementNames.add(ElementName.forTypeName(deferredTypeElementEntry.getKey()));
}
}
ImmutableSetMultimap<Class<? extends Annotation>, Element> deferredElementsByAnnotation = deferredElementsByAnnotationBuilder.build();
ImmutableSetMultimap.Builder<Class<? extends Annotation>, Element> validElements = ImmutableSetMultimap.builder();
Set<ElementName> validElementNames = new LinkedHashSet<ElementName>();
// Look at the elements we've found and the new elements from this round and validate them.
for (Class<? extends Annotation> annotationClass : getSupportedAnnotationClasses()) {
// This should just call roundEnv.getElementsAnnotatedWith(Class) directly, but there is a bug
// in some versions of eclipse that cause that method to crash.
TypeElement annotationType = elements.getTypeElement(annotationClass.getCanonicalName());
Set<? extends Element> elementsAnnotatedWith = (annotationType == null) ? ImmutableSet.<Element>of() : roundEnv.getElementsAnnotatedWith(annotationType);
for (Element annotatedElement : Sets.union(elementsAnnotatedWith, deferredElementsByAnnotation.get(annotationClass))) {
if (annotatedElement.getKind().equals(PACKAGE)) {
PackageElement annotatedPackageElement = (PackageElement) annotatedElement;
ElementName annotatedPackageName = ElementName.forPackageName(annotatedPackageElement.getQualifiedName().toString());
boolean validPackage = validElementNames.contains(annotatedPackageName) || (!deferredElementNames.contains(annotatedPackageName) && validateElement(annotatedPackageElement));
if (validPackage) {
validElements.put(annotationClass, annotatedPackageElement);
validElementNames.add(annotatedPackageName);
} else {
deferredElementNames.add(annotatedPackageName);
}
} else {
TypeElement enclosingType = getEnclosingType(annotatedElement);
ElementName enclosingTypeName = ElementName.forTypeName(enclosingType.getQualifiedName().toString());
boolean validEnclosingType = validElementNames.contains(enclosingTypeName) || (!deferredElementNames.contains(enclosingTypeName) && validateElement(enclosingType));
if (validEnclosingType) {
validElements.put(annotationClass, annotatedElement);
validElementNames.add(enclosingTypeName);
} else {
deferredElementNames.add(enclosingTypeName);
}
}
}
}
return validElements.build();
}
use of com.google.common.collect.ImmutableSetMultimap in project wikidata-query-rdf by wikimedia.
the class RdfRepository method resultToMap.
/**
* Collect results of the query into a multimap by first parameter.
*
* @param result Result object
* @param keyBinding Binding name to serve as key
* @param valueBinding Binding name to serve as values
* @return Collection of strings resulting from the query.
*/
private ImmutableSetMultimap<String, String> resultToMap(TupleQueryResult result, String keyBinding, String valueBinding) {
ImmutableSetMultimap.Builder<String, String> values = ImmutableSetMultimap.builder();
try {
while (result.hasNext()) {
BindingSet bindings = result.next();
Binding value = bindings.getBinding(valueBinding);
Binding key = bindings.getBinding(keyBinding);
if (value == null || key == null) {
continue;
}
values.put(key.getValue().stringValue(), value.getValue().stringValue());
}
} catch (QueryEvaluationException e) {
throw new FatalException("Can't load results: " + e, e);
}
return values.build();
}
use of com.google.common.collect.ImmutableSetMultimap in project LanternServer by LanternPowered.
the class LanternChunkManager method getForced.
/**
* Gets a map with all the forced chunk and the assigned tickets.
*
* @return the tickets
*/
public ImmutableSetMultimap<Vector3i, LoadingTicket> getForced() {
final ImmutableSetMultimap.Builder<Vector3i, LoadingTicket> builder = ImmutableSetMultimap.builder();
for (Entry<Vector2i, Set<ChunkLoadingTicket>> en : this.ticketsByPos.entrySet()) {
final Vector2i pos0 = en.getKey();
final Vector3i pos = new Vector3i(pos0.getX(), 0, pos0.getY());
for (ChunkLoadingTicket ticket : en.getValue()) {
builder.put(pos, ticket);
}
}
return builder.build();
}
Aggregations