blob: 7485b1e7440c7e6d6306462b1f0878c48613f9fc [file] [log] [blame]
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -08001/*
2 * Copyright 2016 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 */
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().
62 */
63 protected EqualsBuilder toEqualsBuilder(Port that) {
64 if (that == null) {
65 return new EqualsBuilder().appendSuper(false);
66 }
67 return new EqualsBuilder()
68 .append(this.element().id(), that.element().id())
69 .append(this.number(), that.number())
70 .append(this.isEnabled(), that.isEnabled())
71 .append(this.type(), that.type())
72 .append(this.portSpeed(), that.portSpeed())
73 .append(this.annotations(), that.annotations());
74 }
75
76 @Override
77 public boolean equals(Object obj) {
78 if (this == obj) {
79 return true;
80 }
81
82 if (obj != null && getClass() == obj.getClass()) {
83 final ForwardingPort that = (ForwardingPort) obj;
84 return toEqualsBuilder(that)
85 .isEquals();
86 }
87 return false;
88 }
89
90 /**
91 * Returns {@link ToStringHelper} with Port attributes excluding annotations.
92 *
93 * @return {@link ToStringHelper}
94 */
95 protected ToStringHelper toStringHelper() {
96 return MoreObjects.toStringHelper(this)
97 .add("element", element().id())
98 .add("number", number())
99 .add("isEnabled", isEnabled())
100 .add("type", type())
101 .add("portSpeed", portSpeed());
102 }
103
104 @Override
105 public String toString() {
106 return toStringHelper()
107 .toString();
108 }
109
110 @Override
111 public Annotations annotations() {
112 return delegate.annotations();
113 }
114
115 @Override
116 public Element element() {
117 return delegate.element();
118 }
119
120 @Override
121 public PortNumber number() {
122 return delegate.number();
123 }
124
125 @Override
126 public boolean isEnabled() {
127 return delegate.isEnabled();
128 }
129
130 @Override
131 public Port.Type type() {
132 return delegate.type();
133 }
134
135 @Override
136 public long portSpeed() {
137 return delegate.portSpeed();
138 }
139
140}