summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractBlockingOperation.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractBlockingOperation.java')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractBlockingOperation.java58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractBlockingOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractBlockingOperation.java
new file mode 100644
index 0000000..de672cd
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractBlockingOperation.java
@@ -0,0 +1,58 @@
+package com.keuin.kbackupfabric.operation.abstracts;
+
+import com.keuin.kbackupfabric.operation.abstracts.i.Blocking;
+
+public abstract class AbstractBlockingOperation extends AbstractSerializedOperation implements Blocking {
+
+ private static final Object sync = new Object();
+ private static boolean isBlocking = false;
+ private boolean wasBlocked = false;
+ private boolean noUnblocking = false;
+
+ @Override
+ protected final boolean operate() {
+ synchronized (sync) {
+ if (isBlocking) {
+ System.out.println("blocked.");
+ wasBlocked = true;
+ return false;
+ } else {
+ System.out.println("not blocked.");
+ wasBlocked = false;
+ isBlocking = true;
+ }
+ }
+ boolean exitCode = blockingContext();
+ if (!noUnblocking)
+ isBlocking = false;
+ return exitCode;
+ }
+
+ public final boolean isBlocked() {
+ return wasBlocked;
+ }
+
+ protected final void block(boolean blockState) {
+ isBlocking = blockState;
+ }
+
+ protected void noUnblocking(boolean b) {
+ noUnblocking = b;
+ }
+
+ protected boolean blockAndGet() {
+ synchronized (sync) {
+ boolean b = isBlocking;
+ isBlocking = true;
+ return b;
+ }
+ }
+
+ /**
+ * Implement your blocked operation here.
+ *
+ * @return the stat code.
+ */
+ protected abstract boolean blockingContext();
+
+}