org.mockito.internal.matchers.text.ValuePrinter Maven / Gradle / Ivy
/*
* Copyright (c) 2016 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.matchers.text;
import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.Map;
/**
* Prints a Java object value in a way humans can read it neatly.
* Inspired on hamcrest. Used for printing arguments in verification errors.
*/
public class ValuePrinter {
private ValuePrinter() {}
/**
* Prints given value so that it is neatly readable by humans.
* Handles explosive toString() implementations.
*/
public static String print(final Object value) {
if (value == null) {
return "null";
}
if (value instanceof String) {
return '"' + value.toString() + '"';
}
if (value instanceof Character) {
return printChar((Character) value);
}
if (value instanceof Long) {
return value + "L";
}
if (value instanceof Double) {
return value + "d";
}
if (value instanceof Float) {
return value + "f";
}
if (value instanceof Short) {
return "(short) " + value;
}
if (value instanceof Byte) {
return String.format("(byte) 0x%02X", (Byte) value);
}
if (value instanceof Map) {
return printMap((Map) value);
}
if (value.getClass().isArray()) {
return printValues(
"[",
", ",
"]",
new Iterator