com.google.common.collect.HashMultiset#create ( )源码实例Demo

下面列出了com.google.common.collect.HashMultiset#create ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: log-synth   文件: HeaderSamplerTest.java
@Test
public void testUserAgent() throws IOException {
    HeaderSampler s = new HeaderSampler();
    s.setType("normal");
    Multiset<String> agents = HashMultiset.create();
    for (int i = 0; i < 1000; i++) {
        agents.add(s.userAgent());
    }
    assertTrue(agents.elementSet().size() > 100);

    s.setType("ababil");
    agents = HashMultiset.create();
    for (int i = 0; i < 1000; i++) {
        agents.add(s.userAgent());
    }
    assertEquals(1, agents.elementSet().size());
}
 
源代码2 项目: batfish   文件: SwitchedVlanPropertiesAnswerer.java
/**
 * Gets properties of switched vlans.
 *
 * @param ctxt context in which to apply {@code interfacesSpecifier}
 * @param configurations configuration to use in extractions
 * @param nodes the set of nodes to consider
 * @param interfaceSpecifier Specifies which interfaces to consider
 * @param columns a map from column name to {@link ColumnMetadata}
 * @return A multiset of {@link Row}s where each row corresponds to a node/vlan pair and columns
 *     correspond to property values.
 */
public static Multiset<Row> getProperties(
    SpecifierContext ctxt,
    Map<String, Configuration> configurations,
    Set<String> nodes,
    InterfaceSpecifier interfaceSpecifier,
    boolean excludeShutInterfaces,
    IntegerSpace vlans,
    Map<String, ColumnMetadata> columns) {
  Multiset<Row> rows = HashMultiset.create();
  for (String node : nodes) {
    Map<Integer, ImmutableSet.Builder<NodeInterfacePair>> switchedVlanInterfaces =
        new HashMap<>();
    ImmutableMap.Builder<Integer, Integer> vlanVnisBuilder = ImmutableMap.builder();
    computeNodeVlanProperties(
        ctxt,
        configurations.get(node),
        interfaceSpecifier,
        excludeShutInterfaces,
        vlans,
        switchedVlanInterfaces,
        vlanVnisBuilder);
    populateNodeRows(node, switchedVlanInterfaces, vlanVnisBuilder, columns, rows);
  }
  return rows;
}
 
源代码3 项目: nomulus   文件: BaseDomainLabelList.java
/**
 * Turns the list CSV data into a map of labels to parsed data of type R.
 *
 * @param lines the CSV file, line by line
 */
public ImmutableMap<String, R> parse(Iterable<String> lines) {
  Map<String, R> labelsToEntries = new HashMap<>();
  Multiset<String> duplicateLabels = HashMultiset.create();
  for (String line : lines) {
    R entry = createFromLine(line);
    if (entry == null) {
      continue;
    }
    String label = entry.getLabel();
    // Check if the label was already processed for this list (which is an error), and if so,
    // accumulate it so that a list of all duplicates can be thrown.
    if (labelsToEntries.containsKey(label)) {
      duplicateLabels.add(label, duplicateLabels.contains(label) ? 1 : 2);
    } else {
      labelsToEntries.put(label, entry);
    }
  }
  if (!duplicateLabels.isEmpty()) {
    throw new IllegalStateException(
        String.format(
            "List '%s' cannot contain duplicate labels. Dupes (with counts) were: %s",
            name, duplicateLabels));
  }
  return ImmutableMap.copyOf(labelsToEntries);
}
 
源代码4 项目: tracecompass   文件: ImageHelper.java
/**
 * Get the color histogram of the image
 *
 * @return The color density of the image
 */
public Multiset<RGB> getHistogram() {
    Multiset<RGB> colors = HashMultiset.create();
    for (int pixel : fPixels) {
        RGB pixelColor = getRgbFromRGBPixel(pixel);
        colors.add(pixelColor);
    }
    return colors;
}
 
/**
 * Add a single element to the CPD given the context.
 * 
 * @param element
 * @param given
 */
public void addElement(final A element, final B given) {
	final Multiset<A> elements;

	if (!table.containsKey(given)) {
		elements = HashMultiset.create();
		table.put(given, elements);
	} else {
		elements = table.get(given);
	}

	elements.add(element);
}
 
