protected final GraphConnections<N, V> checkedConnections(N node) { GraphConnections<N, V> connections = nodeConnections.get(node); if (connections == null) { checkNotNull(node); throw new IllegalArgumentException("Node " + node + " is not an element of this graph."); } return connections; }
protected final NetworkConnections<N, E> checkedConnections(N node) { NetworkConnections<N, E> connections = nodeConnections.get(node); if (connections == null) { checkNotNull(node); throw new IllegalArgumentException(String.format(NODE_NOT_IN_GRAPH, node)); } return connections; }
protected final N checkedReferenceNode(E edge) { N referenceNode = edgeToReferenceNode.get(edge); if (referenceNode == null) { checkNotNull(edge); throw new IllegalArgumentException(String.format(EDGE_NOT_IN_GRAPH, edge)); } return referenceNode; }
protected final V edgeValueOrDefault_internal(N nodeU, N nodeV, V defaultValue) { GraphConnections<N, V> connectionsU = nodeConnections.get(nodeU); V value = (connectionsU == null) ? null : connectionsU.value(nodeV); return value == null ? defaultValue : value; } }
protected final boolean hasEdgeConnecting_internal(N nodeU, N nodeV) { GraphConnections<N, V> connectionsU = nodeConnections.get(nodeU); return (connectionsU != null) && connectionsU.successors().contains(nodeV); }
protected final GraphConnections<N, V> checkedConnections(N node) { GraphConnections<N, V> connections = nodeConnections.get(node); if (connections == null) { checkNotNull(node); throw new IllegalArgumentException("Node " + node + " is not an element of this graph."); } return connections; }
protected final NetworkConnections<N, E> checkedConnections(N node) { NetworkConnections<N, E> connections = nodeConnections.get(node); if (connections == null) { checkNotNull(node); throw new IllegalArgumentException(String.format(NODE_NOT_IN_GRAPH, node)); } return connections; }
protected final N checkedReferenceNode(E edge) { N referenceNode = edgeToReferenceNode.get(edge); if (referenceNode == null) { checkNotNull(edge); throw new IllegalArgumentException(String.format(EDGE_NOT_IN_GRAPH, edge)); } return referenceNode; }
@Override public boolean hasEdgeConnecting(N nodeU, N nodeV) { checkNotNull(nodeU); checkNotNull(nodeV); GraphConnections<N, V> connectionsU = nodeConnections.get(nodeU); return (connectionsU != null) && connectionsU.successors().contains(nodeV); }
@Override @CanIgnoreReturnValue public V removeEdge(N nodeU, N nodeV) { checkNotNull(nodeU, "nodeU"); checkNotNull(nodeV, "nodeV"); GraphConnections<N, V> connectionsU = nodeConnections.get(nodeU); GraphConnections<N, V> connectionsV = nodeConnections.get(nodeV); if (connectionsU == null || connectionsV == null) { return null; } V previousValue = connectionsU.removeSuccessor(nodeV); if (previousValue != null) { connectionsV.removePredecessor(nodeU); checkNonNegative(--edgeCount); } return previousValue; }
@Override @CanIgnoreReturnValue public boolean removeEdge(E edge) { checkNotNull(edge, "edge"); N nodeU = edgeToReferenceNode.get(edge); if (nodeU == null) { return false; } NetworkConnections<N, E> connectionsU = nodeConnections.get(nodeU); N nodeV = connectionsU.adjacentNode(edge); NetworkConnections<N, E> connectionsV = nodeConnections.get(nodeV); connectionsU.removeOutEdge(edge); connectionsV.removeInEdge(edge, allowsSelfLoops() && nodeU.equals(nodeV)); edgeToReferenceNode.remove(edge); return true; }
@Override public EndpointPair<N> incidentNodes(E edge) { N nodeU = checkedReferenceNode(edge); N nodeV = nodeConnections.get(nodeU).adjacentNode(edge); return EndpointPair.of(this, nodeU, nodeV); }
@Override @CanIgnoreReturnValue public boolean removeNode(N node) { checkNotNull(node, "node"); NetworkConnections<N, E> connections = nodeConnections.get(node); if (connections == null) { return false; } // Since views are returned, we need to copy the edges that will be removed. // Thus we avoid modifying the underlying view while iterating over it. for (E edge : ImmutableList.copyOf(connections.incidentEdges())) { removeEdge(edge); } nodeConnections.remove(node); return true; }
@Override @CanIgnoreReturnValue public V putEdgeValue(N nodeU, N nodeV, V value) { checkNotNull(nodeU, "nodeU"); checkNotNull(nodeV, "nodeV"); checkNotNull(value, "value"); if (!allowsSelfLoops()) { checkArgument(!nodeU.equals(nodeV), SELF_LOOPS_NOT_ALLOWED, nodeU); } GraphConnections<N, V> connectionsU = nodeConnections.get(nodeU); if (connectionsU == null) { connectionsU = addNodeInternal(nodeU); } V previousValue = connectionsU.addSuccessor(nodeV, value); GraphConnections<N, V> connectionsV = nodeConnections.get(nodeV); if (connectionsV == null) { connectionsV = addNodeInternal(nodeV); } connectionsV.addPredecessor(nodeU, value); if (previousValue == null) { checkPositive(++edgeCount); } return previousValue; }
@Override @NullableDecl public V edgeValueOrDefault(N nodeU, N nodeV, @NullableDecl V defaultValue) { checkNotNull(nodeU); checkNotNull(nodeV); GraphConnections<N, V> connectionsU = nodeConnections.get(nodeU); V value = (connectionsU == null) ? null : connectionsU.value(nodeV); return value == null ? defaultValue : value; }
@Override public EndpointPair<N> incidentNodes(E edge) { N nodeU = checkedReferenceNode(edge); N nodeV = nodeConnections.get(nodeU).adjacentNode(edge); return EndpointPair.of(this, nodeU, nodeV); }
@Test public void testRemoveEqualKeyWithDifferentReference() { String fooReference1 = new String("foo"); String fooReference2 = new String("foo"); assertThat(fooReference1).isNotSameAs(fooReference2); assertThat(mapCache.put(fooReference1, "bar")).isNull(); assertThat(mapCache.get(fooReference1)).isEqualTo("bar"); // ensure first reference is cached assertThat(mapCache.remove(fooReference2)).isEqualTo("bar"); assertThat(mapCache.get(fooReference1)).isNull(); }
@Test public void testHandleNulls() { mapCache.put("foo", "bar"); mapCache.put("non-null key", null); mapCache.put(null, "non-null value"); assertThat(mapCache.get("foo")).isEqualTo("bar"); assertThat(mapCache.get("non-null key")).isNull(); assertThat(mapCache.get(null)).isEqualTo("non-null value"); assertThat(mapCache.containsKey("foo")).isTrue(); assertThat(mapCache.containsKey("bar")).isFalse(); assertThat(mapCache.containsKey("non-null key")).isTrue(); assertThat(mapCache.containsKey(null)).isTrue(); // Test again - in reverse order. assertThat(mapCache.get(null)).isEqualTo("non-null value"); assertThat(mapCache.get("non-null key")).isNull(); assertThat(mapCache.get("foo")).isEqualTo("bar"); } }
@Test public void testKeySetIterator() { mapCache.put("A", "A_value"); mapCache.put("B", "B_value"); mapCache.put("C", "C_value"); assertThat(mapCache.unmodifiableKeySet()).hasSize(3); for (String key : mapCache.unmodifiableKeySet()) { assertThat(mapCache.get(key)).isEqualTo(key + "_value"); } }