Example usage for com.google.common.collect Multimaps index

List of usage examples for com.google.common.collect Multimaps index

Introduction

In this page you can find the example usage for com.google.common.collect Multimaps index.

Prototype

public static <K, V> ImmutableListMultimap<K, V> index(Iterator<V> values, Function<? super V, K> keyFunction) 

Source Link

Document

Creates an index ImmutableListMultimap that contains the results of applying a specified function to each item in an Iterator of values.

Usage

From source file:org.apache.brooklyn.entity.network.bind.BindDnsServerImpl.java

public void update() {
    Lifecycle serverState = getAttribute(Attributes.SERVICE_STATE_ACTUAL);
    if (Lifecycle.STOPPED.equals(serverState) || Lifecycle.STOPPING.equals(serverState)
            || Lifecycle.DESTROYED.equals(serverState) || !getAttribute(Attributes.SERVICE_UP)) {
        LOG.debug("Skipped update of {} when service state is {} and running is {}",
                new Object[] { this, getAttribute(Attributes.SERVICE_STATE_ACTUAL), getAttribute(SERVICE_UP) });
        return;//from  w  ww  .  j a  v a2s . c o m
    }
    synchronized (this) {
        Iterable<Entity> availableEntities = FluentIterable.from(getEntities().getMembers())
                .filter(new HasHostnameAndValidLifecycle());
        LOG.debug("{} updating with entities: {}", this, Iterables.toString(availableEntities));
        ImmutableListMultimap<String, Entity> hostnameToEntity = Multimaps.index(availableEntities,
                new HostnameTransformer());

        Map<String, String> octetToName = Maps.newHashMap();
        BiMap<String, String> ipToARecord = HashBiMap.create();
        Multimap<String, String> aRecordToCnames = MultimapBuilder.hashKeys().hashSetValues().build();
        Multimap<String, String> ipToAllNames = MultimapBuilder.hashKeys().hashSetValues().build();

        for (Map.Entry<String, Entity> e : hostnameToEntity.entries()) {
            String domainName = e.getKey();
            Maybe<SshMachineLocation> location = Machines.findUniqueMachineLocation(e.getValue().getLocations(),
                    SshMachineLocation.class);
            if (!location.isPresent()) {
                LOG.debug("Member {} of {} does not have an SSH location so will not be configured",
                        e.getValue(), this);
                continue;
            } else if (ipToARecord.inverse().containsKey(domainName)) {
                continue;
            }

            String address = location.get().getAddress().getHostAddress();
            ipToAllNames.put(address, domainName);
            if (!ipToARecord.containsKey(address)) {
                ipToARecord.put(address, domainName);
                if (getReverseLookupNetwork().contains(new Cidr(address + "/32"))) {
                    String octet = Iterables.get(Splitter.on('.').split(address), 3);
                    if (!octetToName.containsKey(octet))
                        octetToName.put(octet, domainName);
                }
            } else {
                aRecordToCnames.put(ipToARecord.get(address), domainName);
            }
        }
        sensors().set(A_RECORDS, ImmutableMap.copyOf(ipToARecord.inverse()));
        sensors().set(PTR_RECORDS, ImmutableMap.copyOf(octetToName));
        sensors().set(CNAME_RECORDS, Multimaps.unmodifiableMultimap(aRecordToCnames));
        sensors().set(ADDRESS_MAPPINGS, Multimaps.unmodifiableMultimap(ipToAllNames));

        // Update Bind configuration files and restart the service
        getDriver().updateBindConfiguration();
    }
}

From source file:com.facebook.presto.raptor.storage.ShardCompactionManager.java

