-
-
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.
Fix rounding in Duration formatting. Stolen from @Rollczi <3
- Loading branch information
Showing
5 changed files
with
192 additions
and
68 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
eternalcode-commons-shared/src/main/java/com/eternalcode/commons/Lazy.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,57 @@ | ||
package com.eternalcode.commons; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public class Lazy<T> implements Supplier<T> { | ||
|
||
private Supplier<T> supplier; | ||
private boolean initialized; | ||
private T value; | ||
private Exception exception; | ||
|
||
public Lazy(T value) { | ||
this.initialized = true; | ||
this.value = value; | ||
} | ||
|
||
public Lazy(Supplier<T> supplier) { | ||
this.supplier = supplier; | ||
} | ||
|
||
public static Lazy<Void> ofRunnable(Runnable runnable) { | ||
return new Lazy<>(() -> { | ||
runnable.run(); | ||
return null; | ||
}); | ||
} | ||
|
||
@Override | ||
public synchronized T get() { | ||
if (exception != null) { | ||
throw new RuntimeException("Lazy value has been already initialized with exception", exception); | ||
} | ||
|
||
if (initialized) { | ||
return value; | ||
} | ||
|
||
this.initialized = true; | ||
|
||
try { | ||
return this.value = supplier.get(); | ||
} | ||
catch (Exception exception) { | ||
this.exception = exception; | ||
throw new RuntimeException("Cannot initialize lazy value", exception); | ||
} | ||
} | ||
|
||
public boolean isInitialized() { | ||
return initialized; | ||
} | ||
|
||
public boolean hasFailed() { | ||
return exception != null; | ||
} | ||
|
||
} |
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
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
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
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