Refine search
public void testComparators() { TreeMultimap<String, Integer> multimap = TreeMultimap.create(); assertEquals(Ordering.natural(), multimap.keyComparator()); assertEquals(Ordering.natural(), multimap.valueComparator()); }
private TreeMultimap<String, Integer> createMultimap() { TreeMultimap<String, Integer> multimap = TreeMultimap.create( Ordering.<String>natural().nullsFirst(), Ordering.<Integer>natural().nullsFirst()); multimap.put("a", -1); multimap.put("a", -3); multimap.put("z", -2); return multimap; }
/** * @serialData key comparator, value comparator, number of distinct keys, and then for each * distinct key: the key, number of values for that key, and key values */ @GwtIncompatible // java.io.ObjectOutputStream private void writeObject(ObjectOutputStream stream) throws IOException { stream.defaultWriteObject(); stream.writeObject(keyComparator()); stream.writeObject(valueComparator()); Serialization.writeMultimap(this, stream); }
/** Create and populate a {@code TreeMultimap} with the natural ordering of keys and values. */ private TreeMultimap<String, Integer> createPopulate() { TreeMultimap<String, Integer> multimap = TreeMultimap.create(); multimap.put("google", 2); multimap.put("google", 6); multimap.put("foo", 3); multimap.put("foo", 1); multimap.put("foo", 7); multimap.put("tree", 4); multimap.put("tree", 0); return multimap; }
public static <T extends Comparable, E extends Comparable> Iterator<T> extractAndSort(Iterator<T> input, Function<T, E> extractor) { TreeMultimap<E, T> reorgnized = TreeMultimap.create(); while (input.hasNext()) { T t = input.next(); E e = extractor.apply(t); reorgnized.put(e, t); } return reorgnized.values().iterator(); } }
private UsersWsResponse buildResponse(List<UserDto> users, List<UserPermissionDto> userPermissions, Paging paging) { Multimap<Integer, String> permissionsByUserId = TreeMultimap.create(); userPermissions.forEach(userPermission -> permissionsByUserId.put(userPermission.getUserId(), userPermission.getPermission())); UsersWsResponse.Builder response = UsersWsResponse.newBuilder(); users.forEach(user -> { Permissions.User.Builder userResponse = response.addUsersBuilder() .setLogin(user.getLogin()) .addAllPermissions(permissionsByUserId.get(user.getId())); ofNullable(user.getEmail()).ifPresent(userResponse::setEmail); ofNullable(emptyToNull(user.getEmail())).ifPresent(u -> userResponse.setAvatar(avatarResolver.create(user))); ofNullable(user.getName()).ifPresent(userResponse::setName); }); response.getPagingBuilder() .setPageIndex(paging.pageIndex()) .setPageSize(paging.pageSize()) .setTotal(paging.total()) .build(); return response.build(); }
/** generate {@code BUCKFile} */ public static void generate( Project project, BuckFileManager buckFileManager, VisibilityExtension visibilityExtension) { List<Rule> rules = createRules(project); File moduleDir = project.getBuildFile().getParentFile(); File visibilityFile = new File(moduleDir, visibilityExtension.visibilityFileName); boolean hasVisibilityFile = visibilityFile.isFile(); Multimap<String, String> extraLoadStatements = TreeMultimap.create(); if (hasVisibilityFile) { rules.forEach(rule -> rule.fileConfiguredVisibility(true)); extraLoadStatements.put( ":" + visibilityExtension.visibilityFileName, visibilityExtension.visibilityFunction); } File buckFile = project.file(OkBuckGradlePlugin.BUCK); buckFileManager.writeToBuckFile(rules, buckFile, extraLoadStatements); }
int numTransferred = 0; final Multimap<String, SingularityTaskId> eligibleTaskIdByRequestId = TreeMultimap.create(Ordering.natural(), SingularityTaskId.STARTED_AT_COMPARATOR_DESC); eligibleTaskIdByRequestId.put(taskId.getRequestId(), taskId); for (Map.Entry<String, Collection<SingularityTaskId>> entry : eligibleTaskIdByRequestId.asMap().entrySet()) { int i = 0; for (SingularityTaskId taskId : entry.getValue()) {
public void testMultimapComparators() { Multimap<String, Integer> multimap = create(); multimap.put("foo", 3); multimap.put("bar", 1); multimap.putAll("foo", Arrays.asList(-1, 2, 4)); multimap.putAll("bar", Arrays.asList(2, 3)); multimap.put("foo", 1); TreeMultimap<String, Integer> copy = TreeMultimap.create(StringLength.COMPARATOR, DECREASING_INT_COMPARATOR); copy.putAll(multimap); assertEquals(multimap, copy); assertEquals(StringLength.COMPARATOR, copy.keyComparator()); assertEquals(DECREASING_INT_COMPARATOR, copy.valueComparator()); }
/** * Return list of propertyDto by component uuid, sorted from project to lowest module */ private Multimap<String, PropertyDto> loadComponentSettings(DbSession dbSession, Optional<String> key, ComponentDto component) { List<String> componentUuids = DOT_SPLITTER.splitToList(component.moduleUuidPath()); List<ComponentDto> componentDtos = dbClient.componentDao().selectByUuids(dbSession, componentUuids); Set<Long> componentIds = componentDtos.stream().map(ComponentDto::getId).collect(Collectors.toSet()); Map<Long, String> uuidsById = componentDtos.stream().collect(Collectors.toMap(ComponentDto::getId, ComponentDto::uuid)); List<PropertyDto> properties = key.isPresent() ? dbClient.propertiesDao().selectPropertiesByKeysAndComponentIds(dbSession, Collections.singleton(key.get()), componentIds) : dbClient.propertiesDao().selectPropertiesByComponentIds(dbSession, componentIds); Multimap<String, PropertyDto> propertyDtosByUuid = TreeMultimap.create(Ordering.explicit(componentUuids), Ordering.arbitrary()); for (PropertyDto propertyDto : properties) { Long componentId = propertyDto.getResourceId(); String componentUuid = uuidsById.get(componentId); propertyDtosByUuid.put(componentUuid, propertyDto); } return propertyDtosByUuid; }
@GwtIncompatible // SerializableTester public void testTreeMultimapDerived() { TreeMultimap<DerivedComparable, DerivedComparable> multimap = TreeMultimap.create(); assertEquals(ImmutableMultimap.of(), multimap); multimap.put(new DerivedComparable("foo"), new DerivedComparable("f")); multimap.put(new DerivedComparable("foo"), new DerivedComparable("o")); multimap.put(new DerivedComparable("foo"), new DerivedComparable("o")); multimap.put(new DerivedComparable("bar"), new DerivedComparable("b")); multimap.put(new DerivedComparable("bar"), new DerivedComparable("a")); multimap.put(new DerivedComparable("bar"), new DerivedComparable("r")); assertThat(multimap.keySet()) .containsExactly(new DerivedComparable("bar"), new DerivedComparable("foo")) .inOrder(); assertThat(multimap.values()) .containsExactly( new DerivedComparable("a"), new DerivedComparable("b"), new DerivedComparable("r"), new DerivedComparable("f"), new DerivedComparable("o")) .inOrder(); assertEquals(Ordering.natural(), multimap.keyComparator()); assertEquals(Ordering.natural(), multimap.valueComparator()); SerializableTester.reserializeAndAssert(multimap); }
@Override protected Set<String> create(String[] elements) { TreeMultimap<Integer, String> multimap = TreeMultimap.create(Ordering.natural(), Ordering.natural().nullsFirst()); multimap.putAll(1, Arrays.asList(elements)); return (Set<String>) multimap.asMap().entrySet().iterator().next().getValue(); }
@GwtIncompatible // slow (~10s) public void testUnmodifiableSynchronizedTreeMultimap() { TreeMultimap<String, Integer> delegate = TreeMultimap.create(Ordering.<String>natural(), INT_COMPARATOR); SortedSetMultimap<String, Integer> multimap = Multimaps.synchronizedSortedSetMultimap(delegate); checkUnmodifiableMultimap(multimap, false, "null", 42); assertSame(INT_COMPARATOR, multimap.valueComparator()); }
/** Test that a TreeMultimap created from another uses the natural ordering. */ public void testMultimapCreateFromTreeMultimap() { TreeMultimap<String, Integer> tree = TreeMultimap.create(StringLength.COMPARATOR, DECREASING_INT_COMPARATOR); tree.put("google", 2); tree.put("google", 6); tree.put("tree", 0); tree.put("tree", 3); assertThat(tree.keySet()).containsExactly("tree", "google").inOrder(); assertThat(tree.get("google")).containsExactly(6, 2).inOrder(); TreeMultimap<String, Integer> copy = TreeMultimap.create(tree); assertEquals(tree, copy); assertThat(copy.keySet()).containsExactly("google", "tree").inOrder(); assertThat(copy.get("google")).containsExactly(2, 6).inOrder(); assertEquals(Ordering.natural(), copy.keyComparator()); assertEquals(Ordering.natural(), copy.valueComparator()); assertEquals(Ordering.natural(), copy.get("google").comparator()); }
@Override protected SetMultimap<String, String> create(Entry<String, String>[] entries) { SetMultimap<String, String> multimap = TreeMultimap.create( Ordering.natural().nullsFirst(), Ordering.natural().nullsFirst()); for (Entry<String, String> entry : entries) { multimap.put(entry.getKey(), entry.getValue()); } return multimap; }
public void testToMultimap() { Collector<Entry<String, Integer>, ?, TreeMultimap<String, Integer>> collector = Multimaps.toMultimap(Entry::getKey, Entry::getValue, TreeMultimap::create); BiPredicate<Multimap<?, ?>, Multimap<?, ?>> equivalence = Equivalence.equals() .onResultOf((Multimap<?, ?> mm) -> ImmutableList.copyOf(mm.asMap().entrySet())) .and(Equivalence.equals()); TreeMultimap<String, Integer> empty = TreeMultimap.create(); TreeMultimap<String, Integer> filled = TreeMultimap.create(); filled.put("a", 1); filled.put("a", 2); filled.put("b", 2); filled.put("c", 3); CollectorTester.of(collector, equivalence) .expectCollects(empty) .expectCollects( filled, mapEntry("a", 1), mapEntry("a", 2), mapEntry("b", 2), mapEntry("c", 3)); }
private static SortedSetMultimap<Integer, byte[]> getRowsForBatches( Supplier<SqlConnection> connectionSupplier, String query, Object[] args) { SqlConnection connection = connectionSupplier.get(); try { AgnosticResultSet results = connection.selectResultSetUnregisteredQuery(query, args); SortedSetMultimap<Integer, byte[]> ret = TreeMultimap.create( Ordering.natural(), UnsignedBytes.lexicographicalComparator()); for (AgnosticResultRow row : results.rows()) { @SuppressWarnings("deprecation") byte[] rowName = row.getBytes("row_name"); int batchNum = row.getInteger("batch_num"); if (rowName != null) { ret.put(batchNum, rowName); } } return ret; } finally { closeSql(connection); } }