Replace Preconditions.CheckNotNull per RequireNonNull
[bgpcep.git] / bgp / rib-spi / src / main / java / org / opendaylight / protocol / bgp / rib / spi / AbstractRIBExtensionProviderActivator.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.protocol.bgp.rib.spi;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import java.util.List;
14 import javax.annotation.concurrent.GuardedBy;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public abstract class AbstractRIBExtensionProviderActivator implements AutoCloseable, RIBExtensionProviderActivator {
19     private static final Logger LOG = LoggerFactory.getLogger(AbstractRIBExtensionProviderActivator.class);
20
21     @GuardedBy("this")
22     private List<AutoCloseable> registrations;
23
24     @GuardedBy("this")
25     protected abstract List<AutoCloseable> startRIBExtensionProviderImpl(RIBExtensionProviderContext context);
26
27     @Override
28     public final synchronized void startRIBExtensionProvider(final RIBExtensionProviderContext context) {
29         Preconditions.checkState(this.registrations == null);
30
31         this.registrations = requireNonNull(startRIBExtensionProviderImpl(context));
32     }
33
34     @Override
35     public final synchronized void stopRIBExtensionProvider() {
36         if (this.registrations == null) {
37             return;
38         }
39
40         for (final AutoCloseable r : this.registrations) {
41             try {
42                 r.close();
43             } catch (final Exception e) {
44                 LOG.warn("Failed to close registration", e);
45             }
46         }
47
48         this.registrations = null;
49     }
50
51     @Override
52     public final void close() {
53         stopRIBExtensionProvider();
54     }
55 }