Example usage for com.google.common.collect Maps uniqueIndex

List of usage examples for com.google.common.collect Maps uniqueIndex

Introduction

In this page you can find the example usage for com.google.common.collect Maps uniqueIndex.

Prototype

public static <K, V> ImmutableMap<K, V> uniqueIndex(Iterator<V> values, Function<? super V, K> keyFunction) 

Source Link

Document

Returns a map with the given values , indexed by keys derived from those values.

Usage

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.java

private static Map<String, UpdateOp> createMap(List<UpdateOp> updateOps) {
    return Maps.uniqueIndex(updateOps, new Function<UpdateOp, String>() {
        @Override//w  w  w . j  ava 2s  .c  om
        public String apply(UpdateOp input) {
            return input.getId();
        }
    });
}

From source file:org.apache.impala.catalog.HdfsTable.java

/**
 * Refreshes block metadata information for 'partition'. This method is optimized for
 * the case where the files in the partition have not changed dramatically. It first
 * uses a listStatus() call on the partition directory to detect files with changed
 * mtime and fetches their block locations using the getFileBlockLocations() method.
 * Our benchmarks suggest that the listStatus() call is much faster then the listFiles()
 * (up to ~40x faster in some cases). The initial table load still uses the listFiles()
 * on the data directory that fetches both the FileStatus as well as BlockLocations in
 * a single call.//from ww  w. j  a va  2  s . c  o m
 */
private void refreshFileMetadata(HdfsPartition partition) throws CatalogException {
    Path partDir = partition.getLocationPath();
    Preconditions.checkNotNull(partDir);
    try {
        FileSystem fs = partDir.getFileSystem(CONF);
        if (!fs.exists(partDir)) {
            partition.setFileDescriptors(new ArrayList<FileDescriptor>());
            return;
        }
        if (!FileSystemUtil.supportsStorageIds(fs)) {
            synthesizeBlockMetadata(fs, partition);
            return;
        }
        // Index the partition file descriptors by their file names for O(1) look ups.
        ImmutableMap<String, FileDescriptor> fileDescsByName = Maps.uniqueIndex(partition.getFileDescriptors(),
                new Function<FileDescriptor, String>() {
                    public String apply(FileDescriptor desc) {
                        return desc.getFileName();
                    }
                });
        // Iterate through the current snapshot of the partition directory listing to
        // figure out files that were newly added/modified.
        List<FileDescriptor> newFileDescs = Lists.newArrayList();
        long newPartSizeBytes = 0;
        for (FileStatus fileStatus : fs.listStatus(partDir)) {
            if (!FileSystemUtil.isValidDataFile(fileStatus))
                continue;
            String fileName = fileStatus.getPath().getName().toString();
            FileDescriptor fd = fileDescsByName.get(fileName);
            if (fd == null || partition.isMarkedCached() || fd.getFileLength() != fileStatus.getLen()
                    || fd.getModificationTime() != fileStatus.getModificationTime()) {
                fd = new FileDescriptor(fileName, fileStatus.getLen(), fileStatus.getModificationTime());
                setFdBlockMetadata(fd, fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen()));
            }
            newFileDescs.add(fd);
            newPartSizeBytes += fileStatus.getLen();
        }
        partition.setFileDescriptors(newFileDescs);
        numHdfsFiles_ += newFileDescs.size();
        totalHdfsBytes_ += newPartSizeBytes;
    } catch (IOException e) {
        throw new CatalogException("Error loading block metadata for partition " + partition.toString(), e);
    }
}

From source file:org.restlet.test.ext.apispark.conversion.swagger.v2_0.Swagger2TestCase.java