private void discoverShards() {
    if (shardsInProgress.size() >= MAX_PENDING_COMPACTIONS) {
        return;//from w w  w . j a  v a 2s .co  m
    }

    Set<ShardMetadata> allShards = shardManager.getNodeShards(currentNodeIdentifier);
    ListMultimap<Long, ShardMetadata> tableShards = Multimaps.index(allShards, ShardMetadata::getTableId);

    for (Entry<Long, List<ShardMetadata>> entry : Multimaps.asMap(tableShards).entrySet()) {
        long tableId = entry.getKey();
        if (!metadataDao.isCompactionEnabled(tableId)) {
            continue;
        }
        List<ShardMetadata> shardMetadata = entry.getValue();

        Set<ShardMetadata> shards = shardMetadata.stream().filter(this::needsCompaction)
                .filter(shard -> !shardsInProgress.contains(shard.getShardUuid())).collect(toSet());

        if (shards.size() <= 1) {
            continue;
        }

        Long temporalColumnId = metadataDao.getTemporalColumnId(tableId);
        CompactionSetCreator compactionSetCreator;
        if (temporalColumnId == null) {
            compactionSetCreator = new FileCompactionSetCreator(maxShardSize, maxShardRows);
        } else {
            Type type = metadataDao.getTableColumn(tableId, temporalColumnId).getDataType();
            compactionSetCreator = new TemporalCompactionSetCreator(maxShardSize, maxShardRows, type);
            shards = filterShardsWithTemporalMetadata(shardMetadata, tableId, temporalColumnId);
        }
        addToCompactionQueue(compactionSetCreator, tableId, shards);
    }
}

From source file:org.apache.druid.indexing.common.TaskToolbox.java

public void publishSegments(Iterable<DataSegment> segments) throws IOException {
    // Request segment pushes for each set
    final Multimap<Interval, DataSegment> segmentMultimap = Multimaps.index(segments,
            new Function<DataSegment, Interval>() {
                @Override/* w w w.  j av  a  2s.c  o  m*/
                public Interval apply(DataSegment segment) {
                    return segment.getInterval();
                }
            });
    for (final Collection<DataSegment> segmentCollection : segmentMultimap.asMap().values()) {
        getTaskActionClient().submit(new SegmentInsertAction(ImmutableSet.copyOf(segmentCollection)));
    }
}

From source file:edu.isi.karma.modeling.alignment.learner.SteinerNodes.java

private void computeCoherenceList() {

    if (nodes == null || nodes.size() == 0)
        return;// w  ww .  ja  v  a2  s.c  o m

    Map<String, Integer> patternSize = new HashMap<String, Integer>();
    Map<String, String> patternGuid = new HashMap<String, String>();
    int guidSize = new RandomGUID().toString().length();

    for (Node n : nodes) {
        for (String p : n.getModelIds()) {

            Integer size = patternSize.get(p);
            if (size == null)
                patternSize.put(p, 1);
            else
                patternSize.put(p, ++size);

            if (!patternGuid.containsKey(p)) {
                String guid = new RandomGUID().toString();
                patternGuid.put(p, guid);
            }
        }
    }

    // find the maximum pattern size
    int maxPatternSize = 0;
    for (Entry<String, Integer> entry : patternSize.entrySet()) {
        if (entry.getValue().intValue() > maxPatternSize)
            maxPatternSize = entry.getValue().intValue();
    }

    List<String> listOfNodesLargestPatterns = new ArrayList<String>();

    for (Node n : nodes) {
        List<String> patternIds = new ArrayList<String>(n.getModelIds());
        Collections.sort(patternIds);

        String[] nodeMaxPatterns = new String[maxPatternSize];
        Arrays.fill(nodeMaxPatterns, "");

        for (String p : patternIds) {
            int size = patternSize.get(p).intValue();
            nodeMaxPatterns[size - 1] += patternGuid.get(p);
        }
        for (int i = maxPatternSize - 1; i >= 0; i--) {
            if (nodeMaxPatterns[i] != null && nodeMaxPatterns[i].trim().length() > 0) {
                listOfNodesLargestPatterns.add(nodeMaxPatterns[i]);
                break;
            }
        }
    }

    Function<String, String> stringEqualiy = new Function<String, String>() {
        @Override
        public String apply(final String s) {
            return s;
        }
    };

    Multimap<String, String> index = Multimaps.index(listOfNodesLargestPatterns, stringEqualiy);

    this.coherenceList.clear();
    int x, y;
    for (String s : index.keySet()) {
        if (s.trim().length() == 0)
            continue;
        x = index.get(s).size();
        y = x > 0 ? index.get(s).iterator().next().length() / guidSize : 0;
        CoherenceItem ci = new CoherenceItem(x, y);
        this.coherenceList.add(ci);
    }

    Collections.sort(this.coherenceList);

}

