blob: 3e8960e637b8abfd712dd32cef90f0aa90b9354a [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
tome3489412014-08-29 02:30:38 -070016package org.onlab.graph;
17
18import java.util.Objects;
19
tomeadbb462014-09-07 16:10:19 -070020import static com.google.common.base.MoreObjects.toStringHelper;
tome3489412014-08-29 02:30:38 -070021import static com.google.common.base.Preconditions.checkNotNull;
22
23/**
24 * Abstract graph edge implementation.
25 */
26public 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) {
tomfc9a4ff2014-09-22 18:22:47 -070059 if (this == obj) {
60 return true;
61 }
tome3489412014-08-29 02:30:38 -070062 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() {
tomeadbb462014-09-07 16:10:19 -070071 return toStringHelper(this)
tome3489412014-08-29 02:30:38 -070072 .add("src", src)
73 .add("dst", dst)
74 .toString();
75 }
76}