private void compareRwadefProperties(List<Property> savedPropertiesList,
        List<Property> translatedPropertiesList) {
    if (assertBothNull(savedPropertiesList, translatedPropertiesList)) {
        return;/*from  w  w  w .  j a  v a2s.  c o m*/
    }

    ImmutableMap<String, Property> savedProperties = Maps.uniqueIndex(savedPropertiesList,
            new Function<Property, String>() {
                public String apply(Property property) {
                    return property.getName();
                }
            });
    ImmutableMap<String, Property> translatedProperties = Maps.uniqueIndex(translatedPropertiesList,
            new Function<Property, String>() {
                public String apply(Property property) {
                    return property.getName();
                }
            });

    assertEquals(savedProperties.size(), translatedProperties.size());
    for (String key : savedProperties.keySet()) {
        Property savedProperty = savedProperties.get(key);
        Property translatedProperty = translatedProperties.get(key);
        assertNotNull(savedProperty);
        assertNotNull(translatedProperty);

        assertEquals(savedProperty.getDefaultValue(), translatedProperty.getDefaultValue());
        assertEquals(savedProperty.getDescription(), translatedProperty.getDescription());
        assertEquals(savedProperty.getExample(), translatedProperty.getExample());
        assertEquals(savedProperty.getMax(), translatedProperty.getMax());
        assertEquals(savedProperty.getMin(), translatedProperty.getMin());
        assertEquals(savedProperty.getType(), translatedProperty.getType());
        assertEquals(savedProperty.getMaxOccurs(), translatedProperty.getMaxOccurs());
        assertEquals(savedProperty.getMinOccurs(), translatedProperty.getMinOccurs());

        compareRwadefProperties(savedProperty.getProperties(), translatedProperty.getProperties());
        compareStringLists(savedProperty.getEnumeration(), translatedProperty.getEnumeration());
    }
}

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

private static Table buildTableObject(String queryId, String schemaName, String tableName, String tableOwner,
        List<HiveColumnHandle> columnHandles, HiveStorageFormat hiveStorageFormat, List<String> partitionedBy,
        Optional<HiveBucketProperty> bucketProperty, Map<String, String> additionalTableParameters,
        Path targetPath, boolean external, String prestoVersion) {
    Map<String, HiveColumnHandle> columnHandlesByName = Maps.uniqueIndex(columnHandles,
            HiveColumnHandle::getName);//www .  j a v a 2  s.c o m
    List<Column> partitionColumns = partitionedBy.stream().map(columnHandlesByName::get)
            .map(column -> new Column(column.getName(), column.getHiveType(), column.getComment()))
            .collect(toList());

    Set<String> partitionColumnNames = ImmutableSet.copyOf(partitionedBy);

    ImmutableList.Builder<Column> columns = ImmutableList.builder();
    for (HiveColumnHandle columnHandle : columnHandles) {
        String name = columnHandle.getName();
        HiveType type = columnHandle.getHiveType();
        if (!partitionColumnNames.contains(name)) {
            verify(!columnHandle.isPartitionKey(),
                    "Column handles are not consistent with partitioned by property");
            columns.add(new Column(name, type, columnHandle.getComment()));
        } else {
            verify(columnHandle.isPartitionKey(),
                    "Column handles are not consistent with partitioned by property");
        }
    }

    ImmutableMap.Builder<String, String> tableParameters = ImmutableMap.<String, String>builder()
            .put(PRESTO_VERSION_NAME, prestoVersion).put(PRESTO_QUERY_ID_NAME, queryId)
            .putAll(additionalTableParameters);

    if (external) {
        tableParameters.put("EXTERNAL", "TRUE");
    }

    Table.Builder tableBuilder = Table.builder().setDatabaseName(schemaName).setTableName(tableName)
            .setOwner(tableOwner).setTableType((external ? EXTERNAL_TABLE : MANAGED_TABLE).name())
            .setDataColumns(columns.build()).setPartitionColumns(partitionColumns)
            .setParameters(tableParameters.build());

    tableBuilder.getStorageBuilder().setStorageFormat(fromHiveStorageFormat(hiveStorageFormat))
            .setBucketProperty(bucketProperty).setLocation(targetPath.toString());

    return tableBuilder.build();
}

