Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into experimental-rewrite
- Loading branch information
Showing
120 changed files
with
3,114 additions
and
1,554 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
45 changes: 45 additions & 0 deletions
45
de.peeeq.wurstscript/src/main/java/de/peeeq/datastructures/Deferred.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,45 @@ | ||
package de.peeeq.datastructures; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Compute something lazily. | ||
* | ||
* At the moment, this is used to avoid cyclic dependencies. | ||
* This should be improved with a better design. | ||
*/ | ||
public class Deferred<T> { | ||
private Supplier<T> supplier; | ||
private T value; | ||
|
||
public Deferred(Supplier<T> supplier) { | ||
this.supplier = supplier; | ||
} | ||
public Deferred(T value) { | ||
this.value = value; | ||
} | ||
|
||
public T get() { | ||
if (supplier != null) { | ||
value = supplier.get(); | ||
supplier = null; | ||
} | ||
return value; | ||
} | ||
|
||
public <S> Deferred<S> map(Function<T, S> f) { | ||
if (supplier == null) { | ||
S newValue = f.apply(value); | ||
if (newValue == value) { | ||
//noinspection unchecked | ||
return (Deferred<S>) this; | ||
} else { | ||
return new Deferred<>(newValue); | ||
} | ||
} | ||
return new Deferred<>(() -> f.apply(get())); | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.