001/*
002 * Copyright (C) 2016 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.graph;
018
019import static com.google.common.base.Preconditions.checkNotNull;
020import static com.google.common.graph.GraphConstants.NOT_AVAILABLE_ON_UNDIRECTED;
021
022import com.google.common.annotations.Beta;
023import com.google.common.base.Objects;
024import com.google.common.collect.Iterators;
025import com.google.common.collect.UnmodifiableIterator;
026import com.google.errorprone.annotations.Immutable;
027import javax.annotation.CheckForNull;
028
029/**
030 * An immutable pair representing the two endpoints of an edge in a graph. The {@link EndpointPair}
031 * of a directed edge is an ordered pair of nodes ({@link #source()} and {@link #target()}). The
032 * {@link EndpointPair} of an undirected edge is an unordered pair of nodes ({@link #nodeU()} and
033 * {@link #nodeV()}).
034 *
035 * <p>The edge is a self-loop if, and only if, the two endpoints are equal.
036 *
037 * @author James Sexton
038 * @since 20.0
039 */
040@Beta
041@Immutable(containerOf = {"N"})
042public abstract class EndpointPair<N> implements Iterable<N> {
043  private final N nodeU;
044  private final N nodeV;
045
046  private EndpointPair(N nodeU, N nodeV) {
047    this.nodeU = checkNotNull(nodeU);
048    this.nodeV = checkNotNull(nodeV);
049  }
050
051  /** Returns an {@link EndpointPair} representing the endpoints of a directed edge. */
052  public static <N> EndpointPair<N> ordered(N source, N target) {
053    return new Ordered<>(source, target);
054  }
055
056  /** Returns an {@link EndpointPair} representing the endpoints of an undirected edge. */
057  public static <N> EndpointPair<N> unordered(N nodeU, N nodeV) {
058    // Swap nodes on purpose to prevent callers from relying on the "ordering" of an unordered pair.
059    return new Unordered<>(nodeV, nodeU);
060  }
061
062  /** Returns an {@link EndpointPair} representing the endpoints of an edge in {@code graph}. */
063  static <N> EndpointPair<N> of(Graph<?> graph, N nodeU, N nodeV) {
064    return graph.isDirected() ? ordered(nodeU, nodeV) : unordered(nodeU, nodeV);
065  }
066
067  /** Returns an {@link EndpointPair} representing the endpoints of an edge in {@code network}. */
068  static <N> EndpointPair<N> of(Network<?, ?> network, N nodeU, N nodeV) {
069    return network.isDirected() ? ordered(nodeU, nodeV) : unordered(nodeU, nodeV);
070  }
071
072  /**
073   * If this {@link EndpointPair} {@link #isOrdered()}, returns the node which is the source.
074   *
075   * @throws UnsupportedOperationException if this {@link EndpointPair} is not ordered
076   */
077  public abstract N source();
078
079  /**
080   * If this {@link EndpointPair} {@link #isOrdered()}, returns the node which is the target.
081   *
082   * @throws UnsupportedOperationException if this {@link EndpointPair} is not ordered
083   */
084  public abstract N target();
085
086  /**
087   * If this {@link EndpointPair} {@link #isOrdered()} returns the {@link #source()}; otherwise,
088   * returns an arbitrary (but consistent) endpoint of the origin edge.
089   */
090  public final N nodeU() {
091    return nodeU;
092  }
093
094  /**
095   * Returns the node {@link #adjacentNode(Object) adjacent} to {@link #nodeU()} along the origin
096   * edge. If this {@link EndpointPair} {@link #isOrdered()}, this is equal to {@link #target()}.
097   */
098  public final N nodeV() {
099    return nodeV;
100  }
101
102  /**
103   * Returns the node that is adjacent to {@code node} along the origin edge.
104   *
105   * @throws IllegalArgumentException if this {@link EndpointPair} does not contain {@code node}
106   * @since 20.0 (but the argument type was changed from {@code Object} to {@code N} in 31.0)
107   */
108  public final N adjacentNode(N node) {
109    if (node.equals(nodeU)) {
110      return nodeV;
111    } else if (node.equals(nodeV)) {
112      return nodeU;
113    } else {
114      throw new IllegalArgumentException("EndpointPair " + this + " does not contain node " + node);
115    }
116  }
117
118  /**
119   * Returns {@code true} if this {@link EndpointPair} is an ordered pair (i.e. represents the
120   * endpoints of a directed edge).
121   */
122  public abstract boolean isOrdered();
123
124  /** Iterates in the order {@link #nodeU()}, {@link #nodeV()}. */
125  @Override
126  public final UnmodifiableIterator<N> iterator() {
127    return Iterators.forArray(nodeU, nodeV);
128  }
129
130  /**
131   * Two ordered {@link EndpointPair}s are equal if their {@link #source()} and {@link #target()}
132   * are equal. Two unordered {@link EndpointPair}s are equal if they contain the same nodes. An
133   * ordered {@link EndpointPair} is never equal to an unordered {@link EndpointPair}.
134   */
135  @Override
136  public abstract boolean equals(@CheckForNull Object obj);
137
138  /**
139   * The hashcode of an ordered {@link EndpointPair} is equal to {@code Objects.hashCode(source(),
140   * target())}. The hashcode of an unordered {@link EndpointPair} is equal to {@code
141   * nodeU().hashCode() + nodeV().hashCode()}.
142   */
143  @Override
144  public abstract int hashCode();
145
146  private static final class Ordered<N> extends EndpointPair<N> {
147    private Ordered(N source, N target) {
148      super(source, target);
149    }
150
151    @Override
152    public N source() {
153      return nodeU();
154    }
155
156    @Override
157    public N target() {
158      return nodeV();
159    }
160
161    @Override
162    public boolean isOrdered() {
163      return true;
164    }
165
166    @Override
167    public boolean equals(@CheckForNull Object obj) {
168      if (obj == this) {
169        return true;
170      }
171      if (!(obj instanceof EndpointPair)) {
172        return false;
173      }
174
175      EndpointPair<?> other = (EndpointPair<?>) obj;
176      if (isOrdered() != other.isOrdered()) {
177        return false;
178      }
179
180      return source().equals(other.source()) && target().equals(other.target());
181    }
182
183    @Override
184    public int hashCode() {
185      return Objects.hashCode(source(), target());
186    }
187
188    @Override
189    public String toString() {
190      return "<" + source() + " -> " + target() + ">";
191    }
192  }
193
194  private static final class Unordered<N> extends EndpointPair<N> {
195    private Unordered(N nodeU, N nodeV) {
196      super(nodeU, nodeV);
197    }
198
199    @Override
200    public N source() {
201      throw new UnsupportedOperationException(NOT_AVAILABLE_ON_UNDIRECTED);
202    }
203
204    @Override
205    public N target() {
206      throw new UnsupportedOperationException(NOT_AVAILABLE_ON_UNDIRECTED);
207    }
208
209    @Override
210    public boolean isOrdered() {
211      return false;
212    }
213
214    @Override
215    public boolean equals(@CheckForNull Object obj) {
216      if (obj == this) {
217        return true;
218      }
219      if (!(obj instanceof EndpointPair)) {
220        return false;
221      }
222
223      EndpointPair<?> other = (EndpointPair<?>) obj;
224      if (isOrdered() != other.isOrdered()) {
225        return false;
226      }
227
228      // Equivalent to the following simple implementation:
229      // boolean condition1 = nodeU().equals(other.nodeU()) && nodeV().equals(other.nodeV());
230      // boolean condition2 = nodeU().equals(other.nodeV()) && nodeV().equals(other.nodeU());
231      // return condition1 || condition2;
232      if (nodeU().equals(other.nodeU())) { // check condition1
233        // Here's the tricky bit. We don't have to explicitly check for condition2 in this case.
234        // Why? The second half of condition2 requires that nodeV equals other.nodeU.
235        // We already know that nodeU equals other.nodeU. Combined with the earlier statement,
236        // and the transitive property of equality, this implies that nodeU equals nodeV.
237        // If nodeU equals nodeV, condition1 == condition2, so checking condition1 is sufficient.
238        return nodeV().equals(other.nodeV());
239      }
240      return nodeU().equals(other.nodeV()) && nodeV().equals(other.nodeU()); // check condition2
241    }
242
243    @Override
244    public int hashCode() {
245      return nodeU().hashCode() + nodeV().hashCode();
246    }
247
248    @Override
249    public String toString() {
250      return "[" + nodeU() + ", " + nodeV() + "]";
251    }
252  }
253}