@Override public Map<Symbol, Symbol> visitProject(ProjectNode node, Set<Symbol> lookupSymbols) { // Map from output Symbols to source Symbols Map<Symbol, Symbol> directSymbolTranslationOutputMap = Maps.transformValues(Maps.filterValues(node.getAssignments().getMap(), SymbolReference.class::isInstance), Symbol::from); Map<Symbol, Symbol> outputToSourceMap = lookupSymbols.stream() .filter(directSymbolTranslationOutputMap.keySet()::contains) .collect(toImmutableMap(identity(), directSymbolTranslationOutputMap::get)); checkState(!outputToSourceMap.isEmpty(), "No lookup symbols were able to pass through the projection"); // Map from source Symbols to underlying index source Symbols Map<Symbol, Symbol> sourceToIndexMap = node.getSource().accept(this, ImmutableSet.copyOf(outputToSourceMap.values())); // Generate the Map the connects lookup symbols to underlying index source symbols Map<Symbol, Symbol> outputToIndexMap = Maps.transformValues(Maps.filterValues(outputToSourceMap, in(sourceToIndexMap.keySet())), Functions.forMap(sourceToIndexMap)); return ImmutableMap.copyOf(outputToIndexMap); }
public void testForMapWildCardWithDefault() { Map<String, Integer> map = Maps.newHashMap(); map.put("One", 1); map.put("Three", 3); Number number = Double.valueOf(42); Function<String, Number> function = Functions.forMap(map, number); assertEquals(1, function.apply("One").intValue()); assertEquals(number, function.apply("Two")); assertEquals(3L, function.apply("Three").longValue()); }
public void testUniqueIndexIterable() { ImmutableMap<Integer, String> outputMap = Maps.uniqueIndex( new Iterable<String>() { @Override public Iterator<String> iterator() { return INT_TO_STRING_MAP.values().iterator(); } }, Functions.forMap(INT_TO_STRING_MAP.inverse())); assertEquals(INT_TO_STRING_MAP, outputMap); }
public void testCompositionWildcard() { Map<String, Integer> mapJapaneseToInteger = Maps.newHashMap(); Function<String, Integer> japaneseToInteger = Functions.forMap(mapJapaneseToInteger); Function<Object, String> numberToSpanish = Functions.constant("Yo no se"); Function<String, String> japaneseToSpanish = Functions.compose(numberToSpanish, japaneseToInteger); }
@GwtIncompatible // SerializableTester public void testForMapWithoutDefaultSerializable() { checkCanReserialize(Functions.forMap(ImmutableMap.of(1, 2))); }
@GwtIncompatible // SerializableTester public void testForMapWithDefaultSerializable() { checkCanReserialize(Functions.forMap(ImmutableMap.of(1, 2), 3)); }
public void testForMapWithDefault() { Map<String, Integer> map = Maps.newHashMap(); map.put("One", 1); map.put("Three", 3); map.put("Null", null); Function<String, Integer> function = Functions.forMap(map, 42); assertEquals(1, function.apply("One").intValue()); assertEquals(42, function.apply("Two").intValue()); assertEquals(3, function.apply("Three").intValue()); assertNull(function.apply("Null")); new EqualsTester() .addEqualityGroup(function, Functions.forMap(map, 42)) .addEqualityGroup(Functions.forMap(map)) .addEqualityGroup(Functions.forMap(map, null)) .addEqualityGroup(Functions.forMap(map, 43)) .testEquals(); }
@GwtIncompatible // SerializableTester public void testForMapWithDefault_includeSerializable() { Map<String, Integer> map = Maps.newHashMap(); map.put("One", 1); map.put("Three", 3); Function<String, Integer> function = Functions.forMap(map, 42); assertEquals(1, function.apply("One").intValue()); assertEquals(42, function.apply("Two").intValue()); assertEquals(3, function.apply("Three").intValue()); new EqualsTester() .addEqualityGroup( function, Functions.forMap(map, 42), SerializableTester.reserialize(function)) .addEqualityGroup(Functions.forMap(map)) .addEqualityGroup(Functions.forMap(map, null)) .addEqualityGroup(Functions.forMap(map, 43)) .testEquals(); }
public void testForMapWithDefault_null() { ImmutableMap<String, Integer> map = ImmutableMap.of("One", 1); Function<String, Integer> function = Functions.forMap(map, null); assertEquals((Integer) 1, function.apply("One")); assertNull(function.apply("Two")); // check basic sanity of equals and hashCode new EqualsTester() .addEqualityGroup(function) .addEqualityGroup(Functions.forMap(map, 1)) .testEquals(); }
public void testForMapWithoutDefault() { Map<String, Integer> map = Maps.newHashMap(); map.put("One", 1); map.put("Three", 3); map.put("Null", null); Function<String, Integer> function = Functions.forMap(map); assertEquals(1, function.apply("One").intValue()); assertEquals(3, function.apply("Three").intValue()); assertNull(function.apply("Null")); try { function.apply("Two"); fail(); } catch (IllegalArgumentException expected) { } new EqualsTester() .addEqualityGroup(function, Functions.forMap(map)) .addEqualityGroup(Functions.forMap(map, 42)) .testEquals(); }
public void testUniqueIndexCollection() { ImmutableMap<Integer, String> outputMap = Maps.uniqueIndex(INT_TO_STRING_MAP.values(), Functions.forMap(INT_TO_STRING_MAP.inverse())); assertEquals(INT_TO_STRING_MAP, outputMap); }
public void testComposeOfPredicateAndFunctionIsAssociative() { Map<Float, String> m = ImmutableMap.of(4.0f, "A", 3.0f, "B", 2.0f, "C", 1.0f, "D"); Predicate<? super Integer> h = Predicates.equalTo(42); Function<? super String, Integer> g = new HashCodeFunction(); Function<Float, String> f = Functions.forMap(m, "F"); Predicate<Float> p1 = Predicates.compose(Predicates.compose(h, g), f); Predicate<Float> p2 = Predicates.compose(h, Functions.compose(g, f)); // Might be nice (eventually) to have: // assertEquals(p1, p2); // But for now, settle for this: assertEquals(p1.hashCode(), p2.hashCode()); assertEquals(p1.apply(1.0f), p2.apply(1.0f)); assertEquals(p1.apply(5.0f), p2.apply(5.0f)); }
@GwtIncompatible // SerializableTester public void testForMapWithDefault_null_compareWithSerializable() { ImmutableMap<String, Integer> map = ImmutableMap.of("One", 1); Function<String, Integer> function = Functions.forMap(map, null); assertEquals((Integer) 1, function.apply("One")); assertNull(function.apply("Two")); // check basic sanity of equals and hashCode new EqualsTester() .addEqualityGroup(function, SerializableTester.reserialize(function)) .addEqualityGroup(Functions.forMap(map, 1)) .testEquals(); }
public boolean reverify(Unifier unifier) { return MoreObjects.firstNonNull( new PlaceholderVerificationVisitor( Collections2.transform( placeholder().requiredParameters(), Functions.forMap(arguments())), arguments().values()) .scan(unifier.getBinding(placeholder().exprKey()), unifier), true); }
public boolean reverify(Unifier unifier) { return MoreObjects.firstNonNull( new PlaceholderVerificationVisitor( Collections2.transform( placeholder().requiredParameters(), Functions.forMap(arguments())), arguments().values()) .scan(unifier.getBinding(placeholder().blockKey()), unifier), true); }
public void testComposeOfFunctionsIsAssociative() { Map<Float, String> m = ImmutableMap.of(4.0f, "A", 3.0f, "B", 2.0f, "C", 1.0f, "D"); Function<? super Integer, Boolean> h = Functions.constant(Boolean.TRUE); Function<? super String, Integer> g = new HashCodeFunction(); Function<Float, String> f = Functions.forMap(m, "F"); Function<Float, Boolean> c1 = Functions.compose(Functions.compose(h, g), f); Function<Float, Boolean> c2 = Functions.compose(h, Functions.compose(g, f)); // Might be nice (eventually) to have: // assertEquals(c1, c2); // But for now, settle for this: assertEquals(c1.hashCode(), c2.hashCode()); assertEquals(c1.apply(1.0f), c2.apply(1.0f)); assertEquals(c1.apply(5.0f), c2.apply(5.0f)); }
public void testUniqueIndexIterator() { ImmutableMap<Integer, String> outputMap = Maps.uniqueIndex( INT_TO_STRING_MAP.values().iterator(), Functions.forMap(INT_TO_STRING_MAP.inverse())); assertEquals(INT_TO_STRING_MAP, outputMap); }
@GwtIncompatible // SerializableTester public void testComposition_includeReserializabled() { Map<String, Integer> mJapaneseToInteger = Maps.newHashMap(); mJapaneseToInteger.put("Ichi", 1); mJapaneseToInteger.put("Ni", 2); mJapaneseToInteger.put("San", 3); Function<String, Integer> japaneseToInteger = Functions.forMap(mJapaneseToInteger); Map<Integer, String> mIntegerToSpanish = Maps.newHashMap(); mIntegerToSpanish.put(1, "Uno"); mIntegerToSpanish.put(3, "Tres"); mIntegerToSpanish.put(4, "Cuatro"); Function<Integer, String> integerToSpanish = Functions.forMap(mIntegerToSpanish); Function<String, String> japaneseToSpanish = Functions.compose(integerToSpanish, japaneseToInteger); new EqualsTester() .addEqualityGroup( japaneseToSpanish, Functions.compose(integerToSpanish, japaneseToInteger), SerializableTester.reserialize(japaneseToSpanish)) .addEqualityGroup(japaneseToInteger) .addEqualityGroup(integerToSpanish) .addEqualityGroup(Functions.compose(japaneseToInteger, integerToSpanish)) .testEquals(); }
public void testBulkGetReturnsSorted() { for (Striped<?> striped : allImplementations()) { Map<Object, Integer> indexByLock = Maps.newHashMap(); for (int i = 0; i < striped.size(); i++) { indexByLock.put(striped.getAt(i), i); } // ensure that bulkGet returns locks in monotonically increasing order for (int objectsNum = 1; objectsNum <= striped.size() * 2; objectsNum++) { Set<Object> objects = Sets.newHashSetWithExpectedSize(objectsNum); for (int i = 0; i < objectsNum; i++) { objects.add(new Object()); } Iterable<?> locks = striped.bulkGet(objects); assertTrue(Ordering.natural().onResultOf(Functions.forMap(indexByLock)).isOrdered(locks)); // check idempotency Iterable<?> locks2 = striped.bulkGet(objects); assertEquals(Lists.newArrayList(locks), Lists.newArrayList(locks2)); } } }
mJapaneseToInteger.put("Ni", 2); mJapaneseToInteger.put("San", 3); Function<String, Integer> japaneseToInteger = Functions.forMap(mJapaneseToInteger); mIntegerToSpanish.put(3, "Tres"); mIntegerToSpanish.put(4, "Cuatro"); Function<Integer, String> integerToSpanish = Functions.forMap(mIntegerToSpanish);