From source file:org.opentestsystem.authoring.testauth.publish.BasePublisherHelper.java

protected List<AdministrationSegment> setupAdminSegmentData(final Assessment assessment,
        final List<Item> itemList, final List<Segment> segmentList,
        final List<TestBlueprintElement> testBlueprintElementList,
        final List<BlueprintElement> blueprintElementList, final List<AffinityGroup> affinityGroupList) {
    final List<AdministrationSegment> adminSegmentList = Lists.newArrayList();
    for (final Segment segment : segmentList) {
        final ItemSelectionAlgorithmType segmentType = segment.getItemSelectionAlgorithm()
                .getItemSelectionAlgorithmType();
        // AdministrationSegment.id must be equal to segment.id!
        final String itemSelection = (segment.getItemSelectionParameters() != null
                && segment.getItemSelectionParameters().get(SELECTION_ALGORITHM) != null
                        ? segment.getItemSelectionParameters().get(SELECTION_ALGORITHM).toLowerCase()
                        : segmentType.toString().toLowerCase());

        // AK: if there is only one segment segment uniqueid must be equal test(assessment) uniqueid
        final AdministrationSegment adminSegment = new AdministrationSegment((segmentList.size() == 1)
                ? buildIdentifier(assessment.getName(), assessment.getLabel(), assessment.getVersion())
                        .getUniqueId()/* w w  w  . ja  va2s .  co m*/
                : segment.getId(), segment.getPosition(), itemSelection);
        if (segmentType.equals(ItemSelectionAlgorithmType.FIXEDFORM)) {
            final List<FormPartition> formPartitionList = this.formPartitionService
                    .getFormPartitionsBySegmentId(segment.getId());
            adminSegment.setFormPartitionReferenceList(
                    nullsafeListTransform(formPartitionList, FORMPARTITION_REFERENCE_TRANSFORMER.getInstance(
                            assessment.getItemBank(), this.serverUniqueNumber, assessment.getVersion())));
        } else {
            adminSegment.setItemReferenceGroupList(buildItemReferenceGroups(assessment.getItemBank(), null,
                    assessment, itemList, segment.getId(), true));
        }
        // itemselector's itemselectionparameter list populated from all usages of item selection algorithm in the segment/blueprint
        // (scalar/blueprint; e.g. 200 standards x 3 bp isa params + 5 sc isa params = 605 itemselectionparameter entries)
        final ItemSelectionAlgorithm isa = segment.getItemSelectionAlgorithm();
        adminSegment.setItemSelectorIdentifier(buildIdentifier(isa.getName(), null, isa.getVersion()));
        final List<ItemSelectionParameter> itemSelectionParameterList = Lists.newArrayList();
        final List<ItemSelectionAlgorithmParameter> scalarParams = segment.getItemSelectionAlgorithm()
                .getScalarParameters();
        final List<ItemSelectionAlgorithmParameter> blueprintParams = segment.getItemSelectionAlgorithm()
                .getBlueprintParameters();
        if (!CollectionUtils.isEmpty(scalarParams)) {
            final Map<String, Map<String, String>> segmentItemSelectionParameters = ImmutableMap
                    .of(segment.getId(), segment.getItemSelectionParameters());
            itemSelectionParameterList.addAll(nullsafeListTransform(segmentItemSelectionParameters.entrySet(),
                    ISP_LIST_TRANSFORMER.getInstance(Maps.uniqueIndex(segmentList, SEGMENT_KEYID_TRANSFORMER),
                            assessment)));
        }
        if (!CollectionUtils.isEmpty(blueprintParams)) {
            final Map<String, Map<String, String>> blueprintElementItemSelectionParameters = findBlueprintElementItemSelectionParameters(
                    segment.getAssessmentId(), segment.getId(), blueprintElementList);
            itemSelectionParameterList.addAll(nullsafeListTransform(
                    blueprintElementItemSelectionParameters.entrySet(), ISP_LIST_TRANSFORMER.getInstance(
                            Maps.uniqueIndex(segmentList, SEGMENT_KEYID_TRANSFORMER), assessment)));

            final Map<String, Map<String, String>> affinityGroupItemSelectionParameters = findAffinityGroupItemSelectionParameters(
                    segment.getAssessmentId(), segment.getId(), affinityGroupList);
            itemSelectionParameterList.addAll(nullsafeListTransform(
                    affinityGroupItemSelectionParameters.entrySet(), ISP_LIST_TRANSFORMER.getInstance(
                            Maps.uniqueIndex(segmentList, SEGMENT_KEYID_TRANSFORMER), assessment)));
        }
        adminSegment.setItemSelectionParameterList(itemSelectionParameterList);

        final Map<String, ItemCountSummary> blueprintItemCountsBySegment = getBlueprintItemCounts(assessment,
                itemList, segment.getId(), blueprintElementList,
                segmentType.equals(ItemSelectionAlgorithmType.FIXEDFORM));

        final List<SegmentBlueprintElement> segmentBlueprintElementList = Lists.newArrayList();
        for (final Entry<String, ItemCountSummary> blueprintElementCount : blueprintItemCountsBySegment
                .entrySet()) {
            segmentBlueprintElementList.add(new SegmentBlueprintElement(blueprintElementCount.getKey(),
                    blueprintElementCount.getValue().getOpMin(), blueprintElementCount.getValue().getOpMax(),
                    blueprintElementCount.getValue().getFtMin(), blueprintElementCount.getValue().getFtMax(),
                    blueprintElementCount.getValue().getOpCount(),
                    blueprintElementCount.getValue().getFtCount()));
        }

        for (final AffinityGroup affinityGroup : affinityGroupList) {
            // segment bp elements
            final Map<String, ItemCountSummary> affinityGroupItemCounts = this.itemService
                    .getAffinityGroupItemCounts(affinityGroup.getId());
            final ItemCountSummary segmentItemCounts = affinityGroupItemCounts.get(segment.getId());
            segmentBlueprintElementList.add(new SegmentBlueprintElement(
                    buildIdentifier(affinityGroup.getGroupName(), null, DEFAULT_VERSION).getUniqueId(),
                    //affinityGroup.getGroupName(),
                    segmentItemCounts.getOpMin(), segmentItemCounts.getOpMax(), segmentItemCounts.getFtMin(),
                    segmentItemCounts.getFtMax(), segmentItemCounts.getOpCount(),
                    segmentItemCounts.getOpMax()));
        }

        adminSegment.setSegmentBlueprintElementList(segmentBlueprintElementList);
        adminSegmentList.add(adminSegment);
    }
    return adminSegmentList;
}

