Class EndpointPair<N>

  • All Implemented Interfaces:
    java.lang.Iterable<N>
    Direct Known Subclasses:
    EndpointPair.Ordered, EndpointPair.Unordered

    public abstract class EndpointPair<N>
    extends java.lang.Object
    implements java.lang.Iterable<N>
    An immutable pair representing the two endpoints of an edge in a graph. The EndpointPair of a directed edge is an ordered pair of nodes (source() and target()). The EndpointPair of an undirected edge is an unordered pair of nodes (nodeU() and nodeV()).

    The edge is a self-loop if, and only if, the two endpoints are equal.

    Since:
    20.0
    • Field Detail

      • nodeU

        private final N nodeU
      • nodeV

        private final N nodeV
    • Constructor Detail

      • EndpointPair

        private EndpointPair​(N nodeU,
                             N nodeV)
    • Method Detail

      • ordered

        public static <N> EndpointPair<N> ordered​(N source,
                                                  N target)
        Returns an EndpointPair representing the endpoints of a directed edge.
      • unordered

        public static <N> EndpointPair<N> unordered​(N nodeU,
                                                    N nodeV)
        Returns an EndpointPair representing the endpoints of an undirected edge.
      • of

        static <N> EndpointPair<N> of​(Graph<?> graph,
                                      N nodeU,
                                      N nodeV)
        Returns an EndpointPair representing the endpoints of an edge in graph.
      • of

        static <N> EndpointPair<N> of​(Network<?,​?> network,
                                      N nodeU,
                                      N nodeV)
        Returns an EndpointPair representing the endpoints of an edge in network.
      • source

        public abstract N source()
        If this EndpointPair isOrdered(), returns the node which is the source.
        Throws:
        java.lang.UnsupportedOperationException - if this EndpointPair is not ordered
      • target

        public abstract N target()
        If this EndpointPair isOrdered(), returns the node which is the target.
        Throws:
        java.lang.UnsupportedOperationException - if this EndpointPair is not ordered
      • nodeU

        public final N nodeU()
        If this EndpointPair isOrdered() returns the source(); otherwise, returns an arbitrary (but consistent) endpoint of the origin edge.
      • adjacentNode

        public final N adjacentNode​(N node)
        Returns the node that is adjacent to node along the origin edge.
        Throws:
        java.lang.IllegalArgumentException - if this EndpointPair does not contain node
        Since:
        20.0 (but the argument type was changed from Object to N in 31.0)
      • isOrdered

        public abstract boolean isOrdered()
        Returns true if this EndpointPair is an ordered pair (i.e. represents the endpoints of a directed edge).
      • equals

        public abstract boolean equals​(@CheckForNull
                                       java.lang.Object obj)
        Two ordered EndpointPairs are equal if their source() and target() are equal. Two unordered EndpointPairs are equal if they contain the same nodes. An ordered EndpointPair is never equal to an unordered EndpointPair.
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public abstract int hashCode()
        The hashcode of an ordered EndpointPair is equal to Objects.hashCode(source(), target()). The hashcode of an unordered EndpointPair is equal to nodeU().hashCode() + nodeV().hashCode().
        Overrides:
        hashCode in class java.lang.Object