Example usage for com.google.common.collect Multimaps index

List of usage examples for com.google.common.collect Multimaps index

Introduction

In this page you can find the example usage for com.google.common.collect Multimaps index.

Prototype

public static <K, V> ImmutableListMultimap<K, V> index(Iterator<V> values, Function<? super V, K> keyFunction) 

Source Link

Document

Creates an index ImmutableListMultimap that contains the results of applying a specified function to each item in an Iterator of values.

Usage

From source file:io.prestosql.plugin.hive.PartitionUpdate.java

public static List<PartitionUpdate> mergePartitionUpdates(Iterable<PartitionUpdate> unMergedUpdates) {
    ImmutableList.Builder<PartitionUpdate> partitionUpdates = ImmutableList.builder();
    for (Collection<PartitionUpdate> partitionGroup : Multimaps.index(unMergedUpdates, PartitionUpdate::getName)
            .asMap().values()) {//w  ww. j a  va 2s.  c  om
        PartitionUpdate firstPartition = partitionGroup.iterator().next();

        ImmutableList.Builder<String> allFileNames = ImmutableList.builder();
        long totalRowCount = 0;
        long totalInMemoryDataSizeInBytes = 0;
        long totalOnDiskDataSizeInBytes = 0;
        for (PartitionUpdate partition : partitionGroup) {
            // verify partitions have the same new flag, write path and target path
            // this shouldn't happen but could if another user added a partition during the write
            if (partition.getUpdateMode() != firstPartition.getUpdateMode()
                    || !partition.getWritePath().equals(firstPartition.getWritePath())
                    || !partition.getTargetPath().equals(firstPartition.getTargetPath())) {
                throw new PrestoException(HIVE_CONCURRENT_MODIFICATION_DETECTED,
                        format("Partition %s was added or modified during INSERT", firstPartition.getName()));
            }
            allFileNames.addAll(partition.getFileNames());
            totalRowCount += partition.getRowCount();
            totalInMemoryDataSizeInBytes += partition.getInMemoryDataSizeInBytes();
            totalOnDiskDataSizeInBytes += partition.getOnDiskDataSizeInBytes();
        }

        partitionUpdates.add(new PartitionUpdate(firstPartition.getName(), firstPartition.getUpdateMode(),
                firstPartition.getWritePath(), firstPartition.getTargetPath(), allFileNames.build(),
                totalRowCount, totalInMemoryDataSizeInBytes, totalOnDiskDataSizeInBytes));
    }
    return partitionUpdates.build();
}

From source file:com.twitter.aurora.scheduler.base.Tasks.java

/**
 * A utility method that returns a multi-map of tasks keyed by IJobKey.
 * @param tasks A list of tasks to be keyed by map
 * @return A multi-map of tasks keyed by job key.
 *//*  w w w.j  a v  a2s  . co m*/
public static Multimap<IJobKey, IScheduledTask> byJobKey(Iterable<IScheduledTask> tasks) {
    return Multimaps.index(tasks, Tasks.SCHEDULED_TO_JOB_KEY);
}

From source file:org.apache.aurora.scheduler.base.Conversions.java

/**
 * Converts protobuf attributes into thrift-generated attributes.
 *
 * @param offer Resource offer.//from  w w  w. j  a v  a  2  s  .  c om
 * @return Equivalent thrift host attributes.
 */
public static IHostAttributes getAttributes(Offer offer) {
    // Group by attribute name.
    Multimap<String, Protos.Attribute> valuesByName = Multimaps.index(offer.getAttributesList(),
            ATTRIBUTE_NAME);

    return IHostAttributes.build(new HostAttributes(offer.getHostname(),
            FluentIterable.from(valuesByName.asMap().entrySet()).transform(ATTRIBUTE_CONVERTER).toSet())
                    .setSlaveId(offer.getAgentId().getValue()));
}

From source file:org.opentestsystem.authoring.testauth.validation.PerformanceLevelValidator.java

