@Test public void equivalent_propertiesDiffer() { network.addEdge(N1, N2, E12); MutableNetwork<Integer, String> g2 = NetworkBuilder.from(network) .allowsParallelEdges(!network.allowsParallelEdges()) .allowsSelfLoops(!network.allowsSelfLoops()) .build(); g2.addEdge(N1, N2, E12); assertThat(network).isEqualTo(g2); }
@Override public boolean allowsParallelEdges() { return delegate.allowsParallelEdges(); }
@Override public synchronized boolean allowsParallelEdges() { return delegate.allowsParallelEdges(); }
private List<N> generateAdjacentNodes(int edgesToAdd) { Preconditions.checkArgument(edgesToAdd >= 1); WeightedChoice<N> nodeChooser = buildNodeProbabilities(); List<N> adjacentNodes = new ArrayList<N>(edgesToAdd); while (adjacentNodes.size() < edgesToAdd) { N attach_point = nodeChooser.nextItem(); // if parallel edges are not allowed, skip this node if already present if (!graph.allowsParallelEdges() && adjacentNodes.contains(attach_point)) { continue; } adjacentNodes.add(attach_point); } return adjacentNodes; }
@Override public E addEdge(V sourceVertex, V targetVertex) { assertVertexExist(sourceVertex); assertVertexExist(targetVertex); if (!network.allowsParallelEdges() && containsEdge(sourceVertex, targetVertex)) { return null; } if (!network.allowsSelfLoops() && sourceVertex.equals(targetVertex)) { throw new IllegalArgumentException(LOOPS_NOT_ALLOWED); } if (edgeSupplier == null) { throw new UnsupportedOperationException("The graph contains no edge supplier"); } E e = edgeSupplier.get(); if (network.addEdge(sourceVertex, targetVertex, e)) { return e; } return null; }
@Override public boolean addEdge(V sourceVertex, V targetVertex, E e) { if (e == null) { throw new NullPointerException(); } assertVertexExist(sourceVertex); assertVertexExist(targetVertex); if (!network.allowsParallelEdges() && containsEdge(sourceVertex, targetVertex)) { return false; } if (!network.allowsSelfLoops() && sourceVertex.equals(targetVertex)) { throw new IllegalArgumentException(LOOPS_NOT_ALLOWED); } if (network.addEdge(sourceVertex, targetVertex, e)) { return true; } return false; }
@Test public void equivalent_propertiesDiffer() { network.addEdge(N1, N2, E12); MutableNetwork<Integer, String> g2 = NetworkBuilder.from(network) .allowsParallelEdges(!network.allowsParallelEdges()) .allowsSelfLoops(!network.allowsSelfLoops()) .build(); g2.addEdge(N1, N2, E12); assertThat(network).isEqualTo(g2); }