Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
267 changes: 122 additions & 145 deletions aas_core_codegen/java/lib/_generate_jsonization.py

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions aas_core_codegen/java/lib/_generate_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,72 @@ def generate(package: java_common.PackageIdentifier) -> List[java_common.JavaFil
{I}public Collection<Segment> getPathSegments() {{
{II}return pathSegments;
{I}}}
}}"""
),
Stripped(
f"""\
/**
* Represent the outcome of a de/serialization or a verification step.
*
* <p>This is shared by JSON de/serialization, XML de/serialization and
* verification, all of which propagate an {{@link Error}} instead of relying
* on exceptions for the common (successful) case.
*/
public static class Result<T> {{
{I}private final T result;
{I}private final Error error;
{I}private final boolean success;

{I}private Result(T result, Error error, boolean success) {{
{II}this.result = result;
{II}this.error = error;
{II}this.success = success;
{I}}}

{I}public static <T> Result<T> success(T result) {{
{II}if (result == null) throw new IllegalArgumentException("Result must not be null.");
{II}return new Result<>(result, null, true);
{I}}}

{I}public static <T> Result<T> failure(Error error) {{
{II}if (error == null) throw new IllegalArgumentException("Error must not be null.");
{II}return new Result<>(null, error, false);
{I}}}

{I}@SuppressWarnings("unchecked")
{I}public <I> Result<I> castTo(Class<I> type) {{
{II}if (isError() || type.isInstance(result)) return (Result<I>) this;
{II}throw new IllegalStateException("Result of type "
{III}+ result.getClass().getName()
{III}+ " is not an instance of "
{III}+ type.getName());
{I}}}

{I}public T getResult() {{
{II}if (!isSuccess()) throw new IllegalStateException("Result is not present.");
{II}return result;
{I}}}

{I}public boolean isSuccess() {{
{II}return success;
{I}}}

{I}public boolean isError() {{
{II}return !success;
{I}}}

{I}public Error getError() {{
{II}if (isSuccess()) throw new IllegalStateException("Result is present.");
{II}return error;
{I}}}

{I}public <R> R map(Function<T, R> successFunction, Function<Error, R> errorFunction) {{
{II}return isSuccess() ? successFunction.apply(result) : errorFunction.apply(error);
{I}}}

{I}public T onError(Function<Error, T> errorFunction) {{
{II}return map(Function.identity(), errorFunction);
{I}}}
}}"""
),
] # type: List[Stripped]
Expand All @@ -198,6 +264,7 @@ def generate(package: java_common.PackageIdentifier) -> List[java_common.JavaFil
import java.util.List;
import java.util.LinkedList;
import java.util.Objects;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down
25 changes: 25 additions & 0 deletions aas_core_codegen/java/lib/_generate_stringification.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,31 @@ def _generate_enum_to_and_from_string(

# endregion

# region Must-to-string-method

must_to_str_name = java_naming.method_name(Identifier("must_to_string"))

blocks.append(
Stripped(
f"""\
/**
* Retrieve the string representation of {{@code that}}.
*
* @throws IllegalArgumentException if {{@code that}} is not a valid literal
*/
public static String {must_to_str_name}({name} that)
{{
{I}final Optional<String> text = {to_str_name}(that);
{I}if (!text.isPresent()) {{
{II}throw new IllegalArgumentException("Invalid literal of {name}: " + that);
{I}}}
{I}return text.get();
}}"""
)
)

# endregion

# region From-string-map

string_to_enum_blocks = [] # type: List[Stripped]
Expand Down
Loading
Loading