From source file:org.restlet.test.ext.apispark.conversion.swagger.v2_0.Swagger2TestCase.java

private void compareRwadefSections(Contract savedContract, Contract translatedContract) {
    if (assertBothNull(savedContract.getSections(), translatedContract.getSections())) {
        return;/* ww w. j  a  v  a 2  s. co  m*/
    }

    ImmutableMap<String, Section> savedSections = Maps.uniqueIndex(savedContract.getSections(),
            new Function<Section, String>() {
                public String apply(Section section) {
                    return section.getName();
                }
            });
    ImmutableMap<String, Section> translatedSections = Maps.uniqueIndex(translatedContract.getSections(),
            new Function<Section, String>() {
                public String apply(Section section) {
                    return section.getName();
                }
            });

    assertEquals(savedSections.size(), translatedSections.size());
    for (String key : savedSections.keySet()) {
        Section savedSection = savedSections.get(key);
        Section translatedSection = translatedSections.get(key);
        assertNotNull(savedSection);
        assertNotNull(translatedSection);
        assertEquals(savedSection.getDescription(), translatedSection.getDescription());
    }
}

From source file:org.kie.workbench.common.screens.projecteditor.client.editor.ProjectScreenPresenter.java

@Override
public void triggerBuildAndDeploy() {
    specManagementService.call(new RemoteCallback<Collection<ServerTemplate>>() {
        @Override//  ww w . j a  v  a2 s.c om
        public void callback(final Collection<ServerTemplate> serverTemplates) {
            //TODO use config for using defaults or not
            final String defaultContainerId = project.getPom().getGav().getArtifactId() + "_"
                    + project.getPom().getGav().getVersion();
            final String defaultContainerAlias = project.getPom().getGav().getArtifactId();
            final boolean defaultStartContainer = true;
            if (serverTemplates.isEmpty()) {
                getSafeExecutedCommand(getBuildDeployCommand(DeploymentMode.VALIDATED)).execute();
            } else if (serverTemplates.size() == 1) {
                final ServerTemplate serverTemplate = serverTemplates.iterator().next();
                final Set<String> existingContainers = FluentIterable.from(serverTemplate.getContainersSpec())
                        .transform(s -> s.getId()).toSet();
                if (existingContainers.contains(defaultContainerId) == false) {
                    getSafeExecutedCommand(
                            getBuildDeployProvisionCommand(DeploymentMode.VALIDATED, defaultContainerId,
                                    defaultContainerAlias, serverTemplate.getId(), defaultStartContainer))
                                            .execute();
                } else {
                    deploymentScreenPopupView.setValidateExistingContainerCallback(
                            containerName -> existingContainers.contains(containerName));
                    deploymentScreenPopupView.setContainerId(defaultContainerId);
                    deploymentScreenPopupView.setContainerAlias(defaultContainerAlias);
                    deploymentScreenPopupView.setStartContainer(defaultStartContainer);
                    deploymentScreenPopupView.configure(new com.google.gwt.user.client.Command() {
                        @Override
                        public void execute() {
                            final String containerId = deploymentScreenPopupView.getContainerId();
                            final String containerAlias = deploymentScreenPopupView.getContainerAlias();
                            final boolean startContainer = deploymentScreenPopupView.getStartContainer();

                            getSafeExecutedCommand(getBuildDeployProvisionCommand(DeploymentMode.VALIDATED,
                                    containerId, containerAlias, serverTemplate.getId(), startContainer))
                                            .execute();

                            deploymentScreenPopupView.hide();
                        }
                    });
                    deploymentScreenPopupView.show();
                }
            } else {
                final Map<String, ServerTemplate> serverTemplatesIds = Maps.uniqueIndex(serverTemplates,
                        s -> s.getId());
                final Map<String, Set<String>> containerNames = Maps.transformEntries(serverTemplatesIds,
                        (id, server) -> FluentIterable.from(server.getContainersSpec())
                                .transform(c -> c.getContainerName()).toSet());
                deploymentScreenPopupView.addServerTemplates(FluentIterable.from(serverTemplatesIds.keySet())
                        .toSortedSet(String.CASE_INSENSITIVE_ORDER));
                deploymentScreenPopupView.setValidateExistingContainerCallback(containerName -> FluentIterable
                        .from(containerNames.get(deploymentScreenPopupView.getServerTemplate()))
                        .contains(containerName));
                deploymentScreenPopupView.setContainerId(defaultContainerId);
                deploymentScreenPopupView.setContainerAlias(defaultContainerAlias);
                deploymentScreenPopupView.setStartContainer(defaultStartContainer);
                deploymentScreenPopupView.configure(new com.google.gwt.user.client.Command() {
                    @Override
                    public void execute() {
                        final String containerId = deploymentScreenPopupView.getContainerId();
                        final String containerAlias = deploymentScreenPopupView.getContainerAlias();
                        final String serverTemplate = deploymentScreenPopupView.getServerTemplate();
                        final boolean startContainer = deploymentScreenPopupView.getStartContainer();

                        getSafeExecutedCommand(getBuildDeployProvisionCommand(DeploymentMode.VALIDATED,
                                containerId, containerAlias, serverTemplate, startContainer)).execute();

                        deploymentScreenPopupView.hide();
                    }
                });
                deploymentScreenPopupView.show();
            }
        }
    }).listServerTemplates();

}

