Enable spotbugs in singleton-service
[mdsal.git] / singleton-service / mdsal-singleton-dom-impl / src / main / java / org / opendaylight / mdsal / singleton / dom / impl / ServiceInfo.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.singleton.dom.impl;
9
10 import static com.google.common.base.Verify.verify;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
13
14 import com.google.common.util.concurrent.ListenableFuture;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.mdsal.singleton.dom.impl.ClusterSingletonServiceGroupImpl.ServiceState;
19
20 @NonNullByDefault
21 @SuppressFBWarnings(value = "NP_NULL_PARAM_DEREF_NONVIRTUAL", justification = "SpotBugs does not grok @Nullable field")
22 final class ServiceInfo {
23     private static final ServiceInfo STARTED = new ServiceInfo(ServiceState.STARTED, null);
24
25     private final @Nullable ListenableFuture<?> future;
26     private final ServiceState state;
27
28     private ServiceInfo(final ServiceState state, final @Nullable ListenableFuture<?> future) {
29         this.state = requireNonNull(state);
30         this.future = future;
31     }
32
33     static ServiceInfo started() {
34         return STARTED;
35     }
36
37     ServiceState getState() {
38         return state;
39     }
40
41     ListenableFuture<?> getFuture() {
42         return verifyNotNull(future);
43     }
44
45     ServiceInfo toState(final ServiceState newState) {
46         verify(state != newState, "Attempted to re-transition into %s", state);
47         return new ServiceInfo(newState, null);
48     }
49
50     ServiceInfo toState(final ServiceState newState, final ListenableFuture<?> newFuture) {
51         verify(state != newState, "Attempted to re-transition into %s", state);
52         return new ServiceInfo(newState, requireNonNull(newFuture));
53     }
54 }