BUG-5280: add AbstractClientConnection
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / InversibleLockException.java
1 /*
2  * Copyright (c) 2016 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.cluster.access.client;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.CountDownLatch;
13
14 /**
15  * Exception thrown from {@link InversibleLock#optimisticRead()} and can be used to wait for the racing write
16  * to complete using {@link #awaitResolution()}.
17  *
18  * @author Robert Varga
19  */
20 @Beta
21 public final class InversibleLockException extends RuntimeException {
22     private static final long serialVersionUID = 1L;
23
24     private final transient CountDownLatch latch;
25
26     InversibleLockException(final CountDownLatch latch) {
27         this.latch = Preconditions.checkNotNull(latch);
28     }
29
30     public void awaitResolution() {
31         // latch can be null after deserialization
32         if (latch != null) {
33             try {
34                 latch.await();
35             } catch (InterruptedException e) {
36                 throw new IllegalStateException("Interrupted while waiting for latch " + latch, e);
37             }
38         }
39     }
40 }