use of com.google.common.collect.ImmutableListMultimap in project buck by facebook.
the class IjModuleGraph method createModules.
/**
* Create all the modules we are capable of representing in IntelliJ from the supplied graph.
*
* @param targetGraph graph whose nodes will be converted to {@link IjModule}s.
* @return map which for every BuildTarget points to the corresponding IjModule. Multiple
* BuildTarget can point to one IjModule (many:one mapping), the BuildTargets which
* can't be prepresented in IntelliJ are missing from this mapping.
*/
private static ImmutableMap<BuildTarget, IjModule> createModules(IjProjectConfig projectConfig, TargetGraph targetGraph, IjModuleFactory moduleFactory, final int minimumPathDepth) {
final BlockedPathNode blockedPathTree = createAggregationHaltPoints(projectConfig, targetGraph);
ImmutableListMultimap<Path, TargetNode<?, ?>> baseTargetPathMultimap = targetGraph.getNodes().stream().filter(input -> IjModuleFactory.SUPPORTED_MODULE_DESCRIPTION_CLASSES.contains(input.getDescription().getClass())).collect(MoreCollectors.toImmutableListMultimap(targetNode -> {
Path path;
Path basePath = targetNode.getBuildTarget().getBasePath();
if (targetNode.getConstructorArg() instanceof AndroidResourceDescription.Arg) {
path = basePath;
} else {
path = simplifyPath(basePath, minimumPathDepth, blockedPathTree);
}
return path;
}, targetNode -> targetNode));
ImmutableMap.Builder<BuildTarget, IjModule> moduleMapBuilder = new ImmutableMap.Builder<>();
for (Path baseTargetPath : baseTargetPathMultimap.keySet()) {
ImmutableSet<TargetNode<?, ?>> targets = ImmutableSet.copyOf(baseTargetPathMultimap.get(baseTargetPath));
IjModule module = moduleFactory.createModule(baseTargetPath, targets);
for (TargetNode<?, ?> target : targets) {
moduleMapBuilder.put(target.getBuildTarget(), module);
}
}
return moduleMapBuilder.build();
}
use of com.google.common.collect.ImmutableListMultimap in project immutables by immutables.
the class SourceOrdering method getAllAccessorsProvider.
/**
* While we have {@link SourceOrdering}, there's still a problem: We have inheritance hierarchy
* and
* we want to have all defined or inherited accessors returned as members of target type, like
* {@link Elements#getAllMembers(TypeElement)}, but we need to have them properly and stably
* sorted.
* This implementation doesn't try to correctly resolve order for accessors inherited from
* different supertypes(interfaces), just something that stable and reasonable wrt source ordering
* without handling complex cases.
* @param elements the elements utility
* @param types the types utility
* @param type the type to traverse
* @return provider of all accessors in source order and mapping
*/
public static AccessorProvider getAllAccessorsProvider(final Elements elements, final Types types, final TypeElement type) {
class CollectedOrdering extends Ordering<Element> {
class Intratype {
Ordering<String> ordering;
int rank;
}
final Map<String, Intratype> accessorOrderings = Maps.newLinkedHashMap();
final List<TypeElement> linearizedTypes = Lists.newArrayList();
final Predicate<String> accessorNotYetInOrderings = Predicates.not(Predicates.in(accessorOrderings.keySet()));
final ArrayListMultimap<String, TypeElement> accessorMapping = ArrayListMultimap.create();
CollectedOrdering() {
traverse(type);
traverseObjectForInterface();
}
private void traverseObjectForInterface() {
if (type.getKind() == ElementKind.INTERFACE) {
traverse(elements.getTypeElement(Object.class.getName()));
}
}
void traverse(@Nullable TypeElement element) {
if (element == null || isJavaLangObject(element)) {
return;
}
collectEnclosing(element);
traverse(asTypeElement(element.getSuperclass()));
for (TypeMirror implementedInterface : element.getInterfaces()) {
traverse(asTypeElement(implementedInterface));
}
}
@Nullable
TypeElement asTypeElement(TypeMirror type) {
if (type.getKind() == TypeKind.DECLARED) {
return (TypeElement) ((DeclaredType) type).asElement();
}
return null;
}
void collectEnclosing(TypeElement type) {
FluentIterable<String> accessorsInType = FluentIterable.from(SourceOrdering.getEnclosedElements(type)).filter(IsParameterlessNonstaticNonobject.PREDICATE).transform(ToSimpleName.FUNCTION);
for (String accessor : accessorsInType) {
accessorMapping.put(accessor, type);
}
List<String> accessors = accessorsInType.filter(accessorNotYetInOrderings).toList();
Intratype intratype = new Intratype();
intratype.rank = linearizedTypes.size();
intratype.ordering = Ordering.explicit(accessors);
for (String name : accessors) {
accessorOrderings.put(name, intratype);
}
linearizedTypes.add(type);
}
@Override
public int compare(Element left, Element right) {
String leftKey = ToSimpleName.FUNCTION.apply(left);
String rightKey = ToSimpleName.FUNCTION.apply(right);
Intratype leftIntratype = accessorOrderings.get(leftKey);
Intratype rightIntratype = accessorOrderings.get(rightKey);
if (leftIntratype == null || rightIntratype == null) {
// FIXME figure out why it happens (null)
return Boolean.compare(leftIntratype == null, rightIntratype == null);
}
return leftIntratype == rightIntratype ? leftIntratype.ordering.compare(leftKey, rightKey) : Integer.compare(leftIntratype.rank, rightIntratype.rank);
}
}
final CollectedOrdering ordering = new CollectedOrdering();
final ImmutableList<ExecutableElement> sortedList = ordering.immutableSortedCopy(disambiguateMethods(ElementFilter.methodsIn(elements.getAllMembers(type))));
return new AccessorProvider() {
ImmutableListMultimap<String, TypeElement> accessorMapping = ImmutableListMultimap.copyOf(ordering.accessorMapping);
@Override
public ImmutableListMultimap<String, TypeElement> accessorMapping() {
return accessorMapping;
}
@Override
public ImmutableList<ExecutableElement> get() {
return sortedList;
}
};
}
use of com.google.common.collect.ImmutableListMultimap in project presto by prestodb.
the class InCodeGenerator method generateExpression.
@Override
public BytecodeNode generateExpression(Signature signature, BytecodeGeneratorContext generatorContext, Type returnType, List<RowExpression> arguments) {
BytecodeNode value = generatorContext.generate(arguments.get(0));
List<RowExpression> values = arguments.subList(1, arguments.size());
ImmutableList.Builder<BytecodeNode> valuesBytecode = ImmutableList.builder();
for (int i = 1; i < arguments.size(); i++) {
BytecodeNode testNode = generatorContext.generate(arguments.get(i));
valuesBytecode.add(testNode);
}
Type type = arguments.get(0).getType();
Class<?> javaType = type.getJavaType();
SwitchGenerationCase switchGenerationCase = checkSwitchGenerationCase(type, values);
Signature hashCodeSignature = internalOperator(HASH_CODE, BIGINT, ImmutableList.of(type));
MethodHandle hashCodeFunction = generatorContext.getRegistry().getScalarFunctionImplementation(hashCodeSignature).getMethodHandle();
ImmutableListMultimap.Builder<Integer, BytecodeNode> hashBucketsBuilder = ImmutableListMultimap.builder();
ImmutableList.Builder<BytecodeNode> defaultBucket = ImmutableList.builder();
ImmutableSet.Builder<Object> constantValuesBuilder = ImmutableSet.builder();
for (RowExpression testValue : values) {
BytecodeNode testBytecode = generatorContext.generate(testValue);
if (testValue instanceof ConstantExpression && ((ConstantExpression) testValue).getValue() != null) {
ConstantExpression constant = (ConstantExpression) testValue;
Object object = constant.getValue();
switch(switchGenerationCase) {
case DIRECT_SWITCH:
case SET_CONTAINS:
constantValuesBuilder.add(object);
break;
case HASH_SWITCH:
try {
int hashCode = toIntExact(Long.hashCode((Long) hashCodeFunction.invoke(object)));
hashBucketsBuilder.put(hashCode, testBytecode);
} catch (Throwable throwable) {
throw new IllegalArgumentException("Error processing IN statement: error calculating hash code for " + object, throwable);
}
break;
default:
throw new IllegalArgumentException("Not supported switch generation case: " + switchGenerationCase);
}
} else {
defaultBucket.add(testBytecode);
}
}
ImmutableListMultimap<Integer, BytecodeNode> hashBuckets = hashBucketsBuilder.build();
ImmutableSet<Object> constantValues = constantValuesBuilder.build();
LabelNode end = new LabelNode("end");
LabelNode match = new LabelNode("match");
LabelNode noMatch = new LabelNode("noMatch");
LabelNode defaultLabel = new LabelNode("default");
Scope scope = generatorContext.getScope();
BytecodeNode switchBlock;
BytecodeBlock switchCaseBlocks = new BytecodeBlock();
LookupSwitch.LookupSwitchBuilder switchBuilder = lookupSwitchBuilder();
switch(switchGenerationCase) {
case DIRECT_SWITCH:
// For these types, it's safe to not use presto HASH_CODE and EQUAL operator.
for (Object constantValue : constantValues) {
switchBuilder.addCase(toIntExact((Long) constantValue), match);
}
switchBuilder.defaultCase(defaultLabel);
switchBlock = new BytecodeBlock().comment("lookupSwitch(<stackValue>))").dup(javaType).append(new IfStatement().condition(new BytecodeBlock().dup(javaType).invokeStatic(InCodeGenerator.class, "isInteger", boolean.class, long.class)).ifFalse(new BytecodeBlock().pop(javaType).gotoLabel(defaultLabel))).longToInt().append(switchBuilder.build());
break;
case HASH_SWITCH:
for (Map.Entry<Integer, Collection<BytecodeNode>> bucket : hashBuckets.asMap().entrySet()) {
LabelNode label = new LabelNode("inHash" + bucket.getKey());
switchBuilder.addCase(bucket.getKey(), label);
Collection<BytecodeNode> testValues = bucket.getValue();
BytecodeBlock caseBlock = buildInCase(generatorContext, scope, type, label, match, defaultLabel, testValues, false);
switchCaseBlocks.append(caseBlock.setDescription("case " + bucket.getKey()));
}
switchBuilder.defaultCase(defaultLabel);
Binding hashCodeBinding = generatorContext.getCallSiteBinder().bind(hashCodeFunction);
switchBlock = new BytecodeBlock().comment("lookupSwitch(hashCode(<stackValue>))").dup(javaType).append(invoke(hashCodeBinding, hashCodeSignature)).invokeStatic(Long.class, "hashCode", int.class, long.class).append(switchBuilder.build()).append(switchCaseBlocks);
break;
case SET_CONTAINS:
Set<?> constantValuesSet = toFastutilHashSet(constantValues, type, registry);
Binding constant = generatorContext.getCallSiteBinder().bind(constantValuesSet, constantValuesSet.getClass());
switchBlock = new BytecodeBlock().comment("inListSet.contains(<stackValue>)").append(new IfStatement().condition(new BytecodeBlock().comment("value").dup(javaType).comment("set").append(loadConstant(constant)).invokeStatic(FastutilSetHelper.class, "in", boolean.class, javaType.isPrimitive() ? javaType : Object.class, constantValuesSet.getClass())).ifTrue(jump(match)));
break;
default:
throw new IllegalArgumentException("Not supported switch generation case: " + switchGenerationCase);
}
BytecodeBlock defaultCaseBlock = buildInCase(generatorContext, scope, type, defaultLabel, match, noMatch, defaultBucket.build(), true).setDescription("default");
BytecodeBlock block = new BytecodeBlock().comment("IN").append(value).append(ifWasNullPopAndGoto(scope, end, boolean.class, javaType)).append(switchBlock).append(defaultCaseBlock);
BytecodeBlock matchBlock = new BytecodeBlock().setDescription("match").visitLabel(match).pop(javaType).append(generatorContext.wasNull().set(constantFalse())).push(true).gotoLabel(end);
block.append(matchBlock);
BytecodeBlock noMatchBlock = new BytecodeBlock().setDescription("noMatch").visitLabel(noMatch).pop(javaType).push(false).gotoLabel(end);
block.append(noMatchBlock);
block.visitLabel(end);
return block;
}
use of com.google.common.collect.ImmutableListMultimap in project wombat by PLOS.
the class TemplateModelUtil method getAsMultimap.
static ImmutableListMultimap<String, TemplateModel> getAsMultimap(TemplateModel value) throws TemplateModelException {
if (value == null)
return ImmutableListMultimap.of();
if (value instanceof TemplateHashModelEx) {
TemplateHashModelEx ftlHash = (TemplateHashModelEx) value;
ImmutableListMultimap.Builder<String, TemplateModel> builder = ImmutableListMultimap.builder();
for (TemplateModelIterator iterator = ftlHash.keys().iterator(); iterator.hasNext(); ) {
String key = iterator.next().toString();
TemplateModel model = ftlHash.get(key);
if (model instanceof TemplateSequenceModel) {
TemplateSequenceModel sequenceModel = (TemplateSequenceModel) model;
int size = sequenceModel.size();
for (int i = 0; i < size; i++) {
builder.put(key, sequenceModel.get(i));
}
} else {
builder.put(key, model);
}
}
return builder.build();
}
throw new TemplateModelException("Hash type expected");
}
use of com.google.common.collect.ImmutableListMultimap in project LanternServer by LanternPowered.
the class LanternChunkManager method loadTickets.
public void loadTickets() throws IOException {
final Multimap<String, LanternLoadingTicket> tickets = LanternLoadingTicketIO.load(this.worldFolder, this, this.chunkLoadService);
final Iterator<Entry<String, LanternLoadingTicket>> it = tickets.entries().iterator();
final CauseStack causeStack = CauseStack.current();
while (it.hasNext()) {
final LanternLoadingTicket ticket = it.next().getValue();
if (ticket instanceof LanternEntityLoadingTicket) {
final LanternEntityLoadingTicket ticket0 = (LanternEntityLoadingTicket) ticket;
final EntityReference ref = ticket0.getEntityReference().orElse(null);
if (ref != null) {
causeStack.pushCause(ticket0);
final LanternChunk chunk = getOrCreateChunk(ref.getChunkCoords(), causeStack, true, true);
causeStack.popCause();
final Entity entity = chunk.getEntity(ref.getUniqueId()).orElse(null);
if (entity != null) {
ticket0.bindToEntity(entity);
} else {
// The entity is gone?
it.remove();
}
} else {
// The entity is gone?
it.remove();
}
}
}
for (Entry<String, Collection<LanternLoadingTicket>> entry : tickets.asMap().entrySet()) {
final Collection<ChunkTicketManager.Callback> callbacks = this.chunkLoadService.getCallbacks().get(entry.getKey());
// These maps will be loaded lazily
ImmutableListMultimap<UUID, LoadingTicket> playerLoadedTickets = null;
ImmutableList<LoadingTicket> nonPlayerLoadedTickets = null;
final Set<LoadingTicket> resultPlayerLoadedTickets = entry.getValue().stream().filter(ticket -> ticket instanceof PlayerLoadingTicket).collect(Collectors.toSet());
final Set<LoadingTicket> resultNonPlayerLoadedTickets = entry.getValue().stream().filter(ticket -> !(ticket instanceof PlayerLoadingTicket)).collect(Collectors.toSet());
final int maxTickets = this.chunkLoadService.getMaxTicketsById(entry.getKey());
for (ChunkTicketManager.Callback callback : callbacks) {
if (callback instanceof ChunkTicketManager.OrderedCallback) {
if (nonPlayerLoadedTickets == null) {
nonPlayerLoadedTickets = ImmutableList.copyOf(resultNonPlayerLoadedTickets);
resultNonPlayerLoadedTickets.clear();
}
final List<LoadingTicket> result = ((ChunkTicketManager.OrderedCallback) callback).onLoaded(nonPlayerLoadedTickets, this.world, maxTickets);
checkNotNull(result, "The OrderedCallback#onLoaded method may not return null, " + "error caused by (plugin=%s, clazz=%s)", entry.getKey(), callback.getClass().getName());
resultNonPlayerLoadedTickets.addAll(result);
}
if (callback instanceof ChunkTicketManager.PlayerOrderedCallback) {
if (playerLoadedTickets == null) {
final ImmutableListMultimap.Builder<UUID, LoadingTicket> mapBuilder = ImmutableListMultimap.builder();
resultPlayerLoadedTickets.forEach(ticket -> mapBuilder.put(((PlayerLoadingTicket) ticket).getPlayerUniqueId(), ticket));
resultPlayerLoadedTickets.clear();
playerLoadedTickets = mapBuilder.build();
}
final ListMultimap<UUID, LoadingTicket> result = ((ChunkTicketManager.PlayerOrderedCallback) callback).onPlayerLoaded(playerLoadedTickets, this.world);
checkNotNull(result, "The PlayerOrderedCallback#onPlayerLoaded method may not return null, " + "error caused by (plugin=%s, clazz=%s)", entry.getKey(), callback.getClass().getName());
resultPlayerLoadedTickets.addAll(result.values());
}
}
final List<LoadingTicket> resultLoadedTickets = new ArrayList<>();
resultLoadedTickets.addAll(resultPlayerLoadedTickets);
resultLoadedTickets.addAll(resultNonPlayerLoadedTickets);
// Lets see how many plugins attempted to add loading tickets
final int sizeA = resultLoadedTickets.size();
resultLoadedTickets.retainAll(entry.getValue());
final int sizeB = resultLoadedTickets.size();
if (sizeA != sizeB) {
Lantern.getLogger().warn("The plugin {} attempted to add LoadingTicket's that were previously not present.", entry.getKey());
}
// Remove all the tickets that are already released
resultLoadedTickets.removeIf(ticket -> ((ChunkLoadingTicket) ticket).isReleased());
if (resultLoadedTickets.size() > maxTickets) {
Lantern.getLogger().warn("The plugin {} has too many open chunk loading tickets {}. " + "Excess will be dropped", entry.getKey(), resultLoadedTickets.size());
resultLoadedTickets.subList(maxTickets, resultLoadedTickets.size()).clear();
}
// Release all the tickets that were no longer usable
final List<LoadingTicket> removedTickets = new ArrayList<>(entry.getValue());
removedTickets.removeAll(resultLoadedTickets);
removedTickets.forEach(LoadingTicket::release);
final ImmutableList<LoadingTicket> loadedTickets = ImmutableList.copyOf(resultLoadedTickets);
for (ChunkTicketManager.Callback callback : callbacks) {
callback.onLoaded(loadedTickets, this.world);
}
}
}
Aggregations