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.jclouds.joyent.sdc.v6_5.compute.config.SDCComputeServiceContextModule.java

@Provides
@Singleton/*from  ww  w . j  a va 2 s . c  om*/
protected Supplier<Map<String, Location>> createLocationIndexedById(
        @Memoized Supplier<Set<? extends Location>> locations) {
    return Suppliers.compose(new Function<Set<? extends Location>, Map<String, Location>>() {

        @Override
        public Map<String, Location> apply(Set<? extends Location> arg0) {
            return Maps.uniqueIndex(Iterables2.concreteCopy(arg0), new Function<Location, String>() {

                @Override
                public String apply(Location arg0) {
                    return arg0.getId();
                }

            });
        }
    }, locations);

}

From source file:annis.sqlgen.FrequencySqlGenerator.java

private Multimap<FrequencyTableEntry, String> conditionsForEntries(List<FrequencyTableEntry> frequencyEntries,
        QueryData queryData, List<QueryNode> alternative) {
    Multimap<FrequencyTableEntry, String> conditions = LinkedHashMultimap.create();
    int i = 1;//  w w  w . j av  a 2s  .c om

    ImmutableMap<String, QueryNode> idxNodeVariables = Maps.uniqueIndex(alternative.iterator(),
            new Function<QueryNode, String>() {
                @Override
                public String apply(QueryNode input) {
                    return input.getVariable();
                }
            });

    for (FrequencyTableEntry e : frequencyEntries) {
        if (e.getType() == FrequencyTableEntryType.meta) {
            List<String> qName = Splitter.on(':').limit(2).omitEmptyStrings().splitToList(e.getKey());
            if (qName.size() == 2) {
                conditions.put(e, "v" + i + ".namespace = '" + escaper.escape(qName.get(0)) + "'");
                conditions.put(e, "v" + i + ".name = '" + escaper.escape(qName.get(1)) + "'");
            } else {
                conditions.put(e, "v" + i + ".name = '" + escaper.escape(qName.get(0)) + "'");
            }
            conditions.put(e, "v" + i + ".corpus_ref = solutions.corpus_ref");
        } else {
            // general partition restriction
            conditions.put(e,
                    "v" + i + ".toplevel_corpus IN (" + StringUtils.join(queryData.getCorpusList(), ",") + ")");
            // specificly join on top level corpus
            conditions.put(e, "v" + i + ".toplevel_corpus = solutions.toplevel_corpus");

            // join on node ID
            QueryNode referencedNode = idxNodeVariables.get(e.getReferencedNode());
            if (referencedNode == null) {
                throw new AnnisQLSemanticsException("No such node \"" + e.getReferencedNode() + "\". "
                        + "Your query contains " + alternative.size()
                        + " node(s), make sure no node definition numbers are greater than this number");
            }
            conditions.put(e, "v" + i + ".id = solutions.id" + referencedNode.getId());

            if (e.getType() == FrequencyTableEntryType.span) {
                conditions.put(e, "v" + i + ".n_sample IS TRUE");
            } else if (e.getType() == FrequencyTableEntryType.annotation) {
                // TODO: support namespaces
                // filter by selected key
                conditions.put(e, "v" + i + ".node_annotext LIKE '"
                        + AnnotationConditionProvider.likeEscaper.escape(e.getKey()) + ":%'");
                conditions.put(e, "v" + i + ".n_na_sample IS TRUE");
            }
        }
        i++;
    }
    return conditions;
}

From source file:org.graylog2.users.RoleServiceImpl.java

@Override
public Map<String, Role> loadAllIdMap() throws NotFoundException {
    final Set<Role> roles = loadAll();
    return Maps.uniqueIndex(roles, new Function<Role, String>() {
        @Nullable//from  ww w  .ja  va 2  s.c  o  m
        @Override
        public String apply(Role input) {
            return input.getId();
        }
    });
}

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

private Map<String, SnomedConcept> getConceptMap(final Options expandOptions, final Set<String> conceptIds) {
    final SnomedConcepts types = SnomedRequests.prepareSearchConcept().filterByIds(conceptIds)
            .setLimit(conceptIds.size()).setLocales(locales())
            .setExpand(expandOptions.get("expand", Options.class)).build().execute(context());

    final Map<String, SnomedConcept> conceptsById = Maps.uniqueIndex(types, ID_FUNCTION);
    return conceptsById;
}

