Java Code Examples for com.google.common.collect.TreeMultimap#putAll()

The following examples show how to use com.google.common.collect.TreeMultimap#putAll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExpressionFormatter.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public String visitObjectLiteral(ObjectLiteral node, Void context) {
    StringBuilder builder = new StringBuilder("{");
    boolean first = true;
    TreeMultimap<String, Expression> sorted = TreeMultimap.create(
            Ordering.natural().nullsLast(),
            Ordering.usingToString().nullsLast()
    );
    sorted.putAll(node.values());
    for (Map.Entry<String, Expression> entry : sorted.entries()) {
        if (!first) {
            builder.append(", ");
        } else {
            first = false;
        }
        builder.append(formatIdentifier(entry.getKey()))
               .append("= ")
               .append(entry.getValue().accept(this, context));

    }
    return builder.append("}").toString();
}
 
Example 2
Source File: Intersection.java    From datawave with Apache License 2.0 5 votes vote down vote up
static TreeMultimap<String,IndexStream> pivot(TreeMultimap<String,IndexStream> children) {
    TreeMultimap<String,IndexStream> newChildren = TreeMultimap.create(Ordering.natural(), Ordering.arbitrary());
    final String max = children.keySet().last();
    newChildren.putAll(max, children.removeAll(max));
    for (IndexStream itr : children.values()) {
        // move this iterator forward until we get to a key past the max processed thus far
        String dayOrShard = null;
        while (itr.hasNext()) {
            
            dayOrShard = key(itr.peek());
            if (dayOrShard.compareTo(max) >= 0) {
                break;
            }
            if (isDay(dayOrShard) && max.startsWith(dayOrShard)) {
                // use the existing max instead of the day to add to the list
                dayOrShard = max;
                break;
            }
            itr.next();
        }
        // add the item into our map
        if (itr.hasNext()) {
            newChildren.put(dayOrShard, itr);
        } else {
            // nobody has anything past max, so no intersection
            return TreeMultimap.create(Ordering.natural(), Ordering.arbitrary());
        }
    }
    return newChildren;
}
 
Example 3
Source File: NestedIteratorContextUtil.java    From datawave with Apache License 2.0 5 votes vote down vote up
/**
 * Apply a context to a List of contextRequiredIterators as either a union or intersection, taking into account previous state of the headmap and
 * nullHeadMap
 *
 * @param context
 *            the context to be used to move the contextRequiredIterators
 * @param contextRequiredIterators
 *            the iterators to union against that require context
 * @param headMap
 *            the contextRequiredIterators head map
 * @param nullHeadMap
 *            the contextRequiredIterators null head map. This is required to track when a move resulted in no result from the iterator and will always be
 *            moved
 * @param transformer
 *            transformer to apply to all results from the contextRequiredIterators
 * @param union
 *            if set to true apply the context as a union with the contextRequiredIterators, otherwise apply as an intersection
 */
private static <T> T applyContext(T context, List<NestedIterator<T>> contextRequiredIterators, TreeMultimap<T,NestedIterator<T>> headMap,
                TreeMultimap<T,NestedIterator<T>> nullHeadMap, Util.Transformer<T> transformer, boolean union) {
    if (context == null) {
        return null;
    }
    
    // no union with nothing
    if (headMap == null) {
        return null;
    }
    
    Set<NestedIterator<T>> sourcesToMove = getIteratorsToMove(context, contextRequiredIterators, headMap, nullHeadMap);
    // null iterators from previous passes should always be moved again
    Set<NestedIterator<T>> nullSources = processMoves(context, sourcesToMove, headMap, transformer);
    
    T headMapKey;
    if (union) {
        // union
        headMapKey = headMap.isEmpty() ? null : headMap.keySet().first();
    } else {
        // intersection
        headMapKey = headMap.isEmpty() ? null : headMap.keySet().last();
    }
    
    // check for nulls first, since a null source means that the move on the iterator yielded no matches
    if (nullSources.size() > 0) {
        // add them to the nullHeads with the current key
        nullHeadMap.putAll(context, nullSources);
        
        if (union && nullSources.size() == sourcesToMove.size() || !union) {
            return null;
        }
    }
    
    // valid headMapKey to return
    return headMapKey;
}
 
