Java Code Examples for com.google.common.collect.ImmutableMultiset#copyOf()

The following examples show how to use com.google.common.collect.ImmutableMultiset#copyOf() . 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: QueryAssertions.java    From presto with Apache License 2.0 6 votes vote down vote up
public static void assertEqualsIgnoreOrder(Iterable<?> actual, Iterable<?> expected, String message)
{
    assertNotNull(actual, "actual is null");
    assertNotNull(expected, "expected is null");

    ImmutableMultiset<?> actualSet = ImmutableMultiset.copyOf(actual);
    ImmutableMultiset<?> expectedSet = ImmutableMultiset.copyOf(expected);
    if (!actualSet.equals(expectedSet)) {
        Multiset<?> unexpectedRows = Multisets.difference(actualSet, expectedSet);
        Multiset<?> missingRows = Multisets.difference(expectedSet, actualSet);
        int limit = 100;
        fail(format(
                "%snot equal\n" +
                        "Actual rows (up to %s of %s extra rows shown, %s rows in total):\n    %s\n" +
                        "Expected rows (up to %s of %s missing rows shown, %s rows in total):\n    %s\n",
                message == null ? "" : (message + "\n"),
                limit,
                unexpectedRows.size(),
                actualSet.size(),
                Joiner.on("\n    ").join(Iterables.limit(unexpectedRows, limit)),
                limit,
                missingRows.size(),
                expectedSet.size(),
                Joiner.on("\n    ").join(Iterables.limit(missingRows, limit))));
    }
}
 
Example 2
Source File: MultisetGwtTest.java    From gwt-jackson with Apache License 2.0 6 votes vote down vote up
public void testSerialization() {
    BeanWithMultisetTypes bean = new BeanWithMultisetTypes();

    List<String> list = Arrays.asList( "foo", "abc", null, "abc" );
    List<String> listWithNonNull = Arrays.asList( "foo", "abc", "bar", "abc" );

    bean.multiset = LinkedHashMultiset.create( list );
    bean.hashMultiset = HashMultiset.create( Arrays.asList( "abc", "abc" ) );
    bean.linkedHashMultiset = LinkedHashMultiset.create( list );
    bean.sortedMultiset = TreeMultiset.create( listWithNonNull );
    bean.treeMultiset = TreeMultiset.create( listWithNonNull );
    bean.immutableMultiset = ImmutableMultiset.copyOf( listWithNonNull );
    bean.enumMultiset = EnumMultiset.create( Arrays.asList( AlphaEnum.B, AlphaEnum.A, AlphaEnum.D, AlphaEnum.A ) );

    String expected = "{" +
            "\"multiset\":[\"foo\",\"abc\",\"abc\",null]," +
            "\"hashMultiset\":[\"abc\",\"abc\"]," +
            "\"linkedHashMultiset\":[\"foo\",\"abc\",\"abc\",null]," +
            "\"sortedMultiset\":[\"abc\",\"abc\",\"bar\",\"foo\"]," +
            "\"treeMultiset\":[\"abc\",\"abc\",\"bar\",\"foo\"]," +
            "\"immutableMultiset\":[\"foo\",\"abc\",\"abc\",\"bar\"]," +
            "\"enumMultiset\":[\"A\",\"A\",\"B\",\"D\"]" +
            "}";

    assertEquals( expected, BeanWithMultisetTypesMapper.INSTANCE.write( bean ) );
}
 
Example 3
Source File: ClassMapper.java    From OSRS-Deobfuscator with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private Multiset<Type> fieldCardinalities(ClassFile cf)
{
	List<Type> t = cf.getFields().stream()
		.filter(f -> !f.isStatic())
		.map(f -> f.getType())
		.collect(Collectors.toList());

	return ImmutableMultiset.copyOf(t);
}
 
Example 4
Source File: ClassMapper.java    From OSRS-Deobfuscator with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private Multiset<Signature> methodCardinalities(ClassFile cf)
{
	List<Signature> t = cf.getMethods().stream()
		.filter(m -> !m.isStatic())
		.filter(m -> !m.getName().startsWith("<"))
		.map(m -> m.getDescriptor())
		.collect(Collectors.toList());

	return ImmutableMultiset.copyOf(t);
}
 
Example 5
Source File: IterableUtils.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static <T> ImmutableCollection<T> copyOf(Iterable<T> iterable) {
    if(iterable instanceof Set) {
        return ImmutableSet.copyOf(iterable);
    } else if(iterable instanceof Multiset) {
        return ImmutableMultiset.copyOf(iterable);
    } else {
        return ImmutableList.copyOf(iterable);
    }
}
 
Example 6
Source File: ObjectGraphMeasurer.java    From memory-measurer with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a Footprint, by specifying the number of objects,
 * references, and primitives (represented as a {@link Multiset}).
 *
 * @param objects the number of objects
 * @param references the number of references
 * @param primitives the number of primitives (represented by the
 * respective primitive classes, e.g. {@code int.class} etc)
 */
public Footprint(int objects, int references, Multiset<Class<?>> primitives) {
  Preconditions.checkArgument(objects >= 0, "Negative number of objects");
  Preconditions.checkArgument(references >= 0, "Negative number of references");
  Preconditions.checkArgument(primitiveTypes.containsAll(primitives.elementSet()),
  "Unexpected primitive type");
  this.objects = objects;
  this.references = references;
  this.primitives = ImmutableMultiset.copyOf(primitives);
}
 
Example 7
Source File: ObjectGraphMeasurer.java    From memory-measurer with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a Footprint, by specifying the number of objects,
 * references, and primitives (represented as a {@link Multiset}).
 *
 * @param objects the number of objects
 * @param references the number of references
 * @param primitives the number of primitives (represented by the
 * respective primitive classes, e.g. {@code int.class} etc)
 */
