protected final boolean isOrderingCompatible(EndpointPair<?> endpoints) { return endpoints.isOrdered() || !this.isDirected(); }
protected final boolean isOrderingCompatible(EndpointPair<?> endpoints) { return endpoints.isOrdered() || !this.isDirected(); }
@Override public boolean contains(@Nullable Object obj) { if (!(obj instanceof EndpointPair)) { return false; } EndpointPair<?> endpointPair = (EndpointPair<?>) obj; if (endpointPair.isOrdered()) { return false; } Set<N> adjacent = graph.adjacentNodes(node); Object nodeU = endpointPair.nodeU(); Object nodeV = endpointPair.nodeV(); return (node.equals(nodeV) && adjacent.contains(nodeU)) || (node.equals(nodeU) && adjacent.contains(nodeV)); } }
@Override public boolean contains(@Nullable Object obj) { if (!(obj instanceof EndpointPair)) { return false; } EndpointPair<?> endpointPair = (EndpointPair<?>) obj; if (!endpointPair.isOrdered()) { return false; } Object source = endpointPair.source(); Object target = endpointPair.target(); return (node.equals(source) && graph.successors(node).contains(target)) || (node.equals(target) && graph.predecessors(node).contains(source)); } }
static <N> EndpointPair<N> transpose(EndpointPair<N> endpoints) { if (endpoints.isOrdered()) { return EndpointPair.ordered(endpoints.target(), endpoints.source()); } return endpoints; }
@Override public boolean equals(@Nullable Object obj) { if (obj == this) { return true; } if (!(obj instanceof EndpointPair)) { return false; } EndpointPair<?> other = (EndpointPair<?>) obj; if (isOrdered() != other.isOrdered()) { return false; } return source().equals(other.source()) && target().equals(other.target()); }
@Override public boolean contains(@NullableDecl Object obj) { if (!(obj instanceof EndpointPair)) { return false; } EndpointPair<?> endpointPair = (EndpointPair<?>) obj; if (endpointPair.isOrdered()) { return false; } Set<N> adjacent = graph.adjacentNodes(node); Object nodeU = endpointPair.nodeU(); Object nodeV = endpointPair.nodeV(); return (node.equals(nodeV) && adjacent.contains(nodeU)) || (node.equals(nodeU) && adjacent.contains(nodeV)); } }
@Override public boolean contains(@NullableDecl Object obj) { if (!(obj instanceof EndpointPair)) { return false; } EndpointPair<?> endpointPair = (EndpointPair<?>) obj; if (!endpointPair.isOrdered()) { return false; } Object source = endpointPair.source(); Object target = endpointPair.target(); return (node.equals(source) && graph.successors(node).contains(target)) || (node.equals(target) && graph.predecessors(node).contains(source)); } }
@Override public boolean contains(@NullableDecl Object obj) { if (!(obj instanceof EndpointPair)) { return false; } EndpointPair<?> endpointPair = (EndpointPair<?>) obj; if (endpointPair.isOrdered()) { return false; } Set<N> adjacent = graph.adjacentNodes(node); Object nodeU = endpointPair.nodeU(); Object nodeV = endpointPair.nodeV(); return (node.equals(nodeV) && adjacent.contains(nodeU)) || (node.equals(nodeU) && adjacent.contains(nodeV)); } }
@Override public boolean contains(@NullableDecl Object obj) { if (!(obj instanceof EndpointPair)) { return false; } EndpointPair<?> endpointPair = (EndpointPair<?>) obj; if (!endpointPair.isOrdered()) { return false; } Object source = endpointPair.source(); Object target = endpointPair.target(); return (node.equals(source) && graph.successors(node).contains(target)) || (node.equals(target) && graph.predecessors(node).contains(source)); } }
@Override public boolean equals(@NullableDecl Object obj) { if (obj == this) { return true; } if (!(obj instanceof EndpointPair)) { return false; } EndpointPair<?> other = (EndpointPair<?>) obj; if (isOrdered() != other.isOrdered()) { return false; } return source().equals(other.source()) && target().equals(other.target()); }
@Override public boolean equals(@Nullable Object obj) { if (obj == this) { return true; } if (!(obj instanceof EndpointPair)) { return false; } EndpointPair<?> other = (EndpointPair<?>) obj; if (isOrdered() != other.isOrdered()) { return false; } // Equivalent to the following simple implementation: // boolean condition1 = nodeU().equals(other.nodeU()) && nodeV().equals(other.nodeV()); // boolean condition2 = nodeU().equals(other.nodeV()) && nodeV().equals(other.nodeU()); // return condition1 || condition2; if (nodeU().equals(other.nodeU())) { // check condition1 // Here's the tricky bit. We don't have to explicitly check for condition2 in this case. // Why? The second half of condition2 requires that nodeV equals other.nodeU. // We already know that nodeU equals other.nodeU. Combined with the earlier statement, // and the transitive property of equality, this implies that nodeU equals nodeV. // If nodeU equals nodeV, condition1 == condition2, so checking condition1 is sufficient. return nodeV().equals(other.nodeV()); } return nodeU().equals(other.nodeV()) && nodeV().equals(other.nodeU()); // check condition2 }
@SuppressWarnings("unchecked") @Override public boolean contains(@NullableDecl Object obj) { if (!(obj instanceof EndpointPair)) { return false; } EndpointPair<?> endpointPair = (EndpointPair<?>) obj; return isDirected() == endpointPair.isOrdered() && nodes().contains(endpointPair.nodeU()) && successors((N) endpointPair.nodeU()).contains(endpointPair.nodeV()); } };
@SuppressWarnings("unchecked") @Override public boolean contains(@NullableDecl Object obj) { if (!(obj instanceof EndpointPair)) { return false; } EndpointPair<?> endpointPair = (EndpointPair<?>) obj; return isDirected() == endpointPair.isOrdered() && nodes().contains(endpointPair.nodeU()) && successors((N) endpointPair.nodeU()).contains(endpointPair.nodeV()); } };
@Override public boolean equals(@NullableDecl Object obj) { if (obj == this) { return true; } if (!(obj instanceof EndpointPair)) { return false; } EndpointPair<?> other = (EndpointPair<?>) obj; if (isOrdered() != other.isOrdered()) { return false; } return source().equals(other.source()) && target().equals(other.target()); }
@Test public void testSelfLoop() { EndpointPair<String> unordered = EndpointPair.unordered("node", "node"); assertThat(unordered.isOrdered()).isFalse(); assertThat(unordered).containsExactly("node", "node"); assertThat(unordered.nodeU()).isEqualTo("node"); assertThat(unordered.nodeV()).isEqualTo("node"); assertThat(unordered.adjacentNode("node")).isEqualTo("node"); assertThat(unordered.toString()).isEqualTo("[node, node]"); }
@Test public void testUnorderedEndpointPair() { EndpointPair<String> unordered = EndpointPair.unordered("chicken", "egg"); assertThat(unordered.isOrdered()).isFalse(); assertThat(unordered).containsExactly("chicken", "egg"); assertThat(ImmutableSet.of(unordered.nodeU(), unordered.nodeV())) .containsExactly("chicken", "egg"); assertThat(unordered.adjacentNode(unordered.nodeU())).isEqualTo(unordered.nodeV()); assertThat(unordered.adjacentNode(unordered.nodeV())).isEqualTo(unordered.nodeU()); assertThat(unordered.toString()).contains("chicken"); assertThat(unordered.toString()).contains("egg"); }
@Override public boolean equals(@NullableDecl Object obj) { if (obj == this) { return true; } if (!(obj instanceof EndpointPair)) { return false; } EndpointPair<?> other = (EndpointPair<?>) obj; if (isOrdered() != other.isOrdered()) { return false; } // Equivalent to the following simple implementation: // boolean condition1 = nodeU().equals(other.nodeU()) && nodeV().equals(other.nodeV()); // boolean condition2 = nodeU().equals(other.nodeV()) && nodeV().equals(other.nodeU()); // return condition1 || condition2; if (nodeU().equals(other.nodeU())) { // check condition1 // Here's the tricky bit. We don't have to explicitly check for condition2 in this case. // Why? The second half of condition2 requires that nodeV equals other.nodeU. // We already know that nodeU equals other.nodeU. Combined with the earlier statement, // and the transitive property of equality, this implies that nodeU equals nodeV. // If nodeU equals nodeV, condition1 == condition2, so checking condition1 is sufficient. return nodeV().equals(other.nodeV()); } return nodeU().equals(other.nodeV()) && nodeV().equals(other.nodeU()); // check condition2 }
@Test public void testOrderedEndpointPair() { EndpointPair<String> ordered = EndpointPair.ordered("source", "target"); assertThat(ordered.isOrdered()).isTrue(); assertThat(ordered).containsExactly("source", "target").inOrder(); assertThat(ordered.source()).isEqualTo("source"); assertThat(ordered.target()).isEqualTo("target"); assertThat(ordered.nodeU()).isEqualTo("source"); assertThat(ordered.nodeV()).isEqualTo("target"); assertThat(ordered.adjacentNode("source")).isEqualTo("target"); assertThat(ordered.adjacentNode("target")).isEqualTo("source"); assertThat(ordered.toString()).isEqualTo("<source -> target>"); }
@Override public boolean equals(@NullableDecl Object obj) { if (obj == this) { return true; } if (!(obj instanceof EndpointPair)) { return false; } EndpointPair<?> other = (EndpointPair<?>) obj; if (isOrdered() != other.isOrdered()) { return false; } // Equivalent to the following simple implementation: // boolean condition1 = nodeU().equals(other.nodeU()) && nodeV().equals(other.nodeV()); // boolean condition2 = nodeU().equals(other.nodeV()) && nodeV().equals(other.nodeU()); // return condition1 || condition2; if (nodeU().equals(other.nodeU())) { // check condition1 // Here's the tricky bit. We don't have to explicitly check for condition2 in this case. // Why? The second half of condition2 requires that nodeV equals other.nodeU. // We already know that nodeU equals other.nodeU. Combined with the earlier statement, // and the transitive property of equality, this implies that nodeU equals nodeV. // If nodeU equals nodeV, condition1 == condition2, so checking condition1 is sufficient. return nodeV().equals(other.nodeV()); } return nodeU().equals(other.nodeV()) && nodeV().equals(other.nodeU()); // check condition2 }