From source file:org.obiba.opal.web.gwt.app.client.magma.view.SummaryTabView.java

private void renderTextSummary(SummaryStatisticsDto dto) {
    TextSummaryDto textSummaryDto = dto.getExtension(TextSummaryDto.SummaryStatisticsDtoExtensions.textSummary)
            .cast();/*from   ww w . j a  v  a  2 s .c  o  m*/

    final double[] totals = { 0d, 0d };
    ImmutableListMultimap<Boolean, FrequencyDto> valuesByMissing = Multimaps.index(
            JsArrays.toIterable(textSummaryDto.getFrequenciesArray()), new Function<FrequencyDto, Boolean>() {
                @Nullable
                @Override
                public Boolean apply(@Nullable FrequencyDto input) {
                    if (input != null && !input.getMissing()) {
                        totals[0] += input.getFreq();
                        return false;
                    }
                    totals[1] += input == null ? 0 : input.getFreq();
                    return true;
                }
            });

    summary.add(new TextSummaryView(textSummaryDto, valuesByMissing.get(false), valuesByMissing.get(true),
            totals[0], totals[1], textSummaryDto.getOtherFrequency(), DEFAULT_MAX_TEXT_RESULTS));
}

From source file:dagger.internal.codegen.ContributionBinding.java

/**
 * Indexes map-multibindings by map key annotation type.
 *///  w ww. j  a  va 2  s  .co m
static ImmutableSetMultimap<Wrapper<DeclaredType>, ContributionBinding> indexMapBindingsByAnnotationType(
        Set<ContributionBinding> mapBindings) {
    return ImmutableSetMultimap.copyOf(Multimaps.index(mapBindings,
            mapBinding -> MoreTypes.equivalence().wrap(mapBinding.mapKey().get().getAnnotationType())));
}

From source file:ibis.constellation.impl.TimerImpl.java

private <T> List<TimerImpl> groupBy(Function<TimerEvent, T> f) {
    Multimap<T, TimerEvent> index = Multimaps.index(events, f);
    ArrayList<TimerImpl> timers = new ArrayList<TimerImpl>();
    for (T t : index.keySet()) {
        TimerImpl timer = new TimerImpl(hostId);
        timer.events.addAll(index.get(t));
        // Collections.sort(timer.events);
        timers.add(timer);//from w  w w .  ja  v  a  2s  . c o  m
    }
    return timers;
}

From source file:com.b2international.snowowl.snomed.datastore.index.change.ConceptChangeProcessor.java