@Override
public void validate(final Object obj, final Errors errors) {
    // execute JSR-303 validations (annotations)
    this.jsrValidator.validate(obj, errors);

    final PerformanceLevel performanceLevel = (PerformanceLevel) obj;
    if (!errors.hasErrors() && !CollectionUtils.isEmpty(performanceLevel.getPerformanceLevelValues())) {

        // custom validation for each performanceLevelValue
        for (int i = 0; i < performanceLevel.getPerformanceLevelValues().size(); i++) {
            final PerformanceLevelValue nextValue = performanceLevel.getPerformanceLevelValues().get(i);
            try {
                errors.pushNestedPath(PERFORMANCE_LEVEL_VALUES + "[" + i + "]");
                ValidationUtils.invokeValidator(this.performanceLevelValueValidator, nextValue, errors);

                if (nextValue.getLevel() > performanceLevel.getPerformanceLevelValues().size()) {
                    rejectValue(errors, "level",
                            getErrorMessageRoot() + PERFORMANCE_LEVEL_VALUES_POSITION + MSG_INVALID,
                            nextValue.getLevel());
                }//w ww . j  av a  2  s. c om
            } finally {
                errors.popNestedPath();
            }
        }

        // check for duplicate performanceLevelValue level
        final Map<String, Collection<PerformanceLevelValue>> duplicateLevels = Maps
                .filterEntries(
                        Multimaps.index(performanceLevel.getPerformanceLevelValues(),
                                PERFORMANCE_LEVEL_TO_POSITION_TRANSFORMER).asMap(),
                        PERFORMANCE_LEVEL_DUPLICATE_FILTER);

        if (!duplicateLevels.isEmpty()) {
            rejectValue(errors, PERFORMANCE_LEVEL_VALUES,
                    getErrorMessageRoot() + PERFORMANCE_LEVEL_VALUES_POSITION + MSG_DUPLICATES,
                    duplicateLevels.keySet().toString());
        } else if (!errors.hasErrors()) {
            performanceLevel.setPerformanceLevelValues(
                    Lists.newArrayList(Ordering.natural().onResultOf(PERFORMANCE_LEVEL_TO_POSITION_TRANSFORMER)
                            .sortedCopy(performanceLevel.getPerformanceLevelValues())));
        }
    }
}

From source file:de.unipassau.isl.evs.ssh.master.network.broadcast.UserConfigurationBroadcaster.java

private UserDeviceInformationPayload generateUserDeviceInformationPayload() {
    final PermissionController permissionController = requireComponent(PermissionController.KEY);
    final List<Group> groups;
    final List<UserDevice> userDevices;
    final List<String> templates;
    final List<PermissionDTO> permissions;
    groups = requireComponent(UserManagementController.KEY).getGroups();
    userDevices = requireComponent(UserManagementController.KEY).getUserDevices();
    permissions = requireComponent(PermissionController.KEY).getPermissions();
    templates = requireComponent(PermissionController.KEY).getTemplates();

    ImmutableListMultimap<Group, UserDevice> groupDeviceMapping = Multimaps.index(userDevices,
            new Function<UserDevice, Group>() {
                @Override/*  w ww . j a  va2 s .com*/
                public Group apply(UserDevice input) {
                    for (Group group : groups) {
                        if (group.getName().equals(input.getInGroup())) {
                            return group;
                        }
                    }
                    return null;
                }
            });

    ListMultimap<UserDevice, PermissionDTO> userHasPermissions = ArrayListMultimap.create();
    for (UserDevice userDevice : userDevices) {
        userHasPermissions.putAll(userDevice,
                permissionController.getPermissionsOfUserDevice(userDevice.getUserDeviceID()));
    }

    return new UserDeviceInformationPayload(userHasPermissions, groupDeviceMapping, permissions, groups,
            templates);
}

From source file:edu.harvard.med.screensaver.service.screens.ScreenDerivedPropertiesUpdater.java

private SortedSet<Integer> findMaxReplicatesPerPlateNumber(SortedSet<AssayPlate> assayPlatesScreened) {
    Multimap<Integer, AssayPlate> plateNumbersToAssayPlates = Multimaps.index(assayPlatesScreened,
            AssayPlate.ToPlateNumber);//  w  ww.j ava  2 s . co m
    SortedSet<Integer> maxReplicatesPerPlateNumber = Sets
            .newTreeSet(Iterables.transform(plateNumbersToAssayPlates.asMap().entrySet(),
                    new Function<Map.Entry<Integer, Collection<AssayPlate>>, Integer>() {
                        @Override
                        public Integer apply(Map.Entry<Integer, Collection<AssayPlate>> e) {
                            Iterable<Integer> replicateOrdinals = Iterables.transform(e.getValue(),
                                    AssayPlate.ToReplicateOrdinal);
                            return Ordering.natural().max(replicateOrdinals) + 1;
                        }
                    }));
    return maxReplicatesPerPlateNumber;
}

