8762562ad15fec88171d962118ca3a1c3d0a76f2
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / impl / RuntimeGeneratedInvoker.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.sal.binding.codegen.impl;
9
10 import java.util.Set;
11
12 import org.eclipse.xtext.xbase.lib.util.ToStringHelper;
13 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper;
14 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory.NotificationInvoker;
15 import org.opendaylight.yangtools.yang.binding.Notification;
16 import org.opendaylight.yangtools.yang.binding.NotificationListener;
17
18 import com.google.common.base.Preconditions;
19
20 final class RuntimeGeneratedInvoker implements NotificationInvoker {
21     private final org.opendaylight.controller.sal.binding.api.NotificationListener<Notification> invocationProxy;
22     private final RuntimeGeneratedInvokerPrototype prototype;
23     private final NotificationListener delegate;
24
25     @SuppressWarnings("unchecked")
26     private RuntimeGeneratedInvoker(final NotificationListener delegate, final RuntimeGeneratedInvokerPrototype prototype, final org.opendaylight.controller.sal.binding.api.NotificationListener<?> proxy) {
27         this.invocationProxy = (org.opendaylight.controller.sal.binding.api.NotificationListener<Notification>) proxy;
28         this.delegate = Preconditions.checkNotNull(delegate);
29         this.prototype = prototype;
30     }
31
32     public static RuntimeGeneratedInvoker create(final NotificationListener delegate, final RuntimeGeneratedInvokerPrototype prototype) throws InstantiationException, IllegalAccessException {
33         final org.opendaylight.controller.sal.binding.api.NotificationListener<?> proxy = Preconditions.checkNotNull(prototype.getProtoClass().newInstance());
34         RuntimeCodeHelper.setDelegate(proxy, delegate);
35         return new RuntimeGeneratedInvoker(delegate, prototype, proxy);
36     }
37
38     @Override
39     public NotificationListener getDelegate() {
40         return delegate;
41     }
42
43     @Override
44     public org.opendaylight.controller.sal.binding.api.NotificationListener<Notification> getInvocationProxy() {
45         return invocationProxy;
46     }
47
48     @Override
49     public Set<Class<? extends Notification>> getSupportedNotifications() {
50         return prototype.getSupportedNotifications();
51     }
52
53     @Override
54     public void close() {
55         // Nothing to do
56     }
57
58     @Override
59     public int hashCode() {
60         final int prime = 31;
61         int result = 1;
62         result = prime * result + delegate.hashCode();
63         result = prime * result + invocationProxy.hashCode();
64         result = prime * result + prototype.hashCode();
65         return result;
66     }
67
68     @Override
69     public boolean equals(final Object obj) {
70         if (this == obj) {
71             return true;
72         }
73         if (!(obj instanceof RuntimeGeneratedInvoker)) {
74             return false;
75         }
76         final RuntimeGeneratedInvoker other = (RuntimeGeneratedInvoker) obj;
77         if (!delegate.equals(other.delegate)) {
78             return false;
79         }
80         if (!invocationProxy.equals(other.invocationProxy)) {
81             return false;
82         }
83         return prototype.equals(other.prototype);
84     }
85
86     @Override
87     public String toString() {
88         String result = new ToStringHelper().toString(this);
89         return result;
90     }
91 }