From source file:org.opentestsystem.authoring.testauth.publish.BasePublisherHelper.java

protected List<TestForm> setupTestFormData(final Assessment assessment, final List<Segment> segmentList,
        final List<Item> itemList, final Map<String, TestItem> testItemMap) {
    final List<TestForm> formList = Lists.newArrayList();
    final SearchResponse<Form> formSearchResponse = this.formService
            .searchForms(searchParamsByAssessmentIdLarge(assessment.getAssessmentId()));
    for (final Form form : formSearchResponse.getSearchResults()) {
        // final List<Item> filteredItemList = Lists.newArrayList(Iterables.concat(retrieveScopedItems(assessmentId, itemList, null, true).values()));
        //EF: different forms can have the same item, we want here list of unique items,
        // changed from Lists to Sets;
        final Set<Item> filteredItemSet = Sets.newHashSet(Iterables
                .concat(retrieveScopedItems(assessment.getAssessmentId(), itemList, null, true).values()));
        final List<Item> filteredItemList = new ArrayList<Item>(filteredItemSet);
        final TestForm testForm = doGeneralFormSetup(TestForm.class, form, filteredItemList, testItemMap,
                assessment.getVersion());
        final List<TestFormPartition> testFormPartitionList = Lists.newArrayList();
        testForm.setTestFormPartitionList(testFormPartitionList);
        final List<FormPartition> formPartitionList = this.formPartitionService
                .getFormPartitionsByFormId(form.getId());
        for (final FormPartition formPartition : formPartitionList) {
            //Maps.uniqueIndex(segmentList, SEGMENT_KEYID_TRANSFORMER)
            //           Segment seg = segmentMap.get (formPartition.getSegmentId ());
            final List<ItemReferenceGroup> itemReferenceGroupList = buildItemReferenceGroups(
                    assessment.getItemBank(), Maps.uniqueIndex(segmentList, SEGMENT_KEYID_TRANSFORMER),
                    assessment, filteredItemList, formPartition.getId(), false);
            final TestFormPartition testFormPartition = new TestFormPartition(

                    new Identifier(
                            FORMPARTITION_REFERENCE_TRANSFORMER.getInstance(assessment.getItemBank(),
                                    this.serverUniqueNumber, assessment.getVersion()).apply(formPartition),
                            formPartition.getName(), formPartition.getName(), DEFAULT_VERSION),
                    ITEM_REF_GROUP_ORDER.sortedCopy(itemReferenceGroupList));

            testForm.getTestFormPartitionList().add(testFormPartition);
        }/*from  w w  w.  ja v a  2  s.c  o m*/
        formList.add(testForm);
    }
    return formList;
}

From source file:org.jasig.ssp.service.impl.EvaluatedSuccessIndicatorServiceImpl.java

/**
 * Must generate keys that match those generated by
 * {@link #externalRiskMapKeyFor(org.jasig.ssp.model.reference.SuccessIndicator)}.
 *
 * @param esriList//from  ww  w  . j  ava2s .c  o  m
 * @return
 */
private Map<String, ExternalStudentRiskIndicator> mapOf(@Nullable List<ExternalStudentRiskIndicator> esriList) {
    return esriList == null ? Collections.EMPTY_MAP
            : Maps.uniqueIndex(esriList, RISK_INDICATOR_MAP_KEY_GENERATOR);
}

From source file:org.jasig.ssp.service.impl.EvaluatedSuccessIndicatorServiceImpl.java

private Map<String, Blurb> mapOfBlurbs(@Nullable Collection<Blurb> blurbList) {
    return blurbList == null ? Collections.EMPTY_MAP : Maps.uniqueIndex(blurbList, BLURB_MAP_KEY_GENERATOR);
}