Example 4
Source File: WriteAppModuleMetadataStep.java    From buck with Apache License 2.0 5 votes vote down vote up
private static TreeMultimap<APKModule, String> sortModuleToStringsMultimap(
    ImmutableMultimap<APKModule, String> multimap) {
  TreeMultimap<APKModule, String> orderedMap =
      TreeMultimap.create(
          (left, right) -> left.getName().compareTo(right.getName()), Ordering.natural());
  orderedMap.putAll(multimap);
  return orderedMap;
}
 
Example 5
Source File: TermFrequencyList.java    From datawave with Apache License 2.0 4 votes vote down vote up
public static TermFrequencyList merge(TermFrequencyList list1, TermFrequencyList list2) {
    TreeMultimap<Zone,TermWeightPosition> offsetsPerField = TreeMultimap.create();
    offsetsPerField.putAll(list1.offsetsPerField);
    offsetsPerField.putAll(list2.offsetsPerField);
    return new TermFrequencyList(offsetsPerField);
}
 
Example 6
Source File: MemPool.java    From snowblossom with Apache License 2.0 4 votes vote down vote up
public synchronized List<Transaction> getTransactionsForBlock(ChainHash last_utxo, int max_size)
{
  List<Transaction> block_list = new ArrayList<Transaction>();
  Set<ChainHash> included_txs = new HashSet<>();


  if (!last_utxo.equals(utxo_for_pri_map))
  {
    rebuildPriorityMap(last_utxo);
  }

  int size = 0;
  int low_fee_size = 0;


  TreeMultimap<Double, TXCluster> priority_map_copy = TreeMultimap.<Double, TXCluster>create();
  priority_map_copy.putAll(priority_map);

  while (priority_map_copy.size() > 0)
  {
    Map.Entry<Double, Collection<TXCluster> > last_entry = priority_map_copy.asMap().pollLastEntry();

    double ratio = last_entry.getKey();
    boolean low_fee = false;
    if (ratio < Globals.LOW_FEE) low_fee=true;

    Collection<TXCluster> list = last_entry.getValue();
    for (TXCluster cluster : list)
    {
      if (size + cluster.total_size <= max_size)
      {
        if ((!low_fee) || (low_fee_size < low_fee_max))
        {

          for (Transaction tx : cluster.tx_list)
          {
            ChainHash tx_hash = new ChainHash(tx.getTxHash());
            if (!included_txs.contains(tx_hash))
            {
              block_list.add(tx);
              included_txs.add(tx_hash);
              int sz = tx.toByteString().size();
              size += sz;
              if (low_fee)
              {
                low_fee_size += sz;
              }
            }
          }
        }

      }
    }
  }
  return block_list;

}
 
Example 7
Source File: FilteringSchemaContextProxy.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Filters SchemaContext for yang modules.
 *
 * @param delegate original SchemaContext
 * @param rootModules modules (yang schemas) to be available and all their dependencies (modules importing
 *                    rootModule and whole chain of their imports)
 * @param additionalModuleIds (additional) modules (yang schemas) to be available and whole chain of their imports
 */