@Test
public void multiNodeClusterNonNormalizedAffinities() throws Exception {
  final Wrapper wrapper = newWrapper(2000, 1, 250,
      ImmutableList.of(
          new EndpointAffinity(N1_EP2, 15, true, 50),
          new EndpointAffinity(N2_EP2, 15, true, 50),
          new EndpointAffinity(N3_EP1, 10, true, 50),
          new EndpointAffinity(N4_EP2, 20, true, 50),
          new EndpointAffinity(N1_EP1, 20, true, 50)
      ));
  INSTANCE.parallelizeFragment(wrapper, newParameters(100, 20, 80), null);

  // Expect the fragment parallelization to be 20 because:
  // 1. the cost (2000) is above the threshold (SLICE_TARGET_DEFAULT) (which gives 2000/100=20 width) and
  // 2. Number of mandatory node assignments are 5 (current width 200 satisfies the requirement)
  // 3. max width per node is 20 which limits the width to 100, but existing width (20) is already less
  assertEquals(20, wrapper.getWidth());

  final List<NodeEndpoint> assignedEps = wrapper.getAssignedEndpoints();
  assertEquals(20, assignedEps.size());
  final HashMultiset<NodeEndpoint> counts = HashMultiset.create();
  for(final NodeEndpoint ep : assignedEps) {
    counts.add(ep);
  }
  // Each node gets at max 5.
  assertThat(counts.count(N1_EP2), CoreMatchers.allOf(greaterThan(1), lessThanOrEqualTo(5)));
  assertThat(counts.count(N2_EP2), CoreMatchers.allOf(greaterThan(1), lessThanOrEqualTo(5)));
  assertThat(counts.count(N3_EP1), CoreMatchers.allOf(greaterThan(1), lessThanOrEqualTo(5)));
  assertThat(counts.count(N4_EP2), CoreMatchers.allOf(greaterThan(1), lessThanOrEqualTo(5)));
  assertThat(counts.count(N1_EP1), CoreMatchers.allOf(greaterThan(1), lessThanOrEqualTo(5)));
}
 
源代码7 项目: gef   文件: ObservableMultisetTests.java
@Test
public void add_withCount() {
	// prepare backup multiset
	Multiset<Integer> backupMultiset = HashMultiset.create();
	check(observable, backupMultiset);

	// register listeners
	registerListeners();

	// add zero occurrences (no change expected)
	assertEquals(backupMultiset.add(1, 0), observable.remove(1, 0));
	check(observable, backupMultiset);
	invalidationListener.check();
	multisetChangeListener.check();

	// add a single value multiple times
	invalidationListener.expect(1);
	multisetChangeListener.addAtomicExpectation();
	multisetChangeListener.addElementaryExpection(5, 0, 5);
	assertEquals(backupMultiset.add(5, 5), observable.add(5, 5));
	check(observable, backupMultiset);
	checkListeners();

	// add a value zero times (no events should occur)
	assertEquals(backupMultiset.add(1, 0), observable.add(1, 0));
	check(observable, backupMultiset);
	checkListeners();
}
 
源代码8 项目: angelix   文件: TestTreeBoundedSynthesis.java
@Test
public void testForbiddenChoice() {

    Multiset<Node> components = HashMultiset.create();
    components.add(x);
    components.add(y);
    components.add(Library.ADD);

    ArrayList<TestCase> testSuite = new ArrayList<>();
    Map<ProgramVariable, Node> assignment1 = new HashMap<>();
    assignment1.put(x, IntConst.of(1));
    assignment1.put(y, IntConst.of(1));
    testSuite.add(TestCase.ofAssignment(assignment1, IntConst.of(2)));

    Map<ProgramVariable, Node> assignment2 = new HashMap<>();
    assignment2.put(x, IntConst.of(1));
    assignment2.put(y, IntConst.of(2));
    testSuite.add(TestCase.ofAssignment(assignment2, IntConst.of(3)));

    List<Expression> forbidden = new ArrayList<>();
    Map<Hole, Expression> args = new HashMap<>();
    args.put((Hole) Library.ADD.getLeft(), Expression.leaf(x));
    args.put((Hole) Library.ADD.getRight(), Expression.leaf(y));
    forbidden.add(Expression.app(Library.ADD, args));

    Synthesis synthesizerWithForbidden =
            new Synthesis(new BoundedShape(2, forbidden), new TreeBoundedEncoder(false));
    Optional<Pair<Expression, Map<Parameter, Constant>>> result = synthesizerWithForbidden.synthesize(testSuite, components);
    assertTrue(result.isPresent());
    Node node = result.get().getLeft().getSemantics(result.get().getRight());
    assertEquals(node, new Add(y, x));
}
 
