org.javers.common.string.ToStringBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javers-core Show documentation
Show all versions of javers-core Show documentation
JaVers - object auditing and diff framework for Java
package org.javers.common.string;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import static org.javers.common.validation.Validate.argumentCheck;
import static org.javers.common.validation.Validate.argumentIsNotNull;
/**
* @author bartosz walacik
*/
public class ToStringBuilder {
public static String typeName(Type type){
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType)type;
parameterizedType.getActualTypeArguments();
StringBuilder out = new StringBuilder();
out.append(typeName(parameterizedType.getRawType()) + "<");
Type[] args = ((ParameterizedType)type).getActualTypeArguments();
out.append( typeName(args[0]));
for (int i=1;i");
return out.toString();
} else if (type instanceof Class) {
return ((Class)type).getSimpleName();
} else {
return type.toString();
}
}
public static String setToString(Set set){
return listToString(new ArrayList(set));
}
public static String listToString(List list){
return "[" + join(list) + "]";
}
public static String join(Collection list){
if (list == null || list.size() == 0){
return "";
}
StringBuilder out = new StringBuilder();
boolean first = true;
for (Object it : list) {
if (!first) {
out.append( ",");
}
out.append(format(it) );
first = false;
}
return out.toString();
}
public static String toStringSimple(Object... fieldsMap){
return toString(", ", fieldsMap);
}
private static String toString(String sep, Object... fieldsMap){
argumentCheck(fieldsMap.length % 2 == 0, "map expected");
StringBuilder out = new StringBuilder();
boolean first = true;
for (int i=0; i