From source file:org.jclouds.joyent.cloudapi.v6_5.compute.config.JoyentCloudComputeServiceContextModule.java

@Provides
@Singleton//ww  w  .j  av  a 2 s  . c  o m
protected Supplier<Map<String, Location>> createLocationIndexedById(
        @Memoized Supplier<Set<? extends Location>> locations) {
    return Suppliers.compose(new Function<Set<? extends Location>, Map<String, Location>>() {

        @Override
        public Map<String, Location> apply(Set<? extends Location> arg0) {
            return Maps.uniqueIndex(ImmutableSet.<Location>copyOf(arg0), new Function<Location, String>() {

                @Override
                public String apply(Location arg0) {
                    return arg0.getId();
                }

            });
        }
    }, locations);

}

From source file:org.artifactory.storage.StorageServiceImpl.java

@Override
public StorageSummaryInfo getStorageSummaryInfo() {
    Set<RepoStorageSummary> summaries = fileService.getRepositoriesStorageSummary();
    List<RepoDescriptor> repos = Lists.newArrayList();
    repos.addAll(repositoryService.getLocalAndCachedRepoDescriptors());
    repos.addAll(repositoryService.getVirtualRepoDescriptors());
    final ImmutableMap<String, RepoDescriptor> reposMap = Maps.uniqueIndex(repos,
            new Function<RepoDescriptor, String>() {
                @Nullable/*ww w .j  a v  a  2  s  . com*/
                @Override
                public String apply(@Nullable RepoDescriptor input) {
                    if (input == null) {
                        return null;
                    }
                    return input.getKey();
                }
            });
    Iterable<RepoStorageSummaryInfo> infos = Iterables.transform(summaries,
            new Function<RepoStorageSummary, RepoStorageSummaryInfo>() {
                @Override
                public RepoStorageSummaryInfo apply(RepoStorageSummary r) {
                    RepositoryType repoType = getRepoType(r.getRepoKey(), reposMap);
                    return new RepoStorageSummaryInfo(r.getRepoKey(), repoType, r.getFoldersCount(),
                            r.getFilesCount(), r.getUsedSpace());
                }

                private RepositoryType getRepoType(String repoKey,
                        ImmutableMap<String, RepoDescriptor> repoDescriptors) {
                    RepoDescriptor repoDescriptor = repoDescriptors.get(repoKey);
                    if (repoDescriptor == null) {
                        return RepositoryType.BROKEN;
                    } else if (repoDescriptor instanceof RemoteRepoDescriptor) {
                        return RepositoryType.REMOTE;
                    } else if (repoDescriptor instanceof VirtualRepoDescriptor) {
                        return RepositoryType.VIRTUAL;
                    } else if (repoDescriptor instanceof LocalCacheRepoDescriptor) {
                        return RepositoryType.CACHE;
                    } else if (repoDescriptor instanceof LocalRepoDescriptor) {
                        return RepositoryType.LOCAL;
                    } else {
                        return RepositoryType.NA;
                    }
                }
            });

    BinariesInfo binariesInfo = binaryStore.getBinariesInfo();

    return new StorageSummaryInfo(Sets.newHashSet(infos), binariesInfo);
}

From source file:com.sahlbach.maven.delivery.DeliveryMojo.java

/**
 * for every local delivery merges the values from deliveryManagement as defaults
 * the resulting merge should contain a merge of all local delivery definitions. local definitions with same ids should overwrite the data of deliveryManagement section
 * @return merged list of deliveries/*from   w w w. j a v a2  s . c o m*/
 * @throws org.apache.maven.plugin.MojoExecutionException in case of merge conflicts
 */
