Skip to content
Closed
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
1 change: 1 addition & 0 deletions api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ java_library(
deps = [
artifact("com.google.code.findbugs:jsr305"),
artifact("com.google.errorprone:error_prone_annotations"),
artifact("org.jspecify:jspecify"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually am not that familiar with bazel, so I just added the line which seemed to be valid, but have not tested it

artifact("com.google.guava:failureaccess"), # future transitive dep of Guava. See #5214
artifact("com.google.guava:guava"),
],
Expand Down
1 change: 1 addition & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
compileOnly sourceSets.context.output
api libraries.jsr305,
libraries.errorprone.annotations
libraries.jspecify
implementation libraries.guava

testFixturesApi libraries.truth
Expand Down
24 changes: 13 additions & 11 deletions api/src/main/java/io/grpc/StatusOr.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,27 @@
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Objects;
import javax.annotation.Nullable;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

/** Either a Status or a value. */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/11563")
public class StatusOr<T> {
private StatusOr(Status status, T value) {
@NullMarked
public class StatusOr<T extends @Nullable Object> {
private StatusOr(@Nullable Status status, @Nullable T value) {
assert status == null || value == null : "is `status` is not `null` then `value` should be `null`";
this.status = status;
this.value = value;
}

/** Construct from a value. */
public static <T> StatusOr<T> fromValue(@Nullable T value) {
StatusOr<T> result = new StatusOr<T>(null, value);
return result;
public static <T extends @Nullable Object> StatusOr<T> fromValue(T value) {
return new StatusOr<>(null, value);
}

/** Construct from a non-Ok status. */
public static <T> StatusOr<T> fromStatus(Status status) {
StatusOr<T> result = new StatusOr<T>(checkNotNull(status, "status"), null);
public static <T extends @Nullable Object> StatusOr<T> fromStatus(Status status) {
StatusOr<T> result = new StatusOr<>(checkNotNull(status, "status"), null);
checkArgument(!status.isOk(), "cannot use OK status: %s", status);
return result;
}
Expand All @@ -54,7 +56,7 @@ public boolean hasValue() {
* Returns the value if set or throws exception if there is no value set. This method is meant
* to be called after checking the return value of hasValue() first.
*/
public @Nullable T getValue() {
public T getValue() {
if (status != null) {
throw new IllegalStateException("No value present.");
}
Expand Down Expand Up @@ -105,6 +107,6 @@ public String toString() {
return stringHelper.toString();
}

private final Status status;
private final T value;
private final @Nullable Status status;
private final @Nullable T value;
}
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cronet-embedded = "org.chromium.net:cronet-embedded:119.6045.31"
errorprone-annotations = "com.google.errorprone:error_prone_annotations:2.36.0"
# error-prone 2.32.0+ require Java 17+
errorprone-core = "com.google.errorprone:error_prone_core:2.31.0"
jspecify = "org.jspecify:jspecify:1.0.0"
google-api-protos = "com.google.api.grpc:proto-google-common-protos:2.59.2"
# google-auth-library 1.25.0+ requires error_prone_annotations 2.31.0+, which
# breaks the Android build
Expand Down
Loading