### Question:
NeighborCache implements GraphListener<V, E> { public Set<V> neighborsOf(V v) { return fetch(v, neighbors, k -> new Neighbors<>(Graphs.neighborListOf(graph, v))); } NeighborCache(Graph<V, E> graph); Set<V> predecessorsOf(V v); Set<V> successorsOf(V v); Set<V> neighborsOf(V v); List<V> neighborListOf(V v); @Override void edgeAdded(GraphEdgeChangeEvent<V, E> e); @Override void edgeRemoved(GraphEdgeChangeEvent<V, E> e); @Override void vertexAdded(GraphVertexChangeEvent<V> e); @Override void vertexRemoved(GraphVertexChangeEvent<V> e);  }### Answer:
@Test public void testNeighborSet() { ListenableGraph<String, Object> g = new DefaultListenableGraph<>(new SimpleGraph<>(Object.class)); NeighborCache<String, Object> cache = new NeighborCache<>(g); g.addGraphListener(cache); g.addVertex(V1); g.addVertex(V2); g.addEdge(V1, V2); Set<String> neighbors1 = cache.neighborsOf(V1); assertEquals(1, neighbors1.size()); assertEquals(true, neighbors1.contains(V2)); g.addVertex(V3); g.addEdge(V3, V1); Set<String> neighbors3 = cache.neighborsOf(V3); assertEquals(2, neighbors1.size()); assertEquals(true, neighbors1.contains(V3)); assertEquals(1, neighbors3.size()); assertEquals(true, neighbors3.contains(V1)); g.removeEdge(V3, V1); assertEquals(1, neighbors1.size()); assertEquals(false, neighbors1.contains(V3)); assertEquals(0, neighbors3.size()); g.removeVertex(V2); assertEquals(0, neighbors1.size()); }
@Test public void testVertexRemoval() { ListenableGraph<String, DefaultEdge> graph = new DefaultListenableGraph<>(new SimpleGraph<>(DefaultEdge.class)); final String A = "A"; final String B = "B"; final String C = "C"; final String D = "D"; NeighborCache<String, DefaultEdge> cache = new NeighborCache<>(graph); graph.addGraphListener(cache); graph.addVertex(A); graph.addVertex(B); graph.addVertex(C); graph.addVertex(D); graph.addEdge(D, A); graph.addEdge(D, B); graph.addEdge(D, C); Set<String> neighborsOfD = cache.neighborsOf(D); Set<String> neighborsOfC = cache.neighborsOf(C); Set<String> neighborsOfB = cache.neighborsOf(B); Set<String> neighborsOfA = cache.neighborsOf(A); assertThat(neighborsOfD, hasItems(A, B, C)); assertThat(neighborsOfA.size(), is(1)); assertThat(neighborsOfB.size(), is(1)); assertThat(neighborsOfC.size(), is(1)); graph.removeVertex(D); assertTrue(neighborsOfD.isEmpty()); assertThat(neighborsOfA.size(), is(0)); assertThat(neighborsOfB.size(), is(0)); assertThat(neighborsOfC.size(), is(0)); }