2 * Copyright (c) 2013 Big Switch Networks, Inc.
4 * Licensed under the Eclipse Public License, Version 1.0 (the
5 * "License"); you may not use this file except in compliance with the
6 * License. You may obtain a copy of the License at
8 * http://www.eclipse.org/legal/epl-v10.html
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
13 * implied. See the License for the specific language governing
14 * permissions and limitations under the License.
17 package org.opendaylight.controller.hosttracker.internal;
19 import java.util.EnumSet;
21 import org.opendaylight.controller.hosttracker.Entity;
22 import org.opendaylight.controller.hosttracker.IDeviceService;
23 import org.opendaylight.controller.hosttracker.IDeviceService.DeviceField;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * This is a thin wrapper around {@link Entity} that allows overriding the
29 * behavior of {@link Object#hashCode()} and {@link Object#equals(Object)} so
30 * that the keying behavior in a hash map can be changed dynamically
34 public class IndexedEntity {
35 protected EnumSet<DeviceField> keyFields;
36 protected Entity entity;
37 private int hashCode = 0;
38 protected static Logger logger = LoggerFactory
39 .getLogger(IndexedEntity.class);
42 * Create a new {@link IndexedEntity} for the given {@link Entity} using the
43 * provided key fields.
46 * The key fields that will be used for computing
47 * {@link IndexedEntity#hashCode()} and
48 * {@link IndexedEntity#equals(Object)}
52 public IndexedEntity(EnumSet<DeviceField> keyFields, Entity entity) {
54 this.keyFields = keyFields;
59 * Check whether this entity has non-null values in any of its key fields
61 * @return true if any key fields have a non-null value
63 public boolean hasNonNullKeys() {
64 for (DeviceField f : keyFields) {
69 if (entity.getIpv4Address() != null)
73 if (entity.getPort() != null)
77 if (entity.getVlan() != null)
86 public int hashCode() {
94 for (DeviceField f : keyFields) {
99 + (int) (entity.getMacAddress() ^ (entity
100 .getMacAddress() >>> 32));
105 + ((entity.getIpv4Address() == null) ? 0 : entity
106 .getIpv4Address().hashCode());
111 + ((entity.getPort() == null) ? 0 : entity.getPort()
117 + ((entity.getVlan() == null) ? 0 : entity.getVlan()
126 public boolean equals(Object obj) {
131 if (getClass() != obj.getClass())
133 IndexedEntity other = (IndexedEntity) obj;
135 if (!keyFields.equals(other.keyFields))
138 for (IDeviceService.DeviceField f : keyFields) {
141 if (entity.getMacAddress() != other.entity.getMacAddress())
145 if (entity.getIpv4Address() == null) {
146 if (other.entity.getIpv4Address() != null)
148 } else if (!entity.getIpv4Address().equals(
149 other.entity.getIpv4Address()))
153 if (entity.getPort() == null) {
154 if (other.entity.getPort() != null)
156 } else if (!entity.getPort().equals(other.entity.getPort()))
160 if (entity.getVlan() == null) {
161 if (other.entity.getVlan() != null)
163 } else if (!entity.getVlan().equals(other.entity.getVlan()))