1 /* 2 * Copyright 2019 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.internal; 18 19 import java.util.ArrayList; 20 import javax.annotation.Nullable; 21 22 /** 23 * Builds a concise and readable string that gives insight of the concerned part of the system. The 24 * resulted string is made up of a list of short strings, each of which gives out a piece of 25 * information. 26 */ 27 public final class InsightBuilder { 28 private final ArrayList<String> buffer = new ArrayList<>(); 29 30 /** 31 * Appends a piece of information which is a plain string. The given object is immediately 32 * converted to string and recorded. 33 */ append(@ullable Object insight)34 public InsightBuilder append(@Nullable Object insight) { 35 buffer.add(String.valueOf(insight)); 36 return this; 37 } 38 39 /** 40 * Appends a piece of information which is a key-value , which will be formatted into {@code 41 * "key=value"}. Value's {@code toString()} or {@code null} is immediately recorded. 42 */ appendKeyValue(String key, @Nullable Object value)43 public InsightBuilder appendKeyValue(String key, @Nullable Object value) { 44 buffer.add(key + "=" + value); 45 return this; 46 } 47 48 /** 49 * Get the resulting string. 50 */ 51 @Override toString()52 public String toString() { 53 return buffer.toString(); 54 } 55 } 56