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.gradle.api.internal.tasks.cache.ZipTaskOutputPacker.java

@Override
public void unpack(TaskOutputsInternal taskOutputs, InputStream input) throws IOException {
    Map<String, TaskOutputFilePropertySpec> propertySpecs = Maps.uniqueIndex(taskOutputs.getFileProperties(),
            new Function<TaskFilePropertySpec, String>() {
                @Override// w w w.j a  va 2s  . c om
                public String apply(TaskFilePropertySpec propertySpec) {
                    return propertySpec.getPropertyName();
                }
            });
    ZipInputStream zipInput = new ZipInputStream(input);
    ZipEntry entry;
    while ((entry = zipInput.getNextEntry()) != null) {
        String name = entry.getName();
        Matcher matcher = PROPERTY_PATH.matcher(name);
        if (!matcher.matches()) {
            // TODO:LPTR What to do here?
            continue;
        }
        String propertyName = matcher.group(1);
        TaskOutputFilePropertySpec propertySpec = propertySpecs.get(propertyName);
        if (propertySpec == null) {
            throw new IllegalStateException(String.format("No output property '%s' registered", propertyName));
        }

        String path = matcher.group(2);
        File outputFile;
        if (Strings.isNullOrEmpty(path)) {
            outputFile = propertySpec.getOutputFile();
        } else {
            outputFile = new File(propertySpec.getOutputFile(), path);
        }
        if (entry.isDirectory()) {
            if (propertySpec.getOutputType() != OutputType.DIRECTORY) {
                throw new IllegalStateException(
                        "Property should be an output directory property: " + propertyName);
            }
            FileUtils.forceMkdir(outputFile);
        } else {
            // TODO:LPTR Can we save on doing this?
            Files.createParentDirs(outputFile);
            Files.asByteSink(outputFile).writeFrom(zipInput);
        }
    }
}

From source file:org.locationtech.geogig.plumbing.merge.MergeFeaturesOp.java

private Map<ObjectId, RevObject> getObjects(NodeRef ancestorRef, NodeRef nodeRefA, NodeRef nodeRefB) {

    final ObjectId metadataId = ancestorRef.getMetadataId();
    final ObjectId ancestorFeatureId = ancestorRef.getObjectId();
    final ObjectId featureAId = nodeRefA.getObjectId();
    final ObjectId featureBId = nodeRefB.getObjectId();
    Iterable<ObjectId> ids = ImmutableList.of(metadataId, ancestorFeatureId, featureAId, featureBId);
    Iterator<RevObject> objsit = objectDatabase().getAll(ids, BulkOpListener.NOOP_LISTENER);

    ImmutableMap<ObjectId, RevObject> map = Maps.uniqueIndex(objsit, (o) -> o.getId());
    checkState(map.containsKey(metadataId), "Invalid reference: %s", metadataId);
    checkState(map.containsKey(ancestorFeatureId), "Invalid reference: %s", ancestorFeatureId);
    checkState(map.containsKey(featureAId), "Invalid reference: %s", featureAId);
    checkState(map.containsKey(featureBId), "Invalid reference: %s", featureBId);
    return map;/*ww  w.  ja  v  a 2 s .c  o  m*/
}

From source file:org.eclipse.sw360.datahandler.thrift.ThriftUtils.java

public static <T> Map<String, T> getIdMap(Collection<T> in) {
    return Maps.uniqueIndex(in, extractId());
}

From source file:org.apache.whirr.HandlerMapFactory.java

static Map<String, ClusterActionHandlerFactory> indexFactoriesByRolePrefix(
        Iterable<ClusterActionHandlerFactory> handlers) {
    return Maps.uniqueIndex(handlers, new Function<ClusterActionHandlerFactory, String>() {

        @Override/*from   www.j av a2  s .  co m*/
        public String apply(ClusterActionHandlerFactory arg0) {
            return arg0.getRolePrefix();
        }

    });
}

From source file:org.jclouds.elb.domain.PolicyType.java

/**
 * convenience method/*from  w w  w.ja  v  a2s . co  m*/
 * @see #getAttributeMetadata()
 */
public Map<String, AttributeMetadata<?>> getAttributeMetadataByName() {
    return Maps.uniqueIndex(attributeMetadata, new Function<AttributeMetadata<?>, String>() {

        @Override
        public String apply(AttributeMetadata<?> input) {
            return input.getName();
        }

    });
}

From source file:org.polarsys.reqcycle.traceability.types.ExtensionPointReader.java

public Map<String, RegisteredAttribute> readAttributes() {
    Iterable<IConfigurationElement> allConf = filter(Arrays.asList(Platform.getExtensionRegistry()
            .getConfigurationElementsFor(Activator.PLUGIN_ID, EXT_ID_ATTRIBUTES)), IConfigurationElement.class);
    Iterable<TAttributeProvider> allProviders = transform(allConf, new Conf2AttProvider());
    Iterable<Iterable<RegisteredAttribute>> allTTypes = transform(allProviders, new Provider2TAType());
    Iterable<RegisteredAttribute> allTTypesFlattened = concat(allTTypes);
    return Maps.uniqueIndex(allTTypesFlattened, new Function<RegisteredAttribute, String>() {

        @Override//from   w  w  w .  j  a  v a2s . c o  m
        public String apply(RegisteredAttribute arg0) {
            return arg0.getId();
        }
    });
}

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

@Override
public Schema override(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;
    }//w w  w . j  a  va  2 s .c  o  m
    return putItems(others);
}

From source file:org.sonar.server.issue.filter.SearchAction.java

private Map<Long, IssueFilterFavouriteDto> searchUserFavouritesByFilterId(DbSession dbSession) {
    List<IssueFilterFavouriteDto> favouriteFilters = dbClient.issueFilterFavouriteDao().selectByUser(dbSession,
            userSession.getLogin());/*  ww  w.  j  a v  a  2  s  .  co m*/
    return Maps.uniqueIndex(favouriteFilters, IssueFilterFavouriteDToIssueFilterId.INSTANCE);
}

From source file:com.google.caliper.runner.ConsoleResultProcessor.java

@Override
public void processRun(Run run) {
    this.run = run;
    Map<String, VM> vms = Maps.uniqueIndex(run.vms, VM_LOCAL_NAME_FUNCTION);
    this.scenarioLocalVars = HashBasedTable.create();
    for (Scenario scenario : run.scenarios) {
        ScenarioName scenarioName = new ScenarioName(scenario.localName);
        scenarioLocalVars.put(scenarioName, new AxisName("benchmark"),
                new AxisValue(scenario.benchmarkMethodName));
        scenarioLocalVars.put(scenarioName, new AxisName("vm"),
                new AxisValue(vms.get(scenario.vmLocalName).vmName));
        for (Entry<String, String> entry : scenario.userParameters.entrySet()) {
            scenarioLocalVars.put(scenarioName, new AxisName(entry.getKey()), new AxisValue(entry.getValue()));
        }/*from   www. j a va2  s. c o m*/
        for (Entry<String, String> entry : scenario.vmArguments.entrySet()) {
            scenarioLocalVars.put(scenarioName, new AxisName(entry.getKey()), new AxisValue(entry.getValue()));
        }
    }

    for (Instrument instrument : run.instruments) {
        displayResults(instrument);
    }
}

From source file:org.trancecode.xproc.Environment.java

private static Map<PortReference, EnvironmentPort> getPortsMap(final Iterable<EnvironmentPort> ports) {
    return Maps.uniqueIndex(ports, PortFunctions.getPortReference());
}