@Override public NavigableMap<K, V> descendingMap() { return asMap(set.descendingSet(), function); } }
private static <N, E> Map<E, EndpointPair<N>> edgeIncidentNodesMap(final Network<N, E> network) { Function<E, EndpointPair<N>> edgeToIncidentNodesFn = new Function<E, EndpointPair<N>>() { @Override public EndpointPair<N> apply(E edge) { return network.incidentNodes(edge); } }; return Maps.asMap(network.edges(), edgeToIncidentNodesFn); } }
private static <N, V> Map<EndpointPair<N>, V> edgeValueMap(final ValueGraph<N, V> graph) { Function<EndpointPair<N>, V> edgeToValueFn = new Function<EndpointPair<N>, V>() { @Override public V apply(EndpointPair<N> edge) { return graph.edgeValueOrDefault(edge.nodeU(), edge.nodeV(), null); } }; return Maps.asMap(graph.edges(), edgeToValueFn); } }
@Override public NavigableMap<K, V> subMap( K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) { return asMap(set.subSet(fromKey, fromInclusive, toKey, toInclusive), function); }
@Override public NavigableMap<K, V> headMap(K toKey, boolean inclusive) { return asMap(set.headSet(toKey, inclusive), function); }
@Override public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) { return asMap(set.tailSet(fromKey, inclusive), function); }
@Override public SortedMap<K, V> headMap(K toKey) { return asMap(backingSet().headSet(toKey), function); }
@Override public SortedMap<K, V> tailMap(K fromKey) { return asMap(backingSet().tailSet(fromKey), function); }
@Override public SortedMap<K, V> subMap(K fromKey, K toKey) { return asMap(backingSet().subSet(fromKey, toKey), function); }
private static <N> GraphConnections<N, Presence> connectionsOf(Graph<N> graph, N node) { Function<Object, Presence> edgeValueFn = Functions.constant(Presence.EDGE_EXISTS); return graph.isDirected() ? DirectedGraphConnections.ofImmutable( graph.predecessors(node), Maps.asMap(graph.successors(node), edgeValueFn)) : UndirectedGraphConnections.ofImmutable( Maps.asMap(graph.adjacentNodes(node), edgeValueFn)); }
private static <N, V> GraphConnections<N, V> connectionsOf( final ValueGraph<N, V> graph, final N node) { Function<N, V> successorNodeToValueFn = new Function<N, V>() { @Override public V apply(N successorNode) { return graph.edgeValueOrDefault(node, successorNode, null); } }; return graph.isDirected() ? DirectedGraphConnections.ofImmutable( graph.predecessors(node), Maps.asMap(graph.successors(node), successorNodeToValueFn)) : UndirectedGraphConnections.ofImmutable( Maps.asMap(graph.adjacentNodes(node), successorNodeToValueFn)); } }
public void testAsMapReadsThrough() { Set<String> strings = Sets.newLinkedHashSet(); Collections.addAll(strings, "one", "two", "three"); Map<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map); assertNull(map.get("four")); strings.add("four"); assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5, "four", 4), map); assertEquals(Integer.valueOf(4), map.get("four")); }
public void testAsMapEmpty() { Set<String> strings = ImmutableSet.of(); Map<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertThat(map.entrySet()).isEmpty(); assertTrue(map.isEmpty()); assertNull(map.get("five")); }
public void testAsMapWritesThrough() { Set<String> strings = Sets.newLinkedHashSet(); Collections.addAll(strings, "one", "two", "three"); Map<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map); assertEquals(Integer.valueOf(3), map.remove("two")); assertThat(strings).containsExactly("one", "three").inOrder(); }
public void testAsMapSortedEmpty() { SortedSet<String> strings = new NonNavigableSortedSet(); SortedMap<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertThat(map.entrySet()).isEmpty(); assertTrue(map.isEmpty()); assertNull(map.get("five")); }
public void testAsMapSortedWritesThrough() { SortedSet<String> strings = new NonNavigableSortedSet(); Collections.addAll(strings, "one", "two", "three"); SortedMap<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map); assertEquals(Integer.valueOf(3), map.remove("two")); assertThat(strings).containsExactly("one", "three").inOrder(); }
public void testIteratesOnce() { Map<AnEnum, AnEnum> map = Maps.asMap( ImmutableSet.of(AnEnum.A), new Function<AnEnum, AnEnum>() { boolean used = false; @Override public AnEnum apply(AnEnum ae) { checkState(!used, "should not be applied more than once"); used = true; return ae; } }); ImmutableMap<AnEnum, AnEnum> copy = Maps.immutableEnumMap(map); assertThat(copy.entrySet()).containsExactly(Helpers.mapEntry(AnEnum.A, AnEnum.A)); }
@GwtIncompatible // NavigableMap public void testAsMapNavigableEmpty() { NavigableSet<String> strings = ImmutableSortedSet.of(); NavigableMap<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertThat(map.entrySet()).isEmpty(); assertTrue(map.isEmpty()); assertNull(map.get("five")); }
public void testAsMap() { Set<String> strings = ImmutableSet.of("one", "two", "three"); Map<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map); assertEquals(Integer.valueOf(5), map.get("three")); assertNull(map.get("five")); assertThat(map.entrySet()) .containsExactly(mapEntry("one", 3), mapEntry("two", 3), mapEntry("three", 5)) .inOrder(); }
@GwtIncompatible // NavigableMap public void testAsMapNavigableWritesThrough() { NavigableSet<String> strings = Sets.newTreeSet(); Collections.addAll(strings, "one", "two", "three"); NavigableMap<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map); assertEquals(Integer.valueOf(3), map.remove("two")); assertThat(strings).containsExactly("one", "three").inOrder(); assertEquals(mapEntry("three", 5), map.subMap("one", false, "zzz", true).pollLastEntry()); assertThat(strings).contains("one"); }