From source file:org.sonar.server.startup.RegisterNewProfiles.java

private Map<String, Collection<RulesProfile>> groupByName(List<RulesProfile> profiles) {
    return Multimaps.index(profiles, new Function<RulesProfile, String>() {
        public String apply(RulesProfile profile) {
            return profile.getName();
        }/*w  w  w . j a  va2s  . c  o  m*/
    }).asMap();
}

From source file:com.b2international.snowowl.core.compare.CompareResultsDsvExporter.java

public File export(IProgressMonitor monitor) throws IOException {

    CsvMapper mapper = new CsvMapper();
    CsvSchema schema = mapper.schemaFor(CompareData.class).withColumnSeparator(delimiter).withHeader().sortedBy(
            "componentType", "componentId", "componentType", "label", "changeKind", "attribute", "from", "to");

    try (SequenceWriter writer = mapper.writer(schema).writeValues(outputPath.toFile())) {
        monitor.beginTask("Exporting compare results to DSV", compareResults.getTotalNew()
                + compareResults.getTotalChanged() + compareResults.getTotalDeleted());

        ListMultimap<Short, ComponentIdentifier> newComponentIdentifiers = Multimaps
                .index(compareResults.getNewComponents(), ComponentIdentifier::getTerminologyComponentId);
        ListMultimap<Short, String> newComponentIds = ImmutableListMultimap.copyOf(
                Multimaps.transformValues(newComponentIdentifiers, ComponentIdentifier::getComponentId));

        for (short terminologyComponentId : newComponentIds.keySet()) {
            for (List<String> componentIds : Lists.partition(newComponentIds.get(terminologyComponentId),
                    PARTITION_SIZE)) {/*  ww  w  . ja  v  a  2s.c o m*/
                RevisionIndexRequestBuilder<CollectionResource<IComponent>> componentFetchRequest = fetcherFunction
                        .apply(terminologyComponentId, componentIds);

                if (componentFetchRequest == null) {
                    break;
                }

                CollectionResource<IComponent> components = componentFetchRequest
                        .build(repositoryUuid, compareBranch)
                        .execute(ApplicationContext.getServiceForClass(IEventBus.class)).getSync();

                for (IComponent component : components) {
                    writer.write(new CompareData(component, ChangeKind.ADDED));
                }

                monitor.worked(components.getItems().size());
            }
        }

        ListMultimap<Short, ComponentIdentifier> changedComponentIdentifiers = Multimaps
                .index(compareResults.getChangedComponents(), ComponentIdentifier::getTerminologyComponentId);
        ListMultimap<Short, String> changedComponentIds = ImmutableListMultimap.copyOf(
                Multimaps.transformValues(changedComponentIdentifiers, ComponentIdentifier::getComponentId));
        ListMultimap<String, IComponent> componentPairs = ArrayListMultimap.create(PARTITION_SIZE, 2);

        for (short terminologyComponentId : changedComponentIds.keySet()) {
            for (List<String> componentIds : Lists.partition(changedComponentIds.get(terminologyComponentId),
                    PARTITION_SIZE)) {
                componentPairs.clear();
                RevisionIndexRequestBuilder<CollectionResource<IComponent>> componentFetchRequest = fetcherFunction
                        .apply(terminologyComponentId, componentIds);

                if (componentFetchRequest == null) {
                    break;
                }

                componentFetchRequest.build(repositoryUuid, baseBranch)
                        .execute(ApplicationContext.getServiceForClass(IEventBus.class)).getSync()
                        .forEach(c -> componentPairs.put(c.getId(), c));

                componentFetchRequest.build(repositoryUuid, compareBranch)
                        .execute(ApplicationContext.getServiceForClass(IEventBus.class)).getSync()
                        .forEach(c -> componentPairs.put(c.getId(), c));

                for (Entry<String, List<IComponent>> entry : Multimaps.asMap(componentPairs).entrySet()) {
                    IComponent baseComponent = entry.getValue().get(0);
                    IComponent compareComponent = entry.getValue().get(1);
                    Collection<CompareData> compareResults = getCompareResultsOfComponent.apply(baseComponent,
                            compareComponent);

                    for (CompareData d : compareResults) {
                        writer.write(d);
                    }
                }

                monitor.worked(componentPairs.keySet().size());
            }
        }

        ListMultimap<Short, ComponentIdentifier> deletedComponentIdentifiers = Multimaps
                .index(compareResults.getDeletedComponents(), ComponentIdentifier::getTerminologyComponentId);
        ListMultimap<Short, String> deletedComponentIds = ImmutableListMultimap.copyOf(
                Multimaps.transformValues(deletedComponentIdentifiers, ComponentIdentifier::getComponentId));

        for (short terminologyComponentId : deletedComponentIds.keySet()) {
            for (List<String> componentIds : Lists.partition(deletedComponentIds.get(terminologyComponentId),
                    PARTITION_SIZE)) {
                RevisionIndexRequestBuilder<CollectionResource<IComponent>> componentFetchRequest = fetcherFunction
                        .apply(terminologyComponentId, componentIds);

                if (componentFetchRequest == null) {
                    break;
                }

                CollectionResource<IComponent> components = componentFetchRequest
                        .build(repositoryUuid, baseBranch)
                        .execute(ApplicationContext.getServiceForClass(IEventBus.class)).getSync();

                for (IComponent component : components) {
                    writer.write(new CompareData(component, ChangeKind.DELETED));
                }

                monitor.worked(components.getItems().size());
            }
        }

    } finally {
        monitor.done();
    }

    return outputPath.toFile();
}

