blob: e2f3a26a6c51da59ecbc2319d21a5b4ba75806b5 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tome3489412014-08-29 02:30:38 -070019package org.onlab.graph;
20
21import java.util.Objects;
22
tomeadbb462014-09-07 16:10:19 -070023import static com.google.common.base.MoreObjects.toStringHelper;
tome3489412014-08-29 02:30:38 -070024import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
27 * Abstract graph edge implementation.
28 */
29public abstract class AbstractEdge<V extends Vertex> implements Edge<V> {
30
31 private final V src;
32 private final V dst;
33
34 /**
35 * Creates a new edge between the specified source and destination vertexes.
36 *
37 * @param src source vertex
38 * @param dst destination vertex
39 */
40 public AbstractEdge(V src, V dst) {
41 this.src = checkNotNull(src, "Source vertex cannot be null");
42 this.dst = checkNotNull(dst, "Destination vertex cannot be null");
43 }
44
45 @Override
46 public V src() {
47 return src;
48 }
49
50 @Override
51 public V dst() {
52 return dst;
53 }
54
55 @Override
56 public int hashCode() {
57 return Objects.hash(src, dst);
58 }
59
60 @Override
61 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -070062 if (this == obj) {
63 return true;
64 }
tome3489412014-08-29 02:30:38 -070065 if (obj instanceof AbstractEdge) {
66 final AbstractEdge other = (AbstractEdge) obj;
67 return Objects.equals(this.src, other.src) && Objects.equals(this.dst, other.dst);
68 }
69 return false;
70 }
71
72 @Override
73 public String toString() {
tomeadbb462014-09-07 16:10:19 -070074 return toStringHelper(this)
tome3489412014-08-29 02:30:38 -070075 .add("src", src)
76 .add("dst", dst)
77 .toString();
78 }
79}