Merge "Add missing copyright text"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / compat / NotifyTask.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.md.sal.binding.compat;
9
10 import org.opendaylight.yangtools.yang.binding.Notification;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import com.google.common.base.Objects;
15 import com.google.common.base.Preconditions;
16
17 class NotifyTask implements Runnable {
18     private static final Logger LOG = LoggerFactory.getLogger(NotifyTask.class);
19
20     private final NotificationListenerRegistration<?> registration;
21     private final Notification notification;
22
23     public NotifyTask(final NotificationListenerRegistration<?> registration, final Notification notification) {
24         this.registration = Preconditions.checkNotNull(registration);
25         this.notification = Preconditions.checkNotNull(notification);
26     }
27
28     @SuppressWarnings("unchecked")
29     private <T extends Notification> NotificationListenerRegistration<T> getRegistration() {
30         return (NotificationListenerRegistration<T>)registration;
31     }
32
33     @Override
34     public void run() {
35         if (LOG.isDebugEnabled()) {
36             LOG.debug("Delivering notification {} to {}", notification, registration.getInstance());
37         } else {
38             LOG.trace("Delivering notification {} to {}", notification.getClass().getName(), registration.getInstance());
39         }
40
41         try {
42             getRegistration().notify(notification);
43         } catch (final Exception e) {
44             LOG.error("Unhandled exception thrown by listener: {}", registration.getInstance(), e);
45         }
46
47         if (LOG.isDebugEnabled()) {
48             LOG.debug("Notification delivered {} to {}", notification, registration.getInstance());
49         } else {
50             LOG.trace("Notification delivered {} to {}", notification.getClass().getName(), registration.getInstance());
51         }
52     }
53
54     @Override
55     public int hashCode() {
56         final int prime = 31;
57         int result = 1;
58         result = prime * result + ((registration== null) ? 0 : registration.hashCode());
59         result = prime * result + ((notification== null) ? 0 : notification.hashCode());
60         return result;
61     }
62
63     @Override
64     public boolean equals(final Object obj) {
65         if (this == obj)
66             return true;
67         if (obj == null)
68             return false;
69         if (getClass() != obj.getClass())
70             return false;
71         NotifyTask other = (NotifyTask) obj;
72         if (registration == null) {
73             if (other.registration != null)
74                 return false;
75         } else if (!registration.equals(other.registration))
76             return false;
77         if (notification == null) {
78             if (other.notification != null)
79                 return false;
80         } else if (!notification.equals(other.notification))
81             return false;
82         return true;
83     }
84
85     @Override
86     public String toString() {
87         return Objects.toStringHelper(this)
88                 .add("listener", registration)
89                 .add("notification", notification.getClass())
90                 .toString();
91     }
92 }