@Override
public void process(ICDOCommitChangeSet commitChangeSet, RevisionSearcher searcher) throws IOException {
    // process concept deletions first
    deleteRevisions(SnomedConceptDocument.class,
            commitChangeSet.getDetachedComponents(SnomedPackage.Literals.CONCEPT));
    // collect member changes
    this.referringRefSets = HashMultimap.create(memberChangeProcessor.process(commitChangeSet, searcher));

    // collect new and dirty reference sets
    final Map<String, SnomedRefSet> newAndDirtyRefSetsById = newHashMap(FluentIterable
            .from(Iterables.concat(commitChangeSet.getNewComponents(), commitChangeSet.getDirtyComponents()))
            .filter(SnomedRefSet.class).uniqueIndex(new Function<SnomedRefSet, String>() {
                @Override/*from   w w  w. j  av  a  2  s.c om*/
                public String apply(SnomedRefSet input) {
                    return input.getIdentifierId();
                }
            }));
    // collect deleted reference sets
    final Set<Long> deletedRefSets = newHashSet(CDOIDUtils.createCdoIdToLong(
            commitChangeSet.getDetachedComponents(SnomedRefSetPackage.Literals.SNOMED_REF_SET)));

    // index new concepts
    for (final Concept concept : commitChangeSet.getNewComponents(Concept.class)) {
        final String id = concept.getId();
        final Builder doc = SnomedConceptDocument.builder().id(id);
        update(doc, concept, null);
        SnomedRefSet refSet = newAndDirtyRefSetsById.remove(id);
        if (refSet != null) {
            doc.refSet(refSet);
        }
        doc.preferredDescriptions(toDescriptionFragments(concept));
        indexNewRevision(concept.cdoID(), doc.build());
    }

    // collect dirty concepts for reindex
    final Map<String, Concept> dirtyConceptsById = Maps
            .uniqueIndex(commitChangeSet.getDirtyComponents(Concept.class), Concept::getId);

    final Set<String> dirtyConceptIds = collectDirtyConceptIds(searcher, commitChangeSet);

    final Multimap<String, Description> dirtyDescriptionsByConcept = Multimaps
            .index(getDirtyDescriptions(commitChangeSet), d -> d.getConcept().getId());

    // remaining new and dirty reference sets should be connected to a non-new concept, so add them here
    dirtyConceptIds.addAll(newAndDirtyRefSetsById.keySet());
    dirtyConceptIds.addAll(dirtyDescriptionsByConcept.keySet());

    if (!dirtyConceptIds.isEmpty()) {
        // fetch all dirty concept documents by their ID
        final Query<SnomedConceptDocument> query = Query.select(SnomedConceptDocument.class)
                .where(SnomedConceptDocument.Expressions.ids(dirtyConceptIds)).limit(dirtyConceptIds.size())
                .build();
        final Map<String, SnomedConceptDocument> currentConceptDocumentsById = Maps
                .uniqueIndex(searcher.search(query), ComponentUtils.<String>getIdFunction());

        // update dirty concepts
        for (final String id : dirtyConceptIds) {
            final Concept concept = dirtyConceptsById.get(id);
            final SnomedConceptDocument currentDoc = currentConceptDocumentsById.get(id);
            if (currentDoc == null) {
                throw new IllegalStateException("Current concept revision should not be null for: " + id);
            }
            final Builder doc = SnomedConceptDocument.builder(currentDoc);
            update(doc, concept, currentDoc);
            SnomedRefSet refSet = newAndDirtyRefSetsById.remove(id);
            if (refSet != null) {
                doc.refSet(refSet);
            }
            // clear refset props when deleting refset
            if (deletedRefSets.contains(currentDoc.getRefSetStorageKey())) {
                doc.clearRefSet();
            }

            if (concept != null) {
                doc.preferredDescriptions(toDescriptionFragments(concept));
            } else {
                Collection<Description> dirtyDescriptions = dirtyDescriptionsByConcept.get(id);
                if (!dirtyDescriptions.isEmpty()) {
                    Map<String, SnomedDescriptionFragment> newDescriptions = newHashMap(Maps.uniqueIndex(
                            currentDoc.getPreferredDescriptions(), SnomedDescriptionFragment::getId));
                    for (Description dirtyDescription : dirtyDescriptions) {
                        newDescriptions.remove(dirtyDescription.getId());
                        if (dirtyDescription.isActive()
                                && !getPreferredLanguageMembers(dirtyDescription).isEmpty()) {
                            newDescriptions.put(dirtyDescription.getId(),
                                    toDescriptionFragment(dirtyDescription));
                        }
                    }
                    doc.preferredDescriptions(newDescriptions.values().stream()
                            .sorted(DESCRIPTION_FRAGMENT_ORDER).collect(Collectors.toList()));
                } else {
                    doc.preferredDescriptions(currentDoc.getPreferredDescriptions());
                }
            }

            if (concept != null) {
                indexChangedRevision(concept.cdoID(), doc.build());
            } else {
                indexChangedRevision(currentDoc.getStorageKey(), doc.build());
            }
        }
    }
}

From source file:org.opentestsystem.authoring.testauth.validation.ItemSelectionAlgorithmParameterValidator.java

