BUG-5280: add AbstractClientConnection
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / InversibleLockException.java
diff --git a/opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/InversibleLockException.java b/opendaylight/md-sal/cds-access-client/src/main/java/org/opendaylight/controller/cluster/access/client/InversibleLockException.java
new file mode 100644 (file)
index 0000000..7ec18a2
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.access.client;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Preconditions;
+import java.util.concurrent.CountDownLatch;
+
+/**
+ * Exception thrown from {@link InversibleLock#optimisticRead()} and can be used to wait for the racing write
+ * to complete using {@link #awaitResolution()}.
+ *
+ * @author Robert Varga
+ */
+@Beta
+public final class InversibleLockException extends RuntimeException {
+    private static final long serialVersionUID = 1L;
+
+    private final transient CountDownLatch latch;
+
+    InversibleLockException(final CountDownLatch latch) {
+        this.latch = Preconditions.checkNotNull(latch);
+    }
+
+    public void awaitResolution() {
+        // latch can be null after deserialization
+        if (latch != null) {
+            try {
+                latch.await();
+            } catch (InterruptedException e) {
+                throw new IllegalStateException("Interrupted while waiting for latch " + latch, e);
+            }
+        }
+    }
+}