CDS: Add stress test RPC to the cars model
[controller.git] / opendaylight / netconf / sal-netconf-connector / src / main / java / org / opendaylight / controller / 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.controller.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 import javax.annotation.concurrent.GuardedBy;
14
15 final class UncancellableFuture<V> extends AbstractFuture<V> {
16     @GuardedBy("this")
17     private boolean uncancellable = false;
18
19     public UncancellableFuture(final boolean uncancellable) {
20         this.uncancellable = uncancellable;
21     }
22
23     public synchronized boolean setUncancellable() {
24         if (isCancelled()) {
25             return false;
26         }
27
28         uncancellable = true;
29         return true;
30     }
31
32     public synchronized boolean isUncancellable() {
33         return uncancellable;
34     }
35
36     @Override
37     public synchronized boolean cancel(final boolean mayInterruptIfRunning) {
38         return uncancellable ? false : super.cancel(mayInterruptIfRunning);
39     }
40
41     @Override
42     public synchronized boolean set(@Nullable final V value) {
43         Preconditions.checkState(uncancellable);
44         return super.set(value);
45     }
46
47     @Override
48     protected boolean setException(final Throwable throwable) {
49         Preconditions.checkState(uncancellable);
50         return super.setException(throwable);
51     }
52 }