private List<Delivery> mergeDeliveries() throws MojoExecutionException {
    Map<String, Delivery> defaultMap = Maps.uniqueIndex(deliveryManagement, new Function<Delivery, String>() {
        @Override
        public String apply(Delivery input) {
            return input.getId();
        }
    });
    List<Delivery> result = Lists.newArrayListWithCapacity(deliveries.size());
    for (Delivery localDelivery : deliveries) {
        Delivery mergedDelivery = defaultMap.get(localDelivery.getId());
        if (mergedDelivery == null)
            mergedDelivery = new Delivery();
        mergedDelivery.mergeWith(localDelivery);
        result.add(mergedDelivery);
    }
    return result;
}

From source file:com.offbytwo.jenkins.JenkinsServer.java

/**
 * Get a list of all the defined views on the server (at the summary level
 * and in the given folder)/* ww w.j av  a2 s. c om*/
 *
 * @return list of defined views
 * @throws IOException
 */
public Map<String, View> getViews(FolderJob folder) throws IOException {
    String path = "/";
    if (folder != null) {
        path = folder.getUrl();
    }
    List<View> views = client.get(path, MainView.class).getViews();
    return Maps.uniqueIndex(views, new Function<View, String>() {
        @Override
        public String apply(View view) {
            view.setClient(client);
            // return view.getName().toLowerCase();
            return view.getName();
        }
    });
}

From source file:org.gradle.api.internal.tasks.properties.DefaultPropertyMetadataStore.java

public DefaultPropertyMetadataStore(Iterable<? extends PropertyAnnotationHandler> customAnnotationHandlers,
        CrossBuildInMemoryCacheFactory cacheFactory) {
    Iterable<PropertyAnnotationHandler> allAnnotationHandlers = Iterables.concat(HANDLERS,
            customAnnotationHandlers);//from www.j ava  2s.  c  om
    Map<Class<? extends Annotation>, PropertyAnnotationHandler> annotationsHandlers = Maps.uniqueIndex(
            allAnnotationHandlers, new Function<PropertyAnnotationHandler, Class<? extends Annotation>>() {
                @Override
                public Class<? extends Annotation> apply(PropertyAnnotationHandler handler) {
                    return handler.getAnnotationType();
                }
            });
    this.annotationHandlers = annotationsHandlers;
    this.annotationOverrides = collectAnnotationOverrides(allAnnotationHandlers);
    this.relevantAnnotationTypes = collectRelevantAnnotationTypes(annotationsHandlers.keySet());
    cache = cacheFactory.newClassCache();
}

From source file:org.atlasapi.remotesite.bbc.BbcProgrammeGraphExtractor.java

public Item extract(BbcProgrammeSource source) {

    String episodeUri = source.getUri();

    SlashProgrammesRdf episode = source.episode();

    Item item = item(episodeUri, episode);

    IonEpisodeDetail episodeDetail = getEpisodeDetail(episodeUri);

    Map<String, IonVersion> ionVersions = ImmutableMap.of();
    if (episodeDetail != null) {
        ionVersions = Maps.uniqueIndex(episodeDetail.getVersions(), new Function<IonVersion, String>() {
            @Override//w w  w  . j a v  a  2s .  c  o  m
            public String apply(IonVersion input) {
                return SLASH_PROGRAMMES_ROOT + input.getId();
            }
        });

        for (IonEpisode clip : episodeDetail.getClips()) {
            item.addClip(clipExtractor.extract(clip));
        }
    }

    if (source.versions() != null && !source.versions().isEmpty()) {

        for (SlashProgrammesVersionRdf versionRdf : source.versions()) {
            Version version = version(versionRdf);
            version.setProvider(BBC);

            IonVersion ionVersion = ionVersions.get(version.getCanonicalUri());
            if (ionVersion != null) {
                setDurations(version, ionVersion);
                version.setLastUpdated(ionVersion.getUpdated());

                if (!Strings.isNullOrEmpty(ionVersion.getGuidanceText())) {
                    version.setRestriction(Restriction.from(ionVersion.getGuidanceText()));
                }

                version.setManifestedAs(encodingsFrom(ionVersion, bbcProgrammeIdFrom(episodeUri)));
            } else {
                Long duration = Long.valueOf(versionRdf.version.duration());
                version.setDuration(standardSeconds(duration));
                version.setPublishedDuration(Ints.saturatedCast(duration));
            }
            item.addVersion(version);
        }

    }

    addExtendedData(item);

    return item;
}