Java Code Examples for com.google.common.collect.HashMultiset#create()

The following examples show how to use com.google.common.collect.HashMultiset#create() . 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: HeaderSamplerTest.java    From log-synth with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 2
Source File: SwitchedVlanPropertiesAnswerer.java    From batfish with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: BaseDomainLabelList.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 4
Source File: DiscreteElementwiseConditionalDistribution.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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);
}
 
Example 5
Source File: TestHardAffinityFragmentParallelizer.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@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)));
}
 
Example 6
Source File: ObservableMultisetTests.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@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();
}
 
Example 7
Source File: ObservableMultisetTests.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@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();
}
 
Example 8
Source File: TestTreeBoundedSynthesis.java    From angelix with MIT License 5 votes vote down vote up
@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)));
}
 
Example 9
Source File: NodePropertiesAnswererTest.java    From batfish with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 10
Source File: ImageHelper.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: TestTreeBoundedSynthesis.java    From angelix with MIT License 5 votes vote down vote up
@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));
}
 
Example 12
Source File: ByEventTypeResultWriter.java    From tac-kbp-eal with MIT License 5 votes vote down vote up
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;
}
 
Example 13
Source File: YagoLabelsToClassProbabilities.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 14
Source File: TagDict.java    From EasySRL with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 15
Source File: Topic.java    From tassal with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Topic(final int topicID) {
	this.tokens = HashMultiset.create();
	this.topicID = topicID;
}
 
Example 16
Source File: GuavaMultiSetUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenMultiSet_whenSetCount_shouldReturnCorrectCount() {
    Multiset<String> bookStore = HashMultiset.create();
    bookStore.setCount("Potter", 50);
    assertThat(bookStore.count("Potter")).isEqualTo(50);
}
 
Example 17
Source File: GuavaMultiSetUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenMultiSet_whenSettingNegativeCount_shouldThrowException() {
    Multiset<String> bookStore = HashMultiset.create();
    assertThatThrownBy(() -> bookStore.setCount("Potter", -1))
      .isInstanceOf(IllegalArgumentException.class);
}
 
Example 18
Source File: BucketBalancer.java    From presto with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 19
Source File: TestResultHandler.java    From neo4j-sparql-extension with GNU General Public License v3.0 4 votes vote down vote up
public TestResultHandler() {
	this.solutions = HashMultiset.create();
}
 
Example 20
Source File: ArchConditionTest.java    From ArchUnit with Apache License 2.0 4 votes vote down vote up
HandledViolation(Collection<Integer> objects, String message) {
    this.objects = HashMultiset.create(objects);
    this.message = message;
}