源代码9 项目: tac-kbp-eal   文件: ByEventTypeResultWriter.java
static Multiset<Symbol> gatherEventTypesSeen(
    final Iterable<EALScorer2015Style.Result> perDocResults) {
  final Multiset<Symbol> eventTypesSeen = HashMultiset.create();
  for (final EALScorer2015Style.Result perDocResult : perDocResults) {
    for (final TypeRoleFillerRealis trfr : perDocResult.argResult().argumentScoringAlignment()
        .allEquivalenceClassess()) {
      eventTypesSeen.add(trfr.type());
    }
  }
  return eventTypesSeen;
}
 
private static void addNerTypeLabel(String token, Label label) {
	Multiset<Label> labelCount = classTypeCounts.get(token);
	
	if(labelCount == null){
		labelCount = HashMultiset.create();
	}
	
	labelCount.add(label);
	classTypeCounts.put(token, labelCount);
}
 
源代码11 项目: EasySRL   文件: TagDict.java
public static Map<String, Collection<Category>> makeDictFromParses(final Iterator<SyntaxTreeNode> input) {
	final Multiset<String> wordCounts = HashMultiset.create();
	final Map<String, Multiset<Category>> wordToCatToCount = new HashMap<>();

	int sentenceCount = 0;
	// First, count how many times each word occurs with each category
	while (input.hasNext()) {
		final SyntaxTreeNode sentence = input.next();
		final List<SyntaxTreeNodeLeaf> leaves = sentence.getLeaves();
		for (int i = 0; i < leaves.size(); i++) {
			final String word = leaves.get(i).getWord();
			final Category cat = leaves.get(i).getCategory();
			wordCounts.add(word);

			if (!wordToCatToCount.containsKey(word)) {
				final Multiset<Category> tmp = HashMultiset.create();
				wordToCatToCount.put(word, tmp);
			}

			wordToCatToCount.get(word).add(cat);
		}

		sentenceCount++;
		if (sentenceCount % 100 == 0) {
			System.out.println(sentenceCount);
		}
	}

	return makeDict(wordCounts, wordToCatToCount);
}
 
源代码12 项目: batfish   文件: NodePropertiesAnswererTest.java
@Test
public void getProperties() {
  String property1 = NodePropertySpecifier.CONFIGURATION_FORMAT;
  String property2 = NodePropertySpecifier.NTP_SERVERS;
  NodePropertySpecifier propertySpec =
      new NodePropertySpecifier(ImmutableSet.of(property1, property2));

  Configuration conf1 = new Configuration("node1", ConfigurationFormat.CISCO_IOS);
  Configuration conf2 = new Configuration("node2", ConfigurationFormat.HOST);
  conf2.setNtpServers(ImmutableSortedSet.of("sa"));
  Map<String, Configuration> configurations = ImmutableMap.of("node1", conf1, "node2", conf2);
  Map<String, ColumnMetadata> columns =
      NodePropertiesAnswerer.createTableMetadata(new NodePropertiesQuestion(null, propertySpec))
          .toColumnMap();

  NodeSpecifier nodeSpecifier = new NameRegexNodeSpecifier(Pattern.compile("node1|node2"));
  MockSpecifierContext ctxt = MockSpecifierContext.builder().setConfigs(configurations).build();

  Multiset<Row> propertyRows =
      NodePropertiesAnswerer.getProperties(propertySpec, ctxt, nodeSpecifier, columns);

  // we should have exactly these two rows
  Multiset<Row> expected =
      HashMultiset.create(
          ImmutableList.of(
              Row.builder()
                  .put(NodePropertiesAnswerer.COL_NODE, new Node("node1"))
                  .put(property1, ConfigurationFormat.CISCO_IOS)
                  .put(property2, ImmutableList.of())
                  .build(),
              Row.builder()
                  .put(NodePropertiesAnswerer.COL_NODE, new Node("node2"))
                  .put(property1, ConfigurationFormat.HOST)
                  .put(property2, ImmutableList.of("sa"))
                  .build()));
  assertThat(propertyRows, equalTo(expected));
}
 
