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