Skip to content

Commit

Permalink
- r Refactor to use Once
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottBob committed Jan 30, 2025
1 parent 06870d1 commit 404823a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,42 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

public class OnceTest {
public static int count = 0;

@Test
public void testOnce() {
increment();
increment();
increment();
increment();
increment();
assertEquals(1, count);
}

private void increment() {
var fieldThatForcesLambdaToHaveMultipleInstances = NumberUtils.doRandomPercentage(50);
Once.run(() -> {
if (fieldThatForcesLambdaToHaveMultipleInstances)
OnceTest.count++;
else
OnceTest.count++;
});
}

@Test
public void testFunctionCallOnce() {
int count = 0;
count = increment(count);
count = increment(count);
count = increment(count);
count = increment(count);
count = increment(count);
assertEquals(1, count);
}

private int increment(int count) {
return Once.run(() -> count + 1);
}

public class OnceTest
{
public static int count = 0;
@Test
public void testOnce()
{
increment();
increment();
increment();
increment();
increment();
assertEquals(1, count);
}
private void increment()
{
var fieldThatForcesLambdaToHaveMultipleInstances = NumberUtils.doRandomPercentage(50);
Once.run(() -> {
if (fieldThatForcesLambdaToHaveMultipleInstances)
OnceTest.count++;
else
OnceTest.count++;
});
}
@Test
public void testFunctionCallOnce()
{
int count = 0;
count = increment(count);
count = increment(count);
count = increment(count);
count = increment(count);
count = increment(count);
assertEquals(1, count);
}
private int increment(int count)
{
return Once.run(() -> count + 1);
}
}
26 changes: 15 additions & 11 deletions approvaltests-util/src/main/java/org/lambda/utils/Once.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@

import java.util.*;

public class Once {
private static final Set<Class> actions = Collections.synchronizedSet(new HashSet<>());
private static final Map<Class, Object> functions = Collections.synchronizedMap(new HashMap<>());
public static void run(Action0 runnable) {
if (!actions.contains(runnable.getClass())) {
actions.add(runnable.getClass());
runnable.call();
}
}
public static <T> T run(Function0<T> runnable) {
return (T) functions.computeIfAbsent(runnable.getClass(), k -> runnable.call());
public class Once
{
private static final Set<Class> actions = Collections.synchronizedSet(new HashSet<>());
private static final Map<Class, Object> functions = Collections.synchronizedMap(new HashMap<>());
public static void run(Action0 runnable)
{
if (!actions.contains(runnable.getClass()))
{
actions.add(runnable.getClass());
runnable.call();
}
}
public static <T> T run(Function0<T> runnable)
{
return (T) functions.computeIfAbsent(runnable.getClass(), k -> runnable.call());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.approvaltests.internal.logs;

import com.spun.util.io.FileUtils;
import org.lambda.utils.Once;

import java.io.File;

Expand All @@ -9,6 +10,7 @@ public class ApprovedFileLog
static
{
FileUtils.writeFile(get(), "");
Once.run(() -> LoggingUtils.downloadScriptIfMissing("detect_and_remove_abandoned"));
}
public static File get()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
package org.approvaltests.internal.logs;

import com.spun.util.io.FileUtils;
import org.lambda.utils.Once;

import java.io.File;

import static org.approvaltests.internal.logs.LoggingUtils.APPROVAL_TEMP_DIRECTORY;

public class FailedFileLog
{
private static boolean downloadedScriptCheck = false;
static
{
FileUtils.writeFile(get(), "");
}
private static void downloadApproveAllScriptIfMissing()
{
if (downloadedScriptCheck)
{ return; }
downloadedScriptCheck = true;
String scriptName = "approve_all";
LoggingUtils.downloadScriptIfMissing(scriptName);
Once.run(() -> LoggingUtils.downloadScriptIfMissing("approve_all"));
}
public static File get()
{
Expand Down

0 comments on commit 404823a

Please sign in to comment.