Example usage for com.google.common.collect BiMap putIfAbsent

List of usage examples for com.google.common.collect BiMap putIfAbsent

Introduction

In this page you can find the example usage for com.google.common.collect BiMap putIfAbsent.

Prototype

default V putIfAbsent(K key, V value) 

Source Link

Document

If the specified key is not already associated with a value (or is mapped to null ) associates it with the given value and returns null , else returns the current value.

Usage

From source file:org.opencb.opencga.storage.core.metadata.StudyConfiguration.java

/**
 * Return all the indexed samples of an study plus the samples from a set of files.
 * Return a map between the sampleName and its position.
 *
 * @param studyConfiguration    Selected study
 * @param fileIds               Additional files to include
 * @return      Map between sampleName and position
 *///w ww .j av a2s .c  o m
public static BiMap<String, Integer> getIndexedSamplesPosition(StudyConfiguration studyConfiguration,
        int... fileIds) {
    Objects.requireNonNull(studyConfiguration, "StudyConfiguration is required");
    BiMap<String, Integer> samplesPosition = HashBiMap.create(studyConfiguration.getSampleIds().size());
    int position = 0;
    BiMap<Integer, String> idSamples = studyConfiguration.sampleIds.inverse();
    for (Integer indexedFileId : studyConfiguration.getIndexedFiles()) {
        for (Integer sampleId : studyConfiguration.getSamplesInFiles().get(indexedFileId)) {
            samplesPosition.putIfAbsent(idSamples.get(sampleId), position++);
        }
    }
    for (int fileId : fileIds) {
        for (Integer sampleId : studyConfiguration.getSamplesInFiles().get(fileId)) {
            samplesPosition.putIfAbsent(idSamples.get(sampleId), position++);
        }
    }
    return samplesPosition;
}