@Override
public void validate(final Object obj, final Errors e) {
    final ItemSelectionAlgorithmParameter parameter = (ItemSelectionAlgorithmParameter) obj;

    if (parameter.getItemSelectionType() != null) {
        final Object[] errorArgs = new String[] { parameter.getItemSelectionType().name(),
                parameter.getParameterName() };

        switch (parameter.getItemSelectionType()) {
        case FLOAT:
        case INTEGER:
            // defaultValue validated via jsr-303
            rejectIfTooLong(e, DEFAULT_FIELD, MAX_PARSEABLE_LENGTH, errorArgs);

            final boolean isFloat = parameter.getItemSelectionType() == ItemSelectionType.FLOAT;
            rejectIfNotFloatOrIntegerParseable(e, DEFAULT_FIELD, isFloat, errorArgs);
            rejectIfNotFloatOrIntegerParseable(e, MINIMUM_FIELD, isFloat, errorArgs);
            rejectIfNotFloatOrIntegerParseable(e, MAXIMUM_FIELD, isFloat, errorArgs);
            if (!e.hasErrors()) {
                boolean compareDefaultToMinMax = true;
                if (StringUtils.isNotBlank(parameter.getMinimumValue())
                        && StringUtils.isNotBlank(parameter.getMaximumValue())) {
                    final BigDecimal minimumBigDecimal = new BigDecimal(parameter.getMinimumValue());
                    final BigDecimal maximumBigDecimal = new BigDecimal(parameter.getMaximumValue());
                    if (minimumBigDecimal.compareTo(maximumBigDecimal) > -1) {
                        rejectValue(e, MINIMUM_FIELD, getErrorMessageRoot() + MINIMUM_FIELD + MSG_LESS_THAN_MAX,
                                errorArgs);
                        compareDefaultToMinMax = false;
                    }//from  ww w.  j ava2 s . co m
                }
                if (StringUtils.isNotBlank(parameter.getDefaultValue())) {
                    final BigDecimal defaultBigDecimal = new BigDecimal(parameter.getDefaultValue());
                    if (StringUtils.isNotBlank(parameter.getMinimumValue())) {
                        final BigDecimal minimumBigDecimal = new BigDecimal(parameter.getMinimumValue());
                        if (compareDefaultToMinMax && defaultBigDecimal.compareTo(minimumBigDecimal) < 0) {
                            rejectValue(e, DEFAULT_FIELD,
                                    getErrorMessageRoot() + DEFAULT_FIELD + MSG_WITHIN_MIN_MAX, errorArgs);
                        }
                    }
                    if (StringUtils.isNotBlank(parameter.getMaximumValue())) {
                        final BigDecimal maximumBigDecimal = new BigDecimal(parameter.getMaximumValue());
                        if (compareDefaultToMinMax && defaultBigDecimal.compareTo(maximumBigDecimal) > 0) {
                            rejectValue(e, DEFAULT_FIELD,
                                    getErrorMessageRoot() + DEFAULT_FIELD + MSG_WITHIN_MIN_MAX, errorArgs);
                        }
                    }
                }
            }
            rejectIfCollectionNotEmpty(e, ITEM_LIST_FIELD, errorArgs);
            break;
        case BOOLEAN:
            rejectIfNotEmpty(e, MINIMUM_FIELD, errorArgs);
            rejectIfNotEmpty(e, MAXIMUM_FIELD, errorArgs);
            rejectIfNotBooleanParseable(e, DEFAULT_FIELD, errorArgs);
            rejectIfCollectionNotEmpty(e, ITEM_LIST_FIELD, errorArgs);
            break;
        case LIST:
            rejectIfNotEmpty(e, MINIMUM_FIELD, errorArgs);
            rejectIfNotEmpty(e, MAXIMUM_FIELD, errorArgs);
            rejectIfCollectionEmpty(e, ITEM_LIST_FIELD, errorArgs);

            if (parameter.getItemSelectionList() != null) {
                if (Iterables.any(parameter.getItemSelectionList(), ITEM_SELECTION_PARAMETER_ELEMENT_BLANK)) {
                    rejectValue(e, ITEM_LIST_FIELD, getErrorMessageRoot() + ITEM_LIST_NAME_FIELD + MSG_REQUIRED,
                            errorArgs);
                }
                // check if defaultValue is one of values in selection list
                rejectIfNotInCollection(e, DEFAULT_FIELD, Lists.transform(parameter.getItemSelectionList(),
                        ITEM_SELECTION_PARAMETER_ELEMENT_TO_NAME_TRANSFORMER), errorArgs);

                // check if list has duplicate names and duplicate or out-of-range positions
                final Map<String, Collection<ItemSelectionParameterElement>> duplicates = Maps.filterEntries(
                        Multimaps.index(parameter.getItemSelectionList(),
                                ITEM_SELECTION_PARAMETER_ELEMENT_TO_NAME_TRANSFORMER).asMap(),
                        ITEM_SELECTION_PARAMETER_ELEMENT_DUPLICATE_FILTER);
                if (!duplicates.isEmpty()) {
                    rejectValue(e, ITEM_LIST_FIELD,
                            getErrorMessageRoot() + ITEM_LIST_NAME_FIELD + MSG_DUPLICATES,
                            duplicates.keySet().toString());
                }
                final Map<String, Collection<ItemSelectionParameterElement>> duplicatePositions = Maps
                        .filterEntries(
                                Multimaps
                                        .index(parameter.getItemSelectionList(),
                                                ITEM_SELECTION_PARAMETER_ELEMENT_TO_POSITION_TRANSFORMER)
                                        .asMap(),
                                ITEM_SELECTION_PARAMETER_ELEMENT_DUPLICATE_FILTER);
                if (!duplicatePositions.isEmpty()) {
                    rejectValue(e, ITEM_LIST_FIELD,
                            getErrorMessageRoot() + ITEM_LIST_POSITION_FIELD + MSG_DUPLICATES,
                            duplicatePositions.keySet().toString());
                }
                final List<String> outOfRange = Lists.newArrayList(Iterables.transform(
                        Iterables.filter(parameter.getItemSelectionList(),
                                PARAMETER_POSITION_OUT_OF_RANGE_FILTER
                                        .getInstance(parameter.getItemSelectionList().size())),
                        ITEM_SELECTION_PARAMETER_ELEMENT_TO_POSITION_TRANSFORMER));
                if (outOfRange.size() > 0) {
                    rejectValue(e, ITEM_LIST_FIELD,
                            getErrorMessageRoot() + ITEM_LIST_POSITION_FIELD + MSG_INVALID,
                            outOfRange.toString());
                }
            }
            break;
        case STRING:
            rejectIfNotEmpty(e, MINIMUM_FIELD, errorArgs);
            rejectIfNotEmpty(e, MAXIMUM_FIELD, errorArgs);
            rejectIfCollectionNotEmpty(e, ITEM_LIST_FIELD, errorArgs);
            rejectIfTooLong(e, DEFAULT_FIELD, MAXIMUM_DEFAULT_STRING_LENGTH, errorArgs);
            break;
        default:
            rejectValue(e, ITEM_TYPE_FIELD, getErrorMessageRoot() + ITEM_TYPE_FIELD + MSG_INVALID, errorArgs);
            break;
        }
    }
}