From source file:com.b2international.snowowl.snomed.datastore.converter.SnomedReferenceSetMemberConverter.java

private void expandTargetComponent(List<SnomedReferenceSetMember> results) {
    if (expand().containsKey(SnomedRf2Headers.FIELD_TARGET_COMPONENT)) {
        final Options expandOptions = expand().get(SnomedRf2Headers.FIELD_TARGET_COMPONENT, Options.class);

        final Multimap<String, SnomedReferenceSetMember> membersByTargetComponent = HashMultimap.create();
        for (SnomedReferenceSetMember member : results) {
            final Map<String, Object> props = member.getProperties();
            if (props.containsKey(SnomedRf2Headers.FIELD_TARGET_COMPONENT)) {
                membersByTargetComponent.put(
                        ((SnomedCoreComponent) props.get(SnomedRf2Headers.FIELD_TARGET_COMPONENT)).getId(),
                        member);/*from w w w.  j  a va 2s.c o m*/
            }
        }

        final Multimap<ComponentCategory, String> targetComponentIdsByCategory = Multimaps
                .index(membersByTargetComponent.keySet(), new Function<String, ComponentCategory>() {
                    @Override
                    public ComponentCategory apply(String id) {
                        return SnomedIdentifiers.getComponentCategory(id);
                    }
                });

        for (ComponentCategory category : targetComponentIdsByCategory.keySet()) {
            final Collection<String> targetComponentIds = targetComponentIdsByCategory.get(category);
            final Map<String, ? extends SnomedCoreComponent> componentsById = Maps.uniqueIndex(
                    getComponents(category, targetComponentIds, expandOptions.get("expand", Options.class)),
                    IComponent.ID_FUNCTION);
            for (String targetComponentId : targetComponentIds) {
                final SnomedCoreComponent targetComponent = componentsById.get(targetComponentId);
                if (targetComponent != null) {
                    for (SnomedReferenceSetMember member : membersByTargetComponent.get(targetComponentId)) {
                        final Map<String, Object> newProps = newHashMap(member.getProperties());
                        newProps.put(SnomedRf2Headers.FIELD_TARGET_COMPONENT, targetComponent);
                        ((SnomedReferenceSetMember) member).setProperties(newProps);
                    }
                }
            }
        }
    }
}

From source file:org.opendaylight.yangtools.yang.model.util.FilteringSchemaContextProxy.java

private static Multimap<String, Module> getStringModuleMap(final SchemaContext delegate) {
    return Multimaps.index(delegate.getModules(), Module::getName);
}