-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created [structures](src/main/java/it/fulminazzo/fulmicollection/stru…
…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
1 parent
bfbf433
commit 7113249
Showing
3 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ plugins { | |
} | ||
|
||
group = 'it.fulminazzo' | ||
version = '1.5' | ||
version = '1.5.1' | ||
|
||
repositories { | ||
mavenCentral() | ||
|
42 changes: 42 additions & 0 deletions
42
src/main/java/it/fulminazzo/fulmicollection/utils/ThreadUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
src/test/java/it/fulminazzo/fulmicollection/utils/ThreadUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |