Added CompletableFuture Support and fixed a small bug in a test

This commit is contained in:
2026-08-10 19:38:35 +02:00
parent 180b603ffd
commit d140aaa788
3 changed files with 1110 additions and 1087 deletions
@@ -1,6 +1,7 @@
package speiger.src.collections.PACKAGE.utils;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
@@ -520,6 +521,16 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE
return toRun.GET_KEY(timeout, unit);
}
/**
* Starts the execution of the task and returns a future so it can be used with a future.
* @return the task as a future
*/
public CompletableFuture<CLASS_TYPE> toFuture() {
BASE_TASK KEY_GENERIC_TYPE toRun = task;
task = null;
return toRun.toFuture();
}
#if !TYPE_OBJECT
private static class ReduceTask extends BASE_TASK
{
@@ -881,6 +892,7 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE
private static final int CANCELLED = 7;
private volatile WaitNode waiter;
private volatile int state = CREATED;
private volatile boolean locked = false;
Consumer<TASK KEY_GENERIC_TYPE> callback;
Executor executor = SanityChecks::invokeAsyncTask;
KEY_TYPE result;
@@ -950,6 +962,17 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE
onCompletion();
}
CompletableFuture<CLASS_TYPE> toFuture() {
locked = true;
Executor exe = this.executor;
this.executor = Runnable::run;
return CompletableFuture.supplyAsync(() -> {
begin();
try { return GET_KEY(); }
catch(Exception e) { throw new RuntimeException(e); }
}, exe);
}
@Override
public boolean cancel(boolean cancelIfRunnning) {
if(state == RUNNING && !cancelIfRunnning) return false;
@@ -1044,12 +1067,14 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE
@Override
public void pause() {
if(locked) throw new IllegalStateException("Completable Future Wrap doesn't support pause");
if(state == PAUSED || state == PAUSING || state >= FINISHING) return;
state = PAUSING;
}
@Override
public void awaitPausing() {
if(locked) throw new IllegalStateException("Completable Future Wrap doesn't support pause");
if(state == PAUSED) return;
pause();
if(state == PAUSING) {
@@ -1061,6 +1086,7 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE
@Override
public void resume() {
if(locked) throw new IllegalStateException("Completable Future Wrap doesn't support pause");
if(state != PAUSED && state != PAUSING) return;
if(state == PAUSING) {
while(state == PAUSING) {
@@ -153,9 +153,6 @@ public class COLLECTIONS
/**
* Internal Use mainly. Its a collection wrapper with 0 dependencies for those actions where a collector is needed.
*/
#if !TYPE_OBJECT
@SuppressWarnings("exports")
#endif
public static GENERIC_KEY_BRACES CollectionWrapper KEY_GENERIC_TYPE wrapper() {
return new CollectionWrapperBRACES();
}
@@ -29,7 +29,7 @@ public abstract class BaseInt2IntMapTest
if(!getValidMapTests().contains(MapTests.PUT)) return;
Int2IntMap putMap = createMap(PUT_ARRAY, PUT_VALUE_ARRAY);
Assert.assertEquals(PUT_ARRAY.length, putMap.size());
Assert.assertEquals(0, putMap.put(0, 512));
Assert.assertEquals(putMap.getDefaultReturnValue(), putMap.put(0, 512));
Assert.assertEquals(1, putMap.put(513, 2));
Assert.assertEquals(PUT_ARRAY.length + 1, putMap.size());
Assert.assertEquals(512, putMap.addTo(0, 1));