-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce common api for finding uses of an element
- Loading branch information
Showing
13 changed files
with
300 additions
and
120 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
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
59 changes: 59 additions & 0 deletions
59
autograder-core/src/main/java/de/firemage/autograder/core/check/utils/Option.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,59 @@ | ||
package de.firemage.autograder.core.check.utils; | ||
|
||
import java.util.Iterator; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
public sealed interface Option<T> extends Iterable<T> permits Option.Some, Option.None { | ||
static <T> Option<T> ofNullable(T value) { | ||
return value == null ? new None<>() : new Some<>(value); | ||
} | ||
|
||
default T unwrap() { | ||
return switch (this) { | ||
case Some<T> (var value) -> value; | ||
case None<T> ignored -> throw new IllegalStateException("Expected Some value, but got None."); | ||
}; | ||
} | ||
|
||
default boolean isSome() { | ||
return this instanceof Some; | ||
} | ||
|
||
default <U> Option<U> map(Function<T, U> function) { | ||
return switch (this) { | ||
case Some<T>(var value) -> new Some<>(function.apply(value)); | ||
case None<T> ignored -> new None<>(); | ||
}; | ||
} | ||
|
||
/** | ||
* Returns the value if it is present or null if it is not. | ||
* | ||
* @return the value or null | ||
*/ | ||
default T nullable() { | ||
return switch (this) { | ||
case Some<T>(var value) -> value; | ||
case None<T> ignored -> null; | ||
}; | ||
} | ||
|
||
default Stream<T> stream() { | ||
return switch (this) { | ||
case Some<T>(var value) -> Stream.of(value); | ||
case None<T> ignored -> Stream.empty(); | ||
}; | ||
} | ||
|
||
@Override | ||
default Iterator<T> iterator() { | ||
return stream().iterator(); | ||
} | ||
|
||
record None<T>() implements Option<T> { | ||
} | ||
|
||
record Some<T>(T value) implements Option<T> { | ||
} | ||
} |
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.