Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 16 | package org.onlab.graph; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 20 | import static com.google.common.base.MoreObjects.toStringHelper; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | /** |
| 24 | * Abstract graph edge implementation. |
| 25 | */ |
| 26 | public abstract class AbstractEdge<V extends Vertex> implements Edge<V> { |
| 27 | |
| 28 | private final V src; |
| 29 | private final V dst; |
| 30 | |
| 31 | /** |
| 32 | * Creates a new edge between the specified source and destination vertexes. |
| 33 | * |
| 34 | * @param src source vertex |
| 35 | * @param dst destination vertex |
| 36 | */ |
| 37 | public AbstractEdge(V src, V dst) { |
| 38 | this.src = checkNotNull(src, "Source vertex cannot be null"); |
| 39 | this.dst = checkNotNull(dst, "Destination vertex cannot be null"); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public V src() { |
| 44 | return src; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public V dst() { |
| 49 | return dst; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public int hashCode() { |
| 54 | return Objects.hash(src, dst); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 59 | if (this == obj) { |
| 60 | return true; |
| 61 | } |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 62 | if (obj instanceof AbstractEdge) { |
| 63 | final AbstractEdge other = (AbstractEdge) obj; |
| 64 | return Objects.equals(this.src, other.src) && Objects.equals(this.dst, other.dst); |
| 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String toString() { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 71 | return toStringHelper(this) |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 72 | .add("src", src) |
| 73 | .add("dst", dst) |
| 74 | .toString(); |
| 75 | } |
| 76 | } |