Skip to content

Commit

Permalink
Created [structures](src/main/java/it/fulminazzo/fulmicollection/stru…
Browse files Browse the repository at this point in the history
…ctures) package.

Added [Tuple](src/main/java/it/fulminazzo/fulmicollection/structures/Tuple.java) type.
Added [Triple](src/main/java/it/fulminazzo/fulmicollection/structures/Triple.java) type.
Added `ReflectionUtils#setAccessible(Object)` function.
Added `ReflectionUtils#get(Object, Field)` function.
Added [FieldEquable](src/main/java/it/fulminazzo/fulmicollection/objects/FieldEquable.java) type.
Added [CacheMap](src/main/java/it/fulminazzo/fulmicollection/structures/CacheMap.java) to temporarily store data in maps.
Added [ThreadUtils](src/main/java/it/fulminazzo/fulmicollection/utils/ThreadUtils.java) class with `sleepAndThen` methods.
Optimized `FieldEquable#equals` cycles.
Fixed enums not being displayed in `Printable#convertToJson`.
Fixed primitive parameters in constructors not being detected.
Fixed `FieldEquable#equals` method.
  • Loading branch information
Fulminazzo committed Apr 9, 2024
1 parent bfbf433 commit 7113249
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'it.fulminazzo'
version = '1.5'
version = '1.5.1'

repositories {
mavenCentral()
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/it/fulminazzo/fulmicollection/utils/ThreadUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package it.fulminazzo.fulmicollection.utils;

import org.jetbrains.annotations.Nullable;

/**
* An utils class to interact with {@link Thread}s.
*/
public class ThreadUtils {
private static final String SLEEP_THEN_NAME = "Sleep-Then-%s";
private static long sleepAndThenCounter = 0;

/**
* Waits for the specified milliseconds, then executes the given action.
*
* @param milliseconds the milliseconds
* @param action the action
*/
protected static void sleepAndThen(long milliseconds, final @Nullable Runnable action) {
sleepAndThen(milliseconds, action, null);
}

/**
* Waits for the specified milliseconds, then executes the given action.
*
* @param milliseconds the milliseconds
* @param action the action
* @param onInterrupt the action to run in case of an {@link InterruptedException}
*/
protected static void sleepAndThen(long milliseconds, final @Nullable Runnable action,
final @Nullable Runnable onInterrupt) {
new Thread(() -> {
try {
Thread.sleep(milliseconds);
} catch (InterruptedException e) {
if (onInterrupt != null) onInterrupt.run();
return;
}
if (action != null) action.run();
}, String.format(SLEEP_THEN_NAME, sleepAndThenCounter++)).start();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package it.fulminazzo.fulmicollection.utils;

import org.junit.jupiter.api.Test;

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

class ThreadUtilsTest {

@Test
void testNullAction() {
assertDoesNotThrow(() -> ThreadUtils.sleepAndThen(50, null));
}

@Test
void testInterruptedException() {
ThreadUtils.sleepAndThen(2000, null);
Thread thread = Thread.getAllStackTraces().keySet().stream()
.filter(t -> t.getState().equals(Thread.State.TIMED_WAITING))
.filter(t -> t.getName().startsWith("Sleep-Then"))
.findFirst().orElse(null);

assertNotNull(thread, "Could not find sleeping thread");
assertDoesNotThrow(thread::interrupt);
}

}

0 comments on commit 7113249

Please sign in to comment.