public FilteringSchemaContextProxy(final SchemaContext delegate, final Collection<ModuleId> rootModules,
        final Set<ModuleId> additionalModuleIds) {
    requireNonNull(rootModules, "Base modules cannot be null.");
    requireNonNull(additionalModuleIds, "Additional modules cannot be null.");

    final Builder<Module> filteredModulesBuilder = new Builder<>();

    // preparing map to get all modules with one name but difference in revision
    final TreeMultimap<String, Module> nameToModulesAll = TreeMultimap.create(String::compareTo,
        REVISION_COMPARATOR);

    nameToModulesAll.putAll(getStringModuleMap(delegate));

    // in case there is a particular dependency to view filteredModules/YANG models dependency is checked
    // for module name and imports
    processForRootModules(delegate, rootModules, filteredModulesBuilder);

    // adding additional modules
    processForAdditionalModules(delegate, additionalModuleIds, filteredModulesBuilder);

    filteredModulesBuilder.addAll(getImportedModules(
            Maps.uniqueIndex(delegate.getModules(), ModuleId.MODULE_TO_MODULE_ID::apply),
            filteredModulesBuilder.build(), nameToModulesAll));

    /**
     * Instead of doing this on each invocation of getModules(), pre-compute it once and keep it around.
     */
    final List<Module> sortedModules = new ArrayList<>(filteredModulesBuilder.build());
    sortedModules.sort(NAME_REVISION_COMPARATOR);
    this.filteredModules = ImmutableSet.copyOf(sortedModules);

    final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(new TreeMap<>(),
        AbstractSchemaContext::createModuleSet);
    final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(new TreeMap<>(),
        AbstractSchemaContext::createModuleSet);
    final ImmutableMap.Builder<QNameModule, Module> moduleMapBuilder = ImmutableMap.builder();
    for (final Module module : filteredModules) {
        nameMap.put(module.getName(), module);
        nsMap.put(module.getNamespace(), module);
        moduleMapBuilder.put(module.getQNameModule(), module);
    }

    namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
    nameToModules = ImmutableSetMultimap.copyOf(nameMap);
    moduleMap = moduleMapBuilder.build();
}
 
Example 8
Source File: WriteAppModuleMetadataStep.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
public StepExecutionResult execute(ExecutionContext context) {
  try {
    // Get module to classes map in sorted order for build determinism and testing
    ProguardTranslatorFactory translatorFactory =
        ProguardTranslatorFactory.create(
            filesystem, proguardFullConfigFile, proguardMappingFile, skipProguard);
    TreeMultimap<APKModule, String> orderedModuleToClassesMap = null;
    if (shouldIncludeClasses) {
      ImmutableMultimap<APKModule, String> moduleToClassesMap =
          APKModuleGraph.getAPKModuleToClassesMap(
              apkModuleToJarPathMap, translatorFactory.createObfuscationFunction(), filesystem);
      orderedModuleToClassesMap = sortModuleToStringsMultimap(moduleToClassesMap);
    }

    TreeMultimap<APKModule, String> orderedModuleToTargetsMap =
        TreeMultimap.create(
            (left, right) -> left.getName().compareTo(right.getName()), Ordering.natural());
    for (APKModule module : apkModuleGraph.getAPKModules()) {
      for (BuildTarget target : apkModuleGraph.getBuildTargets(module)) {
        orderedModuleToTargetsMap.put(module, target.getFullyQualifiedName());
      }
    }

    TreeMultimap<APKModule, String> orderedModuleToLibrariesMap =
        TreeMultimap.create(Comparator.comparing(APKModule::getName), Ordering.natural());
    if (apkModuleToNativeLibraryMap.isPresent()) {
      orderedModuleToLibrariesMap.putAll(apkModuleToNativeLibraryMap.get());
    }

    // Module to module deps map is already sorted
    SortedMap<APKModule, ? extends SortedSet<APKModule>> moduleToDepsMap =
        apkModuleGraph.toOutgoingEdgesMap();

    // Write metadata lines to output
    LinkedList<String> metadataLines = new LinkedList<>();
    if (orderedModuleToClassesMap != null) {
      metadataLines.add(CLASS_SECTION_HEADER);
      writeModuleToStringsMultimap(orderedModuleToClassesMap, metadataLines);
    }
    metadataLines.add(TARGETS_SECTION_HEADER);
    writeModuleToStringsMultimap(orderedModuleToTargetsMap, metadataLines);
    metadataLines.add(DEPS_SECTION_HEADER);
    writeModuleToModulesMap(moduleToDepsMap, metadataLines);

    // Add libraries metadata
    if (apkModuleToNativeLibraryMap.isPresent()) {
      metadataLines.add(LIBRARIES_SECTION_HEADER);
      writeModuleToStringsMultimap(orderedModuleToLibrariesMap, metadataLines);
    }
    filesystem.writeLinesToPath(metadataLines, metadataOutput);

    return StepExecutionResults.SUCCESS;
  } catch (IOException e) {
    context.logError(e, "There was an error running WriteAppModuleMetadataStep.");
    return StepExecutionResults.ERROR;
  }
}