源代码13 项目: angelix   文件: TestTreeBoundedSynthesis.java
@Test
public void testForbiddenNonexistent() {
    Multiset<Node> components = HashMultiset.create();
    components.add(x);
    components.add(y);
    components.add(Library.ADD);

    ArrayList<TestCase> testSuite = new ArrayList<>();
    Map<ProgramVariable, Node> assignment1 = new HashMap<>();
    assignment1.put(x, IntConst.of(1));
    assignment1.put(y, IntConst.of(1));
    testSuite.add(TestCase.ofAssignment(assignment1, IntConst.of(2)));

    Map<ProgramVariable, Node> assignment2 = new HashMap<>();
    assignment2.put(x, IntConst.of(1));
    assignment2.put(y, IntConst.of(2));
    testSuite.add(TestCase.ofAssignment(assignment2, IntConst.of(3)));

    List<Expression> forbidden = new ArrayList<>();
    Map<Hole, Expression> args = new HashMap<>();
    args.put((Hole) Library.ADD.getLeft(), Expression.leaf(x));
    args.put((Hole) Library.ADD.getRight(), Expression.leaf(y));
    forbidden.add(Expression.app(Library.SUB, args));

    Synthesis synthesizerWithForbidden =
            new Synthesis(new BoundedShape(2, forbidden), new TreeBoundedEncoder(false));
    Optional<Pair<Expression, Map<Parameter, Constant>>> result = synthesizerWithForbidden.synthesize(testSuite, components);
    assertTrue(result.isPresent());
    Node node = result.get().getLeft().getSemantics(result.get().getRight());
    assertTrue(node.equals(new Add(x, y)) || node.equals(new Add(y, x)));
}
 
源代码14 项目: gef   文件: ObservableMultisetTests.java
@Test
public void remove() {
	// initialize multiset with some values
	observable.add(1, 1);
	observable.add(2, 2);
	observable.add(3, 3);

	// prepare backup multiset
	Multiset<Integer> backupMultiset = HashMultiset.create();
	backupMultiset.add(1, 1);
	backupMultiset.add(2, 2);
	backupMultiset.add(3, 3);
	check(observable, backupMultiset);

	// register listeners
	registerListeners();

	// remove (first occurrence of) value
	invalidationListener.expect(1);
	multisetChangeListener.addAtomicExpectation();
	multisetChangeListener.addElementaryExpection(2, 1, 0);
	assertEquals(backupMultiset.remove(2), observable.remove(2));
	check(observable, backupMultiset);
	checkListeners();

	// remove (second occurrence of) value
	invalidationListener.expect(1);
	multisetChangeListener.addAtomicExpectation();
	multisetChangeListener.addElementaryExpection(2, 1, 0);
	assertEquals(backupMultiset.remove(2), observable.remove(2));
	check(observable, backupMultiset);
	checkListeners();

	// remove not contained value (no change expected)
	assertEquals(backupMultiset.remove(2), observable.remove(2));
	check(observable, backupMultiset);
	checkListeners();
}
 
源代码15 项目: ArchUnit   文件: ArchConditionTest.java
HandledViolation(Collection<Integer> objects, String message) {
    this.objects = HashMultiset.create(objects);
    this.message = message;
}
 
源代码16 项目: neo4j-sparql-extension   文件: TestResultHandler.java
public TestResultHandler() {
	this.solutions = HashMultiset.create();
}
 
