Rework ClusterSingletonServiceGroupImpl locking
[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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.mdsal.singleton.dom.impl.ClusterSingletonServiceGroupImpl.ServiceState;
18
19 @NonNullByDefault
20 final class ServiceInfo {
21     private static final ServiceInfo STARTED = new ServiceInfo(ServiceState.STARTED, null);
22
23     private final @Nullable ListenableFuture<?> future;
24     private final ServiceState state;
25
26     private ServiceInfo(final ServiceState state, final @Nullable ListenableFuture<?> future) {
27         this.state = requireNonNull(state);
28         this.future = future;
29     }
30
31     static ServiceInfo started() {
32         return STARTED;
33     }
34
35     ServiceState getState() {
36         return state;
37     }
38
39     ListenableFuture<?> getFuture() {
40         return verifyNotNull(future);
41     }
42
43     ServiceInfo toState(final ServiceState newState) {
44         verify(state != newState, "Attempted to re-transition into %s", state);
45         return new ServiceInfo(newState, null);
46     }
47
48     ServiceInfo toState(final ServiceState newState, final ListenableFuture<?> newFuture) {
49         verify(state != newState, "Attempted to re-transition into %s", state);
50         return new ServiceInfo(newState, requireNonNull(newFuture));
51     }
52 }