blob: b5e84fe45ae7756f4b2e2294d50ea6e1aa01dc32 [file] [log] [blame]
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -08001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -08003 *
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 Yutaf46dc4f2016-05-13 11:24:13 -070016package org.onosproject.net.utils;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080017
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Objects;
21
22import org.apache.commons.lang3.builder.EqualsBuilder;
23import org.onosproject.net.Annotations;
24import org.onosproject.net.Element;
25import org.onosproject.net.Port;
26import org.onosproject.net.PortNumber;
27
28import com.google.common.annotations.Beta;
29import com.google.common.base.MoreObjects;
30import com.google.common.base.MoreObjects.ToStringHelper;
31
32/**
33 * A Port which forwards all its method calls to another Port.
34 */
35@Beta
36public 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 Milkeybb23e0b2016-08-02 17:00:21 -070062 *
63 * @param that object to compare to
64 * @return builder object
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080065 */
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}