Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2021 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/
package org.eclipse.edc.spi.result;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* A generic result type.
*/
public class Result extends AbstractResult> {
private Result(T content, Failure failure) {
super(content, failure);
}
public static Result success() {
return new Result<>(null, null);
}
public static Result success(T content) {
return new Result<>(content, null);
}
public static Result failure(String failure) {
return new Result<>(null, new Failure(List.of(failure)));
}
public static Result failure(List failures) {
return new Result<>(null, new Failure(failures));
}
/**
* Runs the block provided by the {@link Supplier} and wrap the return into a Result
*
* @param supplier The block to execute
* @return The Result of the supplier call. Success if no {@link Exception} were thrown. Failure otherwise
*/
public static Result ofThrowable(Supplier supplier) {
try {
return Result.success(supplier.get());
} catch (Exception e) {
return Result.failure(e.getMessage());
}
}
/**
* Converts a {@link Optional} into a result, interpreting the Optional's value as content.
*
* @return {@link Result#failure(String)} if the Optional is empty, {@link Result#success(Object)} using the Optional's value otherwise.
*/
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public static Result from(Optional opt) {
return opt.map(Result::success).orElse(Result.failure("Empty optional"));
}
/**
* Merges this result with another one. If both Results are successful, a new one is created with no content. If
* either this result or {@code other} is a failure, the merged result will be a {@code failure()} and will contain
* all failure messages, no content. If both results are failures, the merged result will contain failure messages
* of {@code this} result, then the failure messages of {@code other}.
*/
public Result merge(Result> other) {
if (succeeded() && other.succeeded()) {
return new Result<>(null, null);
} else {
var messages = new ArrayList();
messages.addAll(Optional.ofNullable(getFailure()).map(Failure::getMessages).orElse(Collections.emptyList()));
messages.addAll(Optional.ofNullable(other.getFailure()).map(Failure::getMessages).orElse(Collections.emptyList()));
return Result.failure(messages);
}
}
/**
* Maps this {@link Result} into another, maintaining the basic semantics (failed vs success). If this
* {@link Result} is successful, the content is discarded. If this {@link Result} failed, the failures are carried
* over. This method is intended for use when the return type is implicit, for example:
*
* public Result<Void> someMethod() {
* Result<String> result = getStringResult();
* return result.mapTo();
* }
*
*
* @see Result#map(Function)
* @see Result#mapTo(Class)
* @deprecated please use {@link #mapEmpty()} or {@link #mapFailure()}.
*/
@Deprecated(since = "0.6.4")
public Result mapTo() {
return mapEmpty();
}
/**
* Maps this {@link Result} into another, maintaining the basic semantics (failed vs success). If this
* {@link Result} is successful, the content is discarded. If this {@link Result} failed, the failures are carried
* over. This method is intended for use when an explicit return type is needed, for example when using var:
*
* Result<String> result = getStringResult();
* var voidResult = result.mapTo(Void.class);
*
*
* @param clazz type of the result, with which the resulting {@link Result} should be parameterized
* @see Result#map(Function)
* @see Result#mapTo()
* @deprecated please use {@link #mapEmpty()} or {@link #mapFailure()}.
*/
@Deprecated(since = "0.6.4")
public Result mapTo(Class clazz) {
return mapEmpty();
}
/**
* Converts this result into an {@link Optional}. When this result is failed, or there is no content,
* {@link Optional#isEmpty()} is returned, otherwise the content is the {@link Optional}'s value
*
* @return {@link Optional#empty()} if failed, or no content, {@link Optional#of(Object)} otherwise.
*/
public Optional asOptional() {
return succeeded() && getContent() != null ? Optional.of(getContent()) : Optional.empty();
}
@Override
@SuppressWarnings("unchecked")
@NotNull
protected , C1> R1 newInstance(@Nullable C1 content, @Nullable Failure failure) {
return (R1) new Result<>(content, failure);
}
}