1 /* 2 * Copyright 2016 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc; 18 19 import static io.grpc.ConnectivityState.TRANSIENT_FAILURE; 20 21 import com.google.common.base.Preconditions; 22 23 /** 24 * A tuple of a {@link ConnectivityState} and its associated {@link Status}. 25 * 26 * <p>If the state is {@code TRANSIENT_FAILURE}, the status is never {@code OK}. For other states, 27 * the status is always {@code OK}. 28 */ 29 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771") 30 public final class ConnectivityStateInfo { 31 private final ConnectivityState state; 32 private final Status status; 33 34 /** 35 * Returns an instance for a state that is not {@code TRANSIENT_FAILURE}. 36 * 37 * @throws IllegalArgumentException if {@code state} is {@code TRANSIENT_FAILURE}. 38 */ 39 public static ConnectivityStateInfo forNonError(ConnectivityState state) { 40 Preconditions.checkArgument( 41 state != TRANSIENT_FAILURE, 42 "state is TRANSIENT_ERROR. Use forError() instead"); 43 return new ConnectivityStateInfo(state, Status.OK); 44 } 45 46 /** 47 * Returns an instance for {@code TRANSIENT_FAILURE}, associated with an error status. 48 */ 49 public static ConnectivityStateInfo forTransientFailure(Status error) { 50 Preconditions.checkArgument(!error.isOk(), "The error status must not be OK"); 51 return new ConnectivityStateInfo(TRANSIENT_FAILURE, error); 52 } 53 54 /** 55 * Returns the state. 56 */ 57 public ConnectivityState getState() { 58 return state; 59 } 60 61 /** 62 * Returns the status associated with the state. 63 * 64 * <p>If the state is {@code TRANSIENT_FAILURE}, the status is never {@code OK}. For other 65 * states, the status is always {@code OK}. 66 */ 67 public Status getStatus() { 68 return status; 69 } 70 71 @Override 72 public boolean equals(Object other) { 73 if (!(other instanceof ConnectivityStateInfo)) { 74 return false; 75 } 76 ConnectivityStateInfo o = (ConnectivityStateInfo) other; 77 return state.equals(o.state) && status.equals(o.status); 78 } 79 80 @Override 81 public int hashCode() { 82 return state.hashCode() ^ status.hashCode(); 83 } 84 85 @Override 86 public String toString() { 87 if (status.isOk()) { 88 return state.toString(); 89 } 90 return state + "(" + status + ")"; 91 } 92 93 private ConnectivityStateInfo(ConnectivityState state, Status status) { 94 this.state = Preconditions.checkNotNull(state, "state is null"); 95 this.status = Preconditions.checkNotNull(status, "status is null"); 96 } 97 } 98