1 /* 2 * Copyright (C) 2016 The Android Open Source Project 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 package com.google.android.exoplayer2.testutil; 17 18 import androidx.annotation.Nullable; 19 import com.google.android.exoplayer2.C; 20 import java.util.Arrays; 21 import java.util.Locale; 22 23 /** 24 * Helper utility to dump field values. 25 */ 26 public final class Dumper { 27 28 /** 29 * Provides custom dump method. 30 */ 31 public interface Dumpable { 32 /** 33 * Dumps the fields of the object using the {@code dumper}. 34 * @param dumper The {@link Dumper} to be used to dump fields. 35 */ dump(Dumper dumper)36 void dump(Dumper dumper); 37 } 38 39 private static final int INDENT_SIZE_IN_SPACES = 2; 40 41 private final StringBuilder sb; 42 private int indent; 43 Dumper()44 public Dumper() { 45 sb = new StringBuilder(); 46 } 47 add(String field, @Nullable Object value)48 public Dumper add(String field, @Nullable Object value) { 49 return addString(field + " = " + value + '\n'); 50 } 51 add(Dumpable object)52 public Dumper add(Dumpable object) { 53 object.dump(this); 54 return this; 55 } 56 add(String field, @Nullable byte[] value)57 public Dumper add(String field, @Nullable byte[] value) { 58 String string = 59 String.format( 60 Locale.US, 61 "%s = length %d, hash %X\n", 62 field, 63 value == null ? 0 : value.length, 64 Arrays.hashCode(value)); 65 return addString(string); 66 } 67 addTime(String field, long time)68 public Dumper addTime(String field, long time) { 69 return add(field, time == C.TIME_UNSET ? "UNSET TIME" : time); 70 } 71 startBlock(String name)72 public Dumper startBlock(String name) { 73 addString(name + ":\n"); 74 indent += INDENT_SIZE_IN_SPACES; 75 return this; 76 } 77 endBlock()78 public Dumper endBlock() { 79 indent -= INDENT_SIZE_IN_SPACES; 80 return this; 81 } 82 83 @Override toString()84 public String toString() { 85 return sb.toString(); 86 } 87 addString(String string)88 private Dumper addString(String string) { 89 for (int i = 0; i < indent; i++) { 90 sb.append(' '); 91 } 92 sb.append(string); 93 return this; 94 } 95 96 } 97