Replace Preconditions.CheckNotNull per RequireNonNull
[bgpcep.git] / rsvp / spi / src / main / java / org / opendaylight / protocol / rsvp / parser / spi / AbstractRSVPExtensionProviderActivator.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.rsvp.parser.spi;
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.concurrent.GuardedBy;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public abstract class AbstractRSVPExtensionProviderActivator implements AutoCloseable, RSVPExtensionProviderActivator {
20     private static final Logger LOG = LoggerFactory.getLogger(AbstractRSVPExtensionProviderActivator.class);
21
22     @GuardedBy("this")
23     private List<AutoCloseable> registrations;
24
25     @GuardedBy("this")
26     protected abstract List<AutoCloseable> startImpl(RSVPExtensionProviderContext context);
27
28     @Override
29     public final synchronized void start(final RSVPExtensionProviderContext context) {
30         Preconditions.checkState(this.registrations == null);
31
32         this.registrations = requireNonNull(startImpl(context));
33     }
34
35     @Override
36     @SuppressWarnings("checkstyle:IllegalCatch")
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 }