From 7444c2b1f281b5b147717ba2a2ed6798c66a057b Mon Sep 17 00:00:00 2001 From: Keuin Date: Mon, 27 Apr 2020 17:42:07 +0800 Subject: Resized the logo to reduce plugin size. Refactored code for a better implementation of async and blocking task. --- .../abstracts/InvokableAsyncBlockingOperation.java | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableAsyncBlockingOperation.java (limited to 'src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableAsyncBlockingOperation.java') diff --git a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableAsyncBlockingOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableAsyncBlockingOperation.java new file mode 100644 index 0000000..6268d5a --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableAsyncBlockingOperation.java @@ -0,0 +1,66 @@ +package com.keuin.kbackupfabric.operation.abstracts; + +import com.keuin.kbackupfabric.operation.abstracts.i.Blocking; +import com.keuin.kbackupfabric.operation.abstracts.i.Invokable; + +public abstract class InvokableAsyncBlockingOperation implements Invokable, Blocking { + + private final InvokableAsyncOperation asyncOperation; + private final HackedBlockingOperation blockingOperation; + + public InvokableAsyncBlockingOperation(String name) { + asyncOperation = new InvokableAsyncOperation(name) { + @Override + protected void async() { + InvokableAsyncBlockingOperation.this.async(); + // When the async operation finishes, unblock + blockingOperation.noUnblocking(false); + blockingOperation.block(false); + } + + @Override + protected boolean sync() { + return InvokableAsyncBlockingOperation.this.sync(); + } + }; + + blockingOperation = new HackedBlockingOperation(); + } + + @Override + public boolean invoke() { + return blockingOperation.invoke(); + } + + @Override + public boolean isBlocked() { + return blockingOperation.isBlocked(); + } + + protected abstract void async(); + + protected boolean sync() { + return true; + } + + private class HackedBlockingOperation extends InvokableBlockingOperation { + + @Override + protected boolean blockingContext() { + + noUnblocking(true); + return asyncOperation.invoke(); + } + + @Override + public void noUnblocking(boolean b) { + super.noUnblocking(b); + } + + @Override + public void block(boolean blockState) { + super.block(blockState); + } + } + +} -- cgit v1.2.3