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.locationtech.geogig.plumbing.DiffFeature.java

/**
 * Finds differences between the two specified trees.
 * /*w w  w. j  a v a2  s .c om*/
 * @return a FeatureDiff object with the differences between the specified features
 * @see FeatureDiff
 */
@Override
protected FeatureDiff _call() throws IllegalArgumentException {
    checkNotNull(oldNodeRef, "old version not specified");
    checkNotNull(newNodeRef, "new version not specified");
    String oldPath = removeRef(oldNodeRef.path());
    String newPath = removeRef(newNodeRef.path());
    checkArgument(oldPath.equals(newPath), "old and new versions do not corespond to the same feature");

    Set<ObjectId> ids = Sets.newHashSet(oldNodeRef.getObjectId(), newNodeRef.getObjectId(),
            oldNodeRef.getMetadataId(), newNodeRef.getMetadataId());

    Map<ObjectId, RevObject> objects = Maps.uniqueIndex(objectDatabase().getAll(ids), (o) -> o.getId());

    RevFeature oldFeature = (RevFeature) objects.get(oldNodeRef.getObjectId());
    checkArgument(oldFeature != null, "Invalid reference: %s", oldNodeRef);

    RevFeature newFeature = (RevFeature) objects.get(newNodeRef.getObjectId());
    checkArgument(newFeature != null, "Invalid reference: %s", newNodeRef);

    RevFeatureType oldFeatureType = (RevFeatureType) objects.get(oldNodeRef.getMetadataId());
    checkArgument(oldFeatureType != null, "Invalid reference: %s", oldNodeRef);

    RevFeatureType newFeatureType = (RevFeatureType) objects.get(newNodeRef.getMetadataId());
    checkArgument(newFeatureType != null, "Invalid reference: %s", newNodeRef);

    return compare(oldFeature, newFeature, oldFeatureType, newFeatureType);

}

From source file:kuona.jenkins.analyser.JenkinsProcessor.java

/**
 * Get a list of all the computers on the server (at the summary level)
 *
 * @return list of defined computers (summary level, for details @see Computer#details
 * @throws IOException/*www.j a va 2 s . c  o m*/
 */
public Map<String, Computer> getComputers() throws IOException {
    List<Computer> computers = client.get("computer/", Computer.class).getComputers();
    return Maps.uniqueIndex(computers, new Function<Computer, String>() {
        @Override
        public String apply(Computer computer) {
            computer.setClient(client);
            return computer.getDisplayName().toLowerCase();
        }
    });
}

From source file:eu.thebluemountain.customers.dctm.brownbag.badcontentslister.Stores.java

/**
 * @return the stores identified by related identifier
 *///from  w ww  .ja v a  2  s . c om
public ImmutableMap<String, Store> byids() {
    return Maps.uniqueIndex(this.all, Store.ID);
}

From source file:springfox.documentation.service.Operation.java

private Map<String, List<AuthorizationScope>> toAuthorizationsMap(List<SecurityReference> securityReferences) {
    return Maps.transformEntries(Maps.uniqueIndex(securityReferences, byType()), toScopes());
}

From source file:org.geogit.api.plumbing.diff.MutableTree.java

public static MutableTree createFromRefs(final ObjectId rootId, final Iterator<NodeRef> treeRefs) {

    Function<NodeRef, String> keyFunction = new Function<NodeRef, String>() {

        @Override//from   w w  w .  ja  va 2  s.  c om
        public String apply(@Nullable NodeRef input) {
            return input.path();
        }
    };

    ImmutableMap<String, NodeRef> treesByPath = Maps.uniqueIndex(treeRefs, keyFunction);

    return createFromPaths(rootId, treesByPath);
}

From source file:com.palantir.lock.ForwardingLockService.java

