Bug 8568: Remove deprecated MountProviderService APIs
[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 com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.yangtools.yang.binding.Notification;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 class NotifyTask implements Runnable {
17     private static final Logger LOG = LoggerFactory.getLogger(NotifyTask.class);
18
19     private final NotificationListenerRegistration<?> registration;
20     private final Notification notification;
21
22     public NotifyTask(final NotificationListenerRegistration<?> registration, final Notification notification) {
23         this.registration = Preconditions.checkNotNull(registration);
24         this.notification = Preconditions.checkNotNull(notification);
25     }
26
27     @SuppressWarnings("unchecked")
28     private <T extends Notification> NotificationListenerRegistration<T> getRegistration() {
29         return (NotificationListenerRegistration<T>)registration;
30     }
31
32     @Override
33     public void run() {
34         if (LOG.isDebugEnabled()) {
35             LOG.debug("Delivering notification {} to {}", notification, registration.getInstance());
36         } else {
37             LOG.trace("Delivering notification {} to {}", notification.getClass().getName(), registration.getInstance());
38         }
39
40         try {
41             getRegistration().notify(notification);
42         } catch (final Exception e) {
43             LOG.error("Unhandled exception thrown by listener: {}", registration.getInstance(), e);
44         }
45
46         if (LOG.isDebugEnabled()) {
47             LOG.debug("Notification delivered {} to {}", notification, registration.getInstance());
48         } else {
49             LOG.trace("Notification delivered {} to {}", notification.getClass().getName(), registration.getInstance());
50         }
51     }
52
53     @Override
54     public int hashCode() {
55         final int prime = 31;
56         int result = 1;
57         result = prime * result + ((registration== null) ? 0 : registration.hashCode());
58         result = prime * result + ((notification== null) ? 0 : notification.hashCode());
59         return result;
60     }
61
62     @Override
63     public boolean equals(final Object obj) {
64         if (this == obj) {
65             return true;
66         }
67         if (obj == null) {
68             return false;
69         }
70         if (getClass() != obj.getClass()) {
71             return false;
72         }
73         final NotifyTask other = (NotifyTask) obj;
74         if (registration == null) {
75             if (other.registration != null) {
76                 return false;
77             }
78         } else if (!registration.equals(other.registration)) {
79             return false;
80         }
81         if (notification == null) {
82             if (other.notification != null) {
83                 return false;
84             }
85         } else if (!notification.equals(other.notification)) {
86             return false;
87         }
88         return true;
89     }
90
91     @Override
92     public String toString() {
93         return MoreObjects.toStringHelper(this)
94                 .add("listener", registration)
95                 .add("notification", notification.getClass())
96                 .toString();
97     }
98 }