From source file:com.github.aptd.simulation.datamodel.CXMLReader.java

/**
 * create the station list//from  w ww. j  ava  2s. c  o  m
 *
 * @param p_network network component
 * @param p_agents map with agent asl scripts
 * @param p_factory factory
 * @param p_time time reference
 * @param p_platforms map with already generated platforms
 * @return unmodifyable map with stations
 */
private static Map<String, IStation<?>> station(final Network p_network, final Map<String, String> p_agents,
        final IFactory p_factory, final ITime p_time, final Map<String, IPlatform<?>> p_platforms) {
    final Map<String, IElement.IGenerator<IStation<?>>> l_generators = new ConcurrentHashMap<>();
    final Set<IAction> l_actions = CCommon.actionsFromPackage().collect(Collectors.toSet());
    final ListMultimap<String, IPlatform<?>> l_platformbystationid = Multimaps.index(p_platforms.values(),
            IPlatform::stationid);
    return Collections.<String, IStation<?>>unmodifiableMap(p_network
            .getInfrastructure().getOperationControlPoints().getOcp().parallelStream().filter(
                    i -> hasagentname(i.getAny()))
            .map(i -> agentname(i, i.getAny()))
            .map(i -> l_generators
                    .computeIfAbsent(i.getRight(),
                            a -> stationgenerator(p_factory, p_agents.get(i.getRight()), l_actions, p_time))
                    .generatesingle(i.getLeft().getId(), i.getLeft().getGeoCoord().getCoord().get(0),
                            i.getLeft().getGeoCoord().getCoord().get(1),
                            l_platformbystationid.get(i.getLeft().getId())))
            .collect(Collectors.toMap(IElement::id, i -> i)));
}