@Override
public Set<HeldLocksToken> refreshTokens(Iterable<HeldLocksToken> tokens) {
    Set<LockRefreshToken> refreshTokens = ImmutableSet
            .copyOf(Iterables.transform(tokens, HeldLocksTokens.getRefreshTokenFun()));
    Set<LockRefreshToken> goodTokens = delegate().refreshLockRefreshTokens(refreshTokens);
    Set<HeldLocksToken> ret = Sets.newHashSetWithExpectedSize(refreshTokens.size());
    Map<LockRefreshToken, HeldLocksToken> tokenMap = Maps.uniqueIndex(tokens,
            HeldLocksTokens.getRefreshTokenFun());
    for (LockRefreshToken goodToken : goodTokens) {
        HeldLocksToken lock = tokenMap.get(goodToken);
        ret.add(goodToken.refreshTokenWithExpriationDate(lock));
    }//from  w w  w.java2  s  .c  o  m
    return ret;
}

From source file:org.jclouds.sqs.domain.BatchResult.java

private BatchResult(Map<String, V> results, Iterable<BatchError> errors) {
    this.results = ImmutableMap.copyOf(checkNotNull(results, "results"));
    this.errors = Maps.uniqueIndex(checkNotNull(errors, "errors"), new Function<BatchError, String>() {
        @Override// w w w . ja  v a  2 s.c  o  m
        public String apply(BatchError in) {
            return in.getId();
        }

    });
}

From source file:net.conquiris.schema.SchemaImpl.java

@Override
public Schema extend(Iterable<? extends SchemaItem> items) {
    // Never written.
    @SuppressWarnings("unchecked")
    Iterable<SchemaItem> i = (Iterable<SchemaItem>) items;
    Map<String, SchemaItem> others = Maps.uniqueIndex(i, SchemaItem.NAME);
    if (others.isEmpty()) {
        return this;
    }//from w w w .ja  v a  2s  . c  om
    return extendWithMap(others);
}

From source file:org.jclouds.vcloud.terremark.domain.internal.VAppImpl.java

public VAppImpl(String id, String name, String type, URI location, VAppStatus status, long size, Link vDC,
        Link computeOptions, Link customizationOptions, ListMultimap<String, InetAddress> networkToAddresses,
        String operatingSystemDescription, VirtualSystem system,
        SortedSet<ResourceAllocation> resourceAllocations) {
    super(id, name, type, location);
    this.status = status;
    this.size = size;
    this.vDC = vDC;
    this.computeOptions = computeOptions;
    this.customizationOptions = customizationOptions;
    this.networkToAddresses = networkToAddresses;
    this.operatingSystemDescription = operatingSystemDescription;
    this.system = system;
    this.resourceAllocations = resourceAllocations;
    resourceAllocationByType = Maps.uniqueIndex(resourceAllocations,
            new Function<ResourceAllocation, ResourceType>() {
                @Override/*from www  .j  ava2  s .c  o m*/
                public ResourceType apply(ResourceAllocation from) {
                    return from.getResourceType();
                }
            });
}

From source file:org.jclouds.byon.functions.NodesFromYamlStream.java

@Override
public LoadingCache<String, Node> apply(InputStream source) {

    Constructor constructor = new Constructor(Config.class);

    TypeDescription nodeDesc = new TypeDescription(YamlNode.class);
    nodeDesc.putListPropertyType("tags", String.class);
    constructor.addTypeDescription(nodeDesc);

    TypeDescription configDesc = new TypeDescription(Config.class);
    configDesc.putListPropertyType("nodes", YamlNode.class);
    constructor.addTypeDescription(configDesc);
    // note that snakeyaml also throws nosuchmethod error when you use the
    // non-deprecated
    // constructor
    Yaml yaml = new Yaml(new Loader(constructor));
    Config config = (Config) yaml.load(source);
    checkState(config != null, "missing config: class");
    checkState(config.nodes != null, "missing nodes: collection");

    Map<String, Node> backingMap = Maps.uniqueIndex(Iterables.transform(config.nodes, YamlNode.toNode),
            new Function<Node, String>() {
                public String apply(Node node) {
                    return node.getId();
                }//from ww  w  . j a v a  2  s. c  om
            });
    LoadingCache<String, Node> cache = CacheBuilder.newBuilder()
            .build(CacheLoader.from(Functions.forMap(backingMap)));
    for (String node : backingMap.keySet())
        cache.getUnchecked(node);
    return cache;
}