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