Skip to content

Commit

Permalink
- r remove duplication
Browse files Browse the repository at this point in the history
Co-authored-by: Jay Bazuzi <jay@bazuzi.com>
Co-authored-by: Llewellyn Falco <llewellyn.falco@gmail.com>
  • Loading branch information
3 people committed Feb 17, 2025
1 parent d6f917e commit 0765812
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions approvaltests-util/src/main/java/org/lambda/utils/Once.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.lambda.utils;

import com.spun.util.ThreadUtils;
import org.lambda.actions.Action0;
import org.lambda.functions.Function0;

Expand All @@ -15,22 +16,22 @@ public class Once
private static final Map<Class, Object> functions = Collections.synchronizedMap(new HashMap<>());
public static void run(Action0 runnable)
{
if (!actions.contains(runnable.getClass()))
run(runnable, runnable.getClass());
}
public static void runAsync(Action0 runnable)
{
run(() -> ThreadUtils.launch(runnable), runnable.getClass());
}
private static void run(Action0 runnable, Class<? extends Action0> identifier)
{
if (!actions.contains(identifier))
{
actions.add(runnable.getClass());
actions.add(identifier);
runnable.call();
}
}
public static <T> T run(Function0<T> runnable)
{
return (T) functions.computeIfAbsent(runnable.getClass(), k -> runnable.call());
}
public static void runAsync(Action0 runnable)
{
if (!actions.contains(runnable.getClass()))
{
actions.add(runnable.getClass());
new Thread(runnable::call).start();
}
}
}

0 comments on commit 0765812

Please sign in to comment.