Fix findbugs violations in netconf
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / listener / UncancellableFuture.java
1 /*
2  * Copyright (c) 2014 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.netconf.sal.connect.netconf.listener;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.AbstractFuture;
12 import javax.annotation.Nullable;
13
14 final class UncancellableFuture<V> extends AbstractFuture<V> {
15     private volatile boolean uncancellable = false;
16
17     UncancellableFuture(final boolean uncancellable) {
18         this.uncancellable = uncancellable;
19     }
20
21     public boolean setUncancellable() {
22         if (isCancelled()) {
23             return false;
24         }
25
26         uncancellable = true;
27         return true;
28     }
29
30     public boolean isUncancellable() {
31         return uncancellable;
32     }
33
34     @Override
35     public boolean cancel(final boolean mayInterruptIfRunning) {
36         return !uncancellable && super.cancel(mayInterruptIfRunning);
37     }
38
39     @Override
40     public boolean set(@Nullable final V value) {
41         Preconditions.checkState(uncancellable);
42         return super.set(value);
43     }
44
45     @Override
46     protected boolean setException(final Throwable throwable) {
47         Preconditions.checkState(uncancellable);
48         return super.setException(throwable);
49     }
50 }