-
-
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.
- Loading branch information
Showing
10 changed files
with
102 additions
and
61 deletions.
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
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
58 changes: 58 additions & 0 deletions
58
common/src/main/java/dev/latvian/mods/rhino/util/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,58 @@ | ||
package dev.latvian.mods.rhino.util; | ||
|
||
import java.util.ServiceLoader; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* @author ZZZank | ||
*/ | ||
public class Lazy<T> implements Supplier<T> { | ||
public static <T> Lazy<T> of(Supplier<T> supplier) { | ||
return new Lazy<>(supplier, -1L); | ||
} | ||
|
||
public static <T> Lazy<T> of(Supplier<T> supplier, long expiresInMs) { | ||
return new Lazy<>(supplier, System.currentTimeMillis() + expiresInMs); | ||
} | ||
|
||
public static <T> Lazy<T> serviceLoader(Class<T> type) { | ||
return of(() -> { | ||
var loaded = ServiceLoader.load(type).iterator(); | ||
if (loaded.hasNext()) { | ||
return loaded.next(); | ||
} | ||
throw new RuntimeException(String.format( | ||
"Could not find platform implementation for %s!", | ||
type.getSimpleName() | ||
)); | ||
}); | ||
} | ||
|
||
private final Supplier<T> factory; | ||
private T value; | ||
private boolean cached; | ||
private final long expires; | ||
|
||
private Lazy(Supplier<T> factory, long expires) { | ||
this.factory = factory; | ||
this.expires = expires; | ||
} | ||
|
||
@Override | ||
public T get() { | ||
if (expires >= 0L && System.currentTimeMillis() > expires) { | ||
cached = false; | ||
} else if (cached) { | ||
return value; | ||
} | ||
|
||
value = factory.get(); | ||
cached = true; | ||
return value; | ||
} | ||
|
||
public void forget() { | ||
value = null; | ||
cached = false; | ||
} | ||
} |
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