public Footprint(int objects, int references, Multiset<Class<?>> primitives) {
  Preconditions.checkArgument(objects >= 0, "Negative number of objects");
  Preconditions.checkArgument(references >= 0, "Negative number of references");
  Preconditions.checkArgument(primitiveTypes.containsAll(primitives.elementSet()),
  "Unexpected primitive type");
  this.objects = objects;
  this.references = references;
  this.primitives = ImmutableMultiset.copyOf(primitives);
}
 
Example 8
Source File: CalciteAssert.java    From Quicksql with MIT License 4 votes vote down vote up
static ImmutableMultiset<String> toSet(ResultSet resultSet)
    throws SQLException {
  return ImmutableMultiset.copyOf(toList(resultSet));
}
 
Example 9
Source File: DeleteOldCommitLogsAction.java    From nomulus with Apache License 2.0 4 votes vote down vote up
@Override
public void reduce(
    final Key<CommitLogManifest> manifestKey,
    ReducerInput<Boolean> canDeleteVerdicts) {
  ImmutableMultiset<Boolean> canDeleteMultiset = ImmutableMultiset.copyOf(canDeleteVerdicts);
  if (canDeleteMultiset.count(TRUE) > 1) {
    getContext().incrementCounter("commit log manifests incorrectly mapped multiple times");
  }
  if (canDeleteMultiset.count(FALSE) > 1) {
    getContext().incrementCounter("commit log manifests referenced multiple times");
  }
  if (canDeleteMultiset.contains(FALSE)) {
    getContext().incrementCounter(
        canDeleteMultiset.contains(TRUE)
        ? "old commit log manifests still referenced"
        : "new (or nonexistent) commit log manifests referenced");
    getContext().incrementCounter(
        "EPP resource revisions handled",
        canDeleteMultiset.count(FALSE));
    return;
  }

  DeletionResult deletionResult = tm().transactNew(() -> {
    CommitLogManifest manifest = ofy().load().key(manifestKey).now();
    // It is possible that the same manifestKey was run twice, if a shard had to be restarted
    // or some weird failure. If this happens, we want to exit immediately.
    // Note that this can never happen in dryRun.
    if (manifest == null) {
      return DeletionResult.create(DeletionResult.Status.ALREADY_DELETED, 0);
    }
    // Doing a sanity check on the date. This is the only place we use the CommitLogManifest,
    // so maybe removing this test will improve performance. However, unless it's proven that
    // the performance boost is significant (and we've tested this enough to be sure it never
    // happens)- the safty of "let's not delete stuff we need from prod" is more important.
    if (manifest.getCommitTime().isAfter(deletionThreshold)) {
      return DeletionResult.create(DeletionResult.Status.AFTER_THRESHOLD, 0);
    }
    Iterable<Key<CommitLogMutation>> commitLogMutationKeys = ofy().load()
        .type(CommitLogMutation.class)
        .ancestor(manifestKey)
        .keys()
        .iterable();
    ImmutableList<Key<?>> keysToDelete = ImmutableList.<Key<?>>builder()
        .addAll(commitLogMutationKeys)
        .add(manifestKey)
        .build();
    // Normally in a dry run we would log the entities that would be deleted, but those can
    // number in the millions so we skip the logging.
    if (!isDryRun) {
      ofy().deleteWithoutBackup().keys(keysToDelete);
    }
    return DeletionResult.create(DeletionResult.Status.SUCCESS, keysToDelete.size());
  });

  switch (deletionResult.status()) {
    case SUCCESS:
      getContext().incrementCounter("old commit log manifests deleted");
      getContext().incrementCounter("total entities deleted", deletionResult.numDeleted());
      break;
    case ALREADY_DELETED:
      getContext().incrementCounter("attempts to delete an already deleted manifest");
      break;
    case AFTER_THRESHOLD:
      logger.atSevere().log(
          "Won't delete CommitLogManifest %s that is too recent.", manifestKey);
      getContext().incrementCounter("manifests incorrectly assigned for deletion (SEE LOGS)");
      break;
  }
}
 
Example 10
Source File: ObjectGraphMeasurer.java    From memory-measurer with Apache License 2.0 4 votes vote down vote up
public Footprint result() {
  return new Footprint(objects, references, ImmutableMultiset.copyOf(primitives));
}
 
Example 11
Source File: CollectionTests.java    From txtUML with Eclipse Public License 1.0 4 votes vote down vote up
@SafeVarargs
private static <T> void assertThatContains(GeneralCollection<T> actual, T... expecteds) {
	Multiset<T> actualSet = ImmutableMultiset.copyOf(actual);
	Multiset<T> expectedSet = ImmutableMultiset.copyOf(expecteds);
	assertEquals(expectedSet, actualSet);
}
 
Example 12
Source File: CalciteAssert.java    From calcite with Apache License 2.0 4 votes vote down vote up
static ImmutableMultiset<String> toSet(ResultSet resultSet)
    throws SQLException {
  return ImmutableMultiset.copyOf(toList(resultSet));
}
 
Example 13
Source File: ImmutableSortedKeyListMultimap.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Override
public Multiset<K> keys() {
  return ImmutableMultiset.copyOf(sortedKeys);
}
 
Example 14
Source File: ObjectGraphMeasurer.java    From memory-measurer with Apache License 2.0 4 votes vote down vote up
public Footprint result() {
  return new Footprint(objects, references, ImmutableMultiset.copyOf(primitives));
}
 
Example 15
Source File: Rows.java    From batfish with Apache License 2.0 2 votes vote down vote up
/**
 * Returns an immutable copy of the rows in this object
 *
 * @return An ImmutableMultiset
 */
@JsonValue
public Multiset<Row> getData() {
  return ImmutableMultiset.copyOf(_data);
}