HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 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 | */ |
HIGUCHI Yuta | f46dc4f | 2016-05-13 11:24:13 -0700 | [diff] [blame] | 16 | package org.onosproject.net.utils; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import java.util.Objects; |
| 21 | |
| 22 | import org.apache.commons.lang3.builder.EqualsBuilder; |
| 23 | import org.onosproject.net.Annotations; |
| 24 | import org.onosproject.net.Element; |
| 25 | import org.onosproject.net.Port; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | |
| 28 | import com.google.common.annotations.Beta; |
| 29 | import com.google.common.base.MoreObjects; |
| 30 | import com.google.common.base.MoreObjects.ToStringHelper; |
| 31 | |
| 32 | /** |
| 33 | * A Port which forwards all its method calls to another Port. |
| 34 | */ |
| 35 | @Beta |
| 36 | public abstract class ForwardingPort implements Port { |
| 37 | |
| 38 | private final Port delegate; |
| 39 | |
| 40 | protected ForwardingPort(Port delegate) { |
| 41 | this.delegate = checkNotNull(delegate); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public int hashCode() { |
| 46 | return Objects.hash(element().id(), |
| 47 | number(), |
| 48 | isEnabled(), |
| 49 | type(), |
| 50 | portSpeed(), |
| 51 | annotations()); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns {@link EqualsBuilder} comparing all Port attributes |
| 56 | * including annotations. |
| 57 | * <p> |
| 58 | * To add extra fields to equality, |
| 59 | * call {@code super.toEqualsBuilder(..)} and append fields. |
| 60 | * To remove field from comparison, override this method |
| 61 | * or manually implement equals(). |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 62 | * |
| 63 | * @param that object to compare to |
| 64 | * @return builder object |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 65 | */ |
| 66 | protected EqualsBuilder toEqualsBuilder(Port that) { |
| 67 | if (that == null) { |
| 68 | return new EqualsBuilder().appendSuper(false); |
| 69 | } |
| 70 | return new EqualsBuilder() |
| 71 | .append(this.element().id(), that.element().id()) |
| 72 | .append(this.number(), that.number()) |
| 73 | .append(this.isEnabled(), that.isEnabled()) |
| 74 | .append(this.type(), that.type()) |
| 75 | .append(this.portSpeed(), that.portSpeed()) |
| 76 | .append(this.annotations(), that.annotations()); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public boolean equals(Object obj) { |
| 81 | if (this == obj) { |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | if (obj != null && getClass() == obj.getClass()) { |
| 86 | final ForwardingPort that = (ForwardingPort) obj; |
| 87 | return toEqualsBuilder(that) |
| 88 | .isEquals(); |
| 89 | } |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns {@link ToStringHelper} with Port attributes excluding annotations. |
| 95 | * |
| 96 | * @return {@link ToStringHelper} |
| 97 | */ |
| 98 | protected ToStringHelper toStringHelper() { |
| 99 | return MoreObjects.toStringHelper(this) |
| 100 | .add("element", element().id()) |
| 101 | .add("number", number()) |
| 102 | .add("isEnabled", isEnabled()) |
| 103 | .add("type", type()) |
| 104 | .add("portSpeed", portSpeed()); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public String toString() { |
| 109 | return toStringHelper() |
| 110 | .toString(); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Annotations annotations() { |
| 115 | return delegate.annotations(); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public Element element() { |
| 120 | return delegate.element(); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public PortNumber number() { |
| 125 | return delegate.number(); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public boolean isEnabled() { |
| 130 | return delegate.isEnabled(); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Port.Type type() { |
| 135 | return delegate.type(); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public long portSpeed() { |
| 140 | return delegate.portSpeed(); |
| 141 | } |
| 142 | |
| 143 | } |