/** Returns a new {@code TypeResolver} with {@code variable} mapping to {@code type}. */ final TypeTable where(Map<TypeVariableKey, ? extends Type> mappings) { ImmutableMap.Builder<TypeVariableKey, Type> builder = ImmutableMap.builder(); builder.putAll(map); for (Entry<TypeVariableKey, ? extends Type> mapping : mappings.entrySet()) { TypeVariableKey variable = mapping.getKey(); Type type = mapping.getValue(); checkArgument(!variable.equalsType(type), "Type variable %s bound to itself", variable); builder.put(variable, type); } return new TypeTable(builder.build()); }
/** * Associates all of {@code map's} keys and values in the built map. Duplicate keys are not * allowed, and will cause {@link #build} to fail. * * @throws NullPointerException if any key or value in {@code map} is null * @throws ClassCastException if any value is not an instance of the type specified by its key */ @CanIgnoreReturnValue public <T extends B> Builder<B> putAll(Map<? extends Class<? extends T>, ? extends T> map) { for (Entry<? extends Class<? extends T>, ? extends T> entry : map.entrySet()) { Class<? extends T> type = entry.getKey(); T value = entry.getValue(); mapBuilder.put(type, cast(type, value)); } return this; }
@Test public void testExtractValuesFromSingleDomain() { Map<Type, Object> testValues = ImmutableMap.<Type, Object>builder() .put(BOOLEAN, true) .put(INTEGER, 1234L) .put(BIGINT, 4321L) .put(DOUBLE, 0.123) .put(VARCHAR, utf8Slice(TEST_STRING)) .build(); for (Map.Entry<Type, Object> testValue : testValues.entrySet()) { Domain predicateDomain = Domain.singleValue(testValue.getKey(), testValue.getValue()); Optional<Collection<Object>> discreteValues = extractDiscreteValues(predicateDomain.getValues()); assertTrue(discreteValues.isPresent()); Collection<Object> objects = discreteValues.get(); assertEquals(objects.size(), 1); assertEquals(objects.iterator().next(), testValue.getValue()); } }
@Test(timeOut = 60_000, dataProvider = "legacy_system_pool_enabled", expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = ".*Query exceeded distributed user memory limit of 1kB.*") public void testQueryUserMemoryLimit(boolean systemPoolEnabled) throws Exception { Map<String, String> properties = ImmutableMap.<String, String>builder() .put("task.max-partial-aggregation-memory", "1B") .put("query.max-memory", "1kB") .put("query.max-total-memory", "1GB") .put("deprecated.legacy-system-pool-enabled", String.valueOf(systemPoolEnabled)) .build(); try (QueryRunner queryRunner = createQueryRunner(SESSION, properties)) { queryRunner.execute(SESSION, "SELECT COUNT(*), repeat(orderstatus, 1000) FROM orders GROUP BY 2"); } }
@Test public void testExplicitPropertyMappings() { Map<String, String> properties = new ImmutableMap.Builder<String, String>() .put("session-property-manager.config-file", "/test.json") .build(); FileSessionPropertyManagerConfig expected = new FileSessionPropertyManagerConfig() .setConfigFile(new File("/test.json")); assertFullMapping(properties, expected); } }
public void testBuilder_orderEntriesByValueAfterExactSizeBuild() { Builder<String, Integer> builder = new Builder<String, Integer>(2).put("four", 4).put("one", 1); ImmutableMap<String, Integer> keyOrdered = builder.build(); ImmutableMap<String, Integer> valueOrdered = builder.orderEntriesByValue(Ordering.natural()).build(); assertMapEquals(keyOrdered, "four", 4, "one", 1); assertMapEquals(valueOrdered, "one", 1, "four", 4); }
/** * Adds the given {@code entry} to the map, making it immutable if necessary. Duplicate keys are * not allowed, and will cause {@link #build} to fail. * * @since 11.0 */ @CanIgnoreReturnValue public Builder<K, V> put(Entry<? extends K, ? extends V> entry) { return put(entry.getKey(), entry.getValue()); }
public void testBuilder_orderEntriesByValue() { ImmutableMap<String, Integer> map = new Builder<String, Integer>() .orderEntriesByValue(Ordering.natural()) .put("three", 3) .put("one", 1) .put("five", 5) .put("four", 4) .put("two", 2) .build(); assertMapEquals(map, "one", 1, "two", 2, "three", 3, "four", 4, "five", 5); }
public void testBuilderReuse() { Builder<String, Integer> builder = new Builder<>(); ImmutableMap<String, Integer> mapOne = builder.put("one", 1).put("two", 2).build(); ImmutableMap<String, Integer> mapTwo = builder.put("three", 3).put("four", 4).build(); assertMapEquals(mapOne, "one", 1, "two", 2); assertMapEquals(mapTwo, "one", 1, "two", 2, "three", 3, "four", 4); }
public void testBuilderPutImmutableEntryWithNullKeyFailsAtomically() { Builder<String, Integer> builder = new Builder<>(); try { builder.put(Maps.immutableEntry((String) null, 1)); fail(); } catch (NullPointerException expected) { } builder.put("foo", 2); assertMapEquals(builder.build(), "foo", 2); }
public void testBuilderPutMutableEntryWithNullKeyFailsAtomically() { Builder<String, Integer> builder = new Builder<>(); try { builder.put(new SimpleEntry<String, Integer>(null, 1)); fail(); } catch (NullPointerException expected) { } builder.put("foo", 2); assertMapEquals(builder.build(), "foo", 2); }
/** Creates an ImmutableSetMultimap from an asMap.entrySet. */ static <K, V> ImmutableSetMultimap<K, V> fromMapEntries( Collection<? extends Map.Entry<? extends K, ? extends Collection<? extends V>>> mapEntries, @Nullable Comparator<? super V> valueComparator) { if (mapEntries.isEmpty()) { return of(); } ImmutableMap.Builder<K, ImmutableSet<V>> builder = new ImmutableMap.Builder<>(mapEntries.size()); int size = 0; for (Entry<? extends K, ? extends Collection<? extends V>> entry : mapEntries) { K key = entry.getKey(); Collection<? extends V> values = entry.getValue(); ImmutableSet<V> set = valueSet(valueComparator, values); if (!set.isEmpty()) { builder.put(key, set); size += set.size(); } } return new ImmutableSetMultimap<>(builder.build(), size, valueComparator); }
/** Creates an ImmutableListMultimap from an asMap.entrySet. */ static <K, V> ImmutableListMultimap<K, V> fromMapEntries( Collection<? extends Map.Entry<? extends K, ? extends Collection<? extends V>>> mapEntries, @Nullable Comparator<? super V> valueComparator) { if (mapEntries.isEmpty()) { return of(); } ImmutableMap.Builder<K, ImmutableList<V>> builder = new ImmutableMap.Builder<>(mapEntries.size()); int size = 0; for (Entry<? extends K, ? extends Collection<? extends V>> entry : mapEntries) { K key = entry.getKey(); Collection<? extends V> values = entry.getValue(); ImmutableList<V> list = (valueComparator == null) ? ImmutableList.copyOf(values) : ImmutableList.sortedCopyOf(valueComparator, values); if (!list.isEmpty()) { builder.put(key, list); size += list.size(); } } return new ImmutableListMultimap<>(builder.build(), size); }