Skip to content

Commit

Permalink
Add experimental API label. Allow null value.
Browse files Browse the repository at this point in the history
  • Loading branch information
kannanjgithub committed Sep 26, 2024
1 parent 97cb232 commit e136fe5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion api/src/main/java/io/grpc/NameResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public abstract static class Listener2 implements Listener {
@Deprecated
@InlineMe(
replacement = "this.onResult2(ResolutionResult.newBuilder().setAddressesOrError("
+ "StatusOr.of(servers)).setAttributes(attributes).build())",
+ "StatusOr.fromValue(servers)).setAttributes(attributes).build())",
imports = {"io.grpc.NameResolver.ResolutionResult", "io.grpc.StatusOr"})
public final void onAddresses(
List<EquivalentAddressGroup> servers, @ResolutionResultAttr Attributes attributes) {
Expand Down
10 changes: 6 additions & 4 deletions api/src/main/java/io/grpc/StatusOr.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Objects;
import javax.annotation.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) {
this.status = status;
this.value = value;
}

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

Expand All @@ -50,8 +52,8 @@ 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 T getValue() {
if (value == null) {
public @Nullable T getValue() {
if (status != null) {
throw new IllegalStateException("No value present.");
}
return value;
Expand Down

0 comments on commit e136fe5

Please sign in to comment.