源代码17 项目: presto   文件: BucketBalancer.java
private static Multimap<String, BucketAssignment> computeAssignmentChanges(ClusterState clusterState)
{
    Multimap<String, BucketAssignment> sourceToAllocationChanges = HashMultimap.create();

    Map<String, Long> allocationBytes = new HashMap<>(clusterState.getAssignedBytes());
    Set<String> activeNodes = clusterState.getActiveNodes();

    for (Distribution distribution : clusterState.getDistributionAssignments().keySet()) {
        // number of buckets in this distribution assigned to a node
        Multiset<String> allocationCounts = HashMultiset.create();
        Collection<BucketAssignment> distributionAssignments = clusterState.getDistributionAssignments().get(distribution);
        distributionAssignments.stream()
                .map(BucketAssignment::getNodeIdentifier)
                .forEach(allocationCounts::add);

        int currentMin = allocationBytes.keySet().stream()
                .mapToInt(allocationCounts::count)
                .min()
                .getAsInt();
        int currentMax = allocationBytes.keySet().stream()
                .mapToInt(allocationCounts::count)
                .max()
                .getAsInt();

        int numBuckets = distributionAssignments.size();
        int targetMin = (int) Math.floor((numBuckets * 1.0) / clusterState.getActiveNodes().size());
        int targetMax = (int) Math.ceil((numBuckets * 1.0) / clusterState.getActiveNodes().size());

        log.info("Distribution %s: Current bucket skew: min %s, max %s. Target bucket skew: min %s, max %s", distribution.getId(), currentMin, currentMax, targetMin, targetMax);

        for (String source : ImmutableSet.copyOf(allocationCounts)) {
            List<BucketAssignment> existingAssignments = distributionAssignments.stream()
                    .filter(assignment -> assignment.getNodeIdentifier().equals(source))
                    .collect(toList());

            for (BucketAssignment existingAssignment : existingAssignments) {
                if (activeNodes.contains(source) && allocationCounts.count(source) <= targetMin) {
                    break;
                }

                // identify nodes with bucket counts lower than the computed target, and greedily select from this set based on projected disk utilization.
                // greediness means that this may produce decidedly non-optimal results if one looks at the global distribution of buckets->nodes.
                // also, this assumes that nodes in a cluster have identical storage capacity
                String target = activeNodes.stream()
                        .filter(candidate -> !candidate.equals(source) && allocationCounts.count(candidate) < targetMax)
                        .sorted(comparingInt(allocationCounts::count))
                        .min(Comparator.comparingDouble(allocationBytes::get))
                        .orElseThrow(() -> new VerifyException("unable to find target for rebalancing"));

                long bucketSize = clusterState.getDistributionBucketSize().get(distribution);

                // only move bucket if it reduces imbalance
                if (activeNodes.contains(source) && (allocationCounts.count(source) == targetMax && allocationCounts.count(target) == targetMin)) {
                    break;
                }

                allocationCounts.remove(source);
                allocationCounts.add(target);
                allocationBytes.compute(source, (k, v) -> v - bucketSize);
                allocationBytes.compute(target, (k, v) -> v + bucketSize);

                sourceToAllocationChanges.put(
                        existingAssignment.getNodeIdentifier(),
                        new BucketAssignment(existingAssignment.getDistributionId(), existingAssignment.getBucketNumber(), target));
            }
        }
    }

    return sourceToAllocationChanges;
}
 
源代码18 项目: tutorials   文件: GuavaMultiSetUnitTest.java
@Test
public void givenMultiSet_whenSettingNegativeCount_shouldThrowException() {
    Multiset<String> bookStore = HashMultiset.create();
    assertThatThrownBy(() -> bookStore.setCount("Potter", -1))
      .isInstanceOf(IllegalArgumentException.class);
}
 
源代码19 项目: tassal   文件: Topic.java
public Topic(final int topicID) {
	this.tokens = HashMultiset.create();
	this.topicID = topicID;
}
 
源代码20 项目: tutorials   文件: GuavaMultiSetUnitTest.java
@Test
public void givenMultiSet_whenSetCount_shouldReturnCorrectCount() {
    Multiset<String> bookStore = HashMultiset.create();
    bookStore.setCount("Potter", 50);
    assertThat(bookStore.count("Potter")).isEqualTo(50);
}