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.ec2.util.Tags.java

/**
 * maps the input on {@link Tag#getResourceId()} with a value of a map of its {@link Tag#getKey()} to
 * {@link Tag#getValue()}/*from   ww w . j a  va2 s  . com*/
 */
public static Map<String, Map<String, String>> resourceToTagsAsMap(Iterable<Tag> tags) {
    return Maps.transformValues(index(tags, resourceIdFunction()).asMap(),
            new Function<Iterable<Tag>, Map<String, String>>() {
                @Override
                public Map<String, String> apply(Iterable<Tag> in) {
                    return Maps.transformValues(Maps.uniqueIndex(in, keyFunction()), valueFunction());
                }
            });
}

From source file:org.ambraproject.rhino.config.PersistenceAdapter.java

public static <E extends Enum<E>> PersistenceAdapter<E, String> byEnumName(Class<E> enumType) {
    ImmutableMap<String, E> enumsByName = Maps.uniqueIndex(Arrays.asList(enumType.getEnumConstants()), E::name);
    return new PersistenceAdapter<E, String>() {
        @Override/* w  w  w  .  j  av  a2 s  .  c o  m*/
        public Class<E> getModelClass() {
            return enumType;
        }

        @Override
        public Class<String> getDataClass() {
            return String.class;
        }

        @Override
        public String encode(E model) {
            return model.name();
        }

        @Override
        public E decode(String data) {
            return enumsByName.get(data);
        }
    };
}

From source file:org.jclouds.openstack.keystone.v2_0.config.CredentialTypes.java

public static <T> Map<String, T> indexByCredentialType(Iterable<T> iterable) {
    return Maps.uniqueIndex(iterable, new Function<T, String>() {

        @Override/* ww  w. j ava2  s.co  m*/
        public String apply(T input) {
            return credentialTypeOf(input);
        }

    });
}

From source file:com.google.testing.junit.runner.internal.StackTraces.java

/**
 * Prints all stack traces to the given stream.
 *
 * @param out Stream to print to/*  w ww . j a v a  2 s . c o m*/
 */
public static void printAll(PrintStream out) {
    out.println("Starting full thread dump ...\n");
    ThreadMXBean mb = ManagementFactory.getThreadMXBean();

    // ThreadInfo has comprehensive information such as locks.
    ThreadInfo[] threadInfos = mb.dumpAllThreads(true, true);

    // But we can know whether a thread is daemon only from Thread
    Set<Thread> threads = Thread.getAllStackTraces().keySet();
    ImmutableMap<Long, Thread> threadMap = Maps.uniqueIndex(threads, new Function<Thread, Long>() {
        @Override
        public Long apply(Thread thread) {
            return thread.getId();
        }
    });

    // Dump non-daemon threads first
    for (ThreadInfo threadInfo : threadInfos) {
        Thread thread = threadMap.get(threadInfo.getThreadId());
        if (thread != null && !thread.isDaemon()) {
            dumpThreadInfo(threadInfo, thread, out);
        }
    }

    // Dump daemon threads
    for (ThreadInfo threadInfo : threadInfos) {
        Thread thread = threadMap.get(threadInfo.getThreadId());
        if (thread != null && thread.isDaemon()) {
            dumpThreadInfo(threadInfo, thread, out);
        }
    }

    long[] deadlockedThreads = mb.findDeadlockedThreads();
    if (deadlockedThreads != null) {
        out.println("Detected deadlocked threads: " + Arrays.toString(deadlockedThreads));
    }
    long[] monitorDeadlockedThreads = mb.findMonitorDeadlockedThreads();
    if (monitorDeadlockedThreads != null) {
        out.println("Detected monitor deadlocked threads: " + Arrays.toString(monitorDeadlockedThreads));
    }
    out.println("\nDone full thread dump.");
    out.flush();
}

From source file:org.jclouds.openstack.keystone.auth.config.CredentialTypes.java

public static <T> Map<String, T> indexByCredentialType(Iterable<T> iterable) {
    return Maps.uniqueIndex(iterable, new Function<T, String>() {
        @Override//from  w  w  w . j a  v  a 2 s  .co  m
        public String apply(T input) {
            return credentialTypeOf(input);
        }
    });
}

From source file:org.locationtech.geogig.porcelain.index.IndexUtils.java

public static NodeRef resolveTypeTreeRef(Context context, String treeRefSpec) {
    checkArgument(treeRefSpec != null, "type tree was not provided");
    final String rootRef;
    final String treePath;
    final int rootSepIndex = treeRefSpec.indexOf(':');
    if (-1 == rootSepIndex) {
        rootRef = "HEAD";
        treePath = treeRefSpec;/* w  w w  .ja  va2 s .co  m*/
    } else {
        rootRef = treeRefSpec.substring(0, rootSepIndex);
        treePath = treeRefSpec.substring(rootSepIndex + 1);
    }
    List<NodeRef> treeRefs = context.command(FindFeatureTypeTrees.class).setRootTreeRef(rootRef).call();
    ImmutableMap<String, NodeRef> map = Maps.uniqueIndex(treeRefs, (r) -> r.path());
    NodeRef treeRef = map.get(treePath);
    checkArgument(treeRef != null, "Can't find feature tree '%s'", treeRefSpec);
    return treeRef;
}

From source file:org.gradle.model.dsl.internal.inputs.PotentialInputs.java

public PotentialInputs(List<ModelView<?>> modelViews, Iterable<PotentialInput> inputs) {
    this.modelViews = modelViews;
    this.inputs = Maps.uniqueIndex(inputs, new Function<PotentialInput, String>() {
        @Override//from w  w w . j  av a 2s .co m
        public String apply(PotentialInput input) {
            return input.getConsumerPath();
        }
    });
}

From source file:com.atlassian.jira.rest.client.api.domain.Permissions.java

public Permissions(final Iterable<Permission> permissions) {
    this.permissionMap = Maps.uniqueIndex(permissions, Permission.TO_KEY);
}

From source file:org.caleydo.view.domino.internal.BlockBands.java

@Override
protected void update() {
    Map<ABandIdentifier, ABand> bak = Maps.uniqueIndex(bands, new Function<ABand, ABandIdentifier>() {
        @Override//from  www .j  av a  2s. c o  m
        public ABandIdentifier apply(ABand input) {
            return input.getId();
        }
    });
    bands.clear();

    Block block = findParent(Block.class);
    block.createOffsetBands(bands);

    for (ABand band : bands) {
        if (bak.containsKey(band.getId())) {
            band.initFrom(bak.get(band.getId()));
        }
    }
}

From source file:org.impressivecode.depress.its.ITSDataHolder.java

ITSDataHolder(final List<ITSDataType> issueData) {
    this.issues = Maps.uniqueIndex(issueData, new Function<ITSDataType, String>() {
        @Override//from   w ww  . j a va 2 s  . c  o m
        public String apply(final ITSDataType from) {
            return from.getIssueId();
        }
    });
}