Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
| 22 | import com.google.common.base.MoreObjects; |
| 23 | |
| 24 | // TODO Consider renaming. |
| 25 | // it's an identifier for a Link, but it's not ElementId, so not using LinkId. |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 26 | |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Immutable representation of a link identity. |
| 29 | */ |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 30 | public final class LinkKey { |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 31 | |
| 32 | private final ConnectPoint src; |
| 33 | private final ConnectPoint dst; |
| 34 | |
| 35 | /** |
| 36 | * Returns source connection point. |
| 37 | * |
| 38 | * @return source connection point |
| 39 | */ |
| 40 | public ConnectPoint src() { |
| 41 | return src; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns destination connection point. |
| 46 | * |
| 47 | * @return destination connection point |
| 48 | */ |
| 49 | public ConnectPoint dst() { |
| 50 | return dst; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Creates a link identifier with source and destination connection point. |
| 55 | * |
| 56 | * @param src source connection point |
| 57 | * @param dst destination connection point |
| 58 | */ |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 59 | private LinkKey(ConnectPoint src, ConnectPoint dst) { |
| 60 | this.src = checkNotNull(src); |
| 61 | this.dst = checkNotNull(dst); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Creates a link identifier with source and destination connection point. |
| 66 | * |
| 67 | * @param src source connection point |
| 68 | * @param dst destination connection point |
| 69 | * @return a link identifier |
| 70 | */ |
| 71 | public static LinkKey linkKey(ConnectPoint src, ConnectPoint dst) { |
| 72 | return new LinkKey(src, dst); |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 73 | } |
| 74 | |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Creates a link identifier for the specified link. |
| 77 | * |
| 78 | * @param link link descriptor |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 79 | * @return a link identifier |
tom | 95329eb | 2014-10-06 08:40:06 -0700 | [diff] [blame] | 80 | */ |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 81 | public static LinkKey linkKey(Link link) { |
| 82 | return new LinkKey(link.src(), link.dst()); |
| 83 | } |
| 84 | |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 85 | @Override |
| 86 | public int hashCode() { |
Thomas Vachuska | c5ea950 | 2014-11-04 10:25:46 -0800 | [diff] [blame] | 87 | return Objects.hash(src, dst); |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public boolean equals(Object obj) { |
| 92 | if (this == obj) { |
| 93 | return true; |
| 94 | } |
| 95 | if (obj instanceof LinkKey) { |
| 96 | final LinkKey other = (LinkKey) obj; |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 97 | return Objects.equals(this.src, other.src) && |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 98 | Objects.equals(this.dst, other.dst); |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public String toString() { |
| 105 | return MoreObjects.toStringHelper(getClass()) |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 106 | .add("src", src) |
Yuta HIGUCHI | 06dc6b9 | 2014-09-25 16:06:16 -0700 | [diff] [blame] | 107 | .add("dst", dst) |
| 108 | .toString(); |
| 109 | } |
| 110 | } |