Replace Preconditions.CheckNotNull per RequireNonNull
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / pojo / AbstractPCEPExtensionProviderActivator.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.pcep.spi.pojo;
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.opendaylight.protocol.pcep.spi.PCEPExtensionProviderActivator;
16 import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public abstract class AbstractPCEPExtensionProviderActivator implements AutoCloseable, PCEPExtensionProviderActivator {
21     private static final Logger LOG = LoggerFactory.getLogger(AbstractPCEPExtensionProviderActivator.class);
22
23     @GuardedBy("this")
24     private List<AutoCloseable> registrations;
25
26     @GuardedBy("this")
27     protected abstract List<AutoCloseable> startImpl(PCEPExtensionProviderContext context);
28
29     @Override
30     public final synchronized void start(final PCEPExtensionProviderContext context) {
31         Preconditions.checkState(this.registrations == null);
32
33         this.registrations = requireNonNull(startImpl(context));
34     }
35
36     @Override
37     public final synchronized void stop() {
38         if (this.registrations == null) {
39             return;
40         }
41
42         for (final AutoCloseable r : this.registrations) {
43             try {
44                 r.close();
45             } catch (final Exception e) {
46                 LOG.warn("Failed to close registration", e);
47             }
48         }
49
50         this.registrations = null;
51     }
52
53     @Override
54     public final void close() {
55         stop();
56     }
57 }