Remove unneeded AutoCloseable interfaces
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / BindingDOMNotificationServiceAdapter.java
1 /*
2  * Copyright (c) 2015 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.mdsal.binding.dom.adapter;
9
10 import com.google.common.collect.ClassToInstanceMap;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Set;
13 import org.opendaylight.mdsal.binding.api.NotificationService;
14 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory;
15 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
16 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
17 import org.opendaylight.mdsal.dom.api.DOMService;
18 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.binding.NotificationListener;
21
22 public class BindingDOMNotificationServiceAdapter implements NotificationService {
23
24     public static final Factory<NotificationService> BUILDER_FACTORY = Builder::new;
25
26     private final BindingNormalizedNodeSerializer codec;
27     private final DOMNotificationService domNotifService;
28
29     public BindingDOMNotificationServiceAdapter(final BindingNormalizedNodeSerializer codec,
30             final DOMNotificationService domNotifService) {
31         this.codec = codec;
32         this.domNotifService = domNotifService;
33     }
34
35     @Override
36     public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener) {
37         final BindingDOMNotificationListenerAdapter domListener
38                 = new BindingDOMNotificationListenerAdapter(codec, listener);
39         final ListenerRegistration<BindingDOMNotificationListenerAdapter> domRegistration =
40                 domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
41         return new ListenerRegistrationImpl<>(listener, domRegistration);
42     }
43
44     private static class ListenerRegistrationImpl<T extends NotificationListener>
45             extends AbstractListenerRegistration<T> {
46         private final ListenerRegistration<?> listenerRegistration;
47
48         ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
49             super(listener);
50             this.listenerRegistration = listenerRegistration;
51         }
52
53         @Override
54         protected void removeRegistration() {
55             listenerRegistration.close();
56         }
57     }
58
59     private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
60
61         @Override
62         protected NotificationService createInstance(final BindingToNormalizedNodeCodec codec,
63                 final ClassToInstanceMap<DOMService> delegates) {
64             final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
65             return new BindingDOMNotificationServiceAdapter(codec.getCodecRegistry(), domNotification);
66         }
67
68         @Override
69         public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
70             return ImmutableSet.of(DOMNotificationService.class);
71         }
72     }
73
74     public DOMNotificationService getDomService() {
75         return domNotifService;
76     }
77 }