Spring之DefaultValueStyler

源碼

org.springframework.core.style.DefaultValueStyler

package org.springframework.core.style;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;

/**
 * Converts objects to String form, generally for debugging purposes,
 * using Spring's {@code toString} styling conventions.
 *
 * <p>Uses the reflective visitor pattern underneath the hood to nicely
 * encapsulate styling algorithms for each type of styled object.
 *
 * @author Keith Donald
 * @author Juergen Hoeller
 * @since 1.2.2
 */
public class DefaultValueStyler implements ValueStyler {
	// 規範的定義變量
	private static final String EMPTY = "[empty]";
	private static final String NULL = "[null]";
	private static final String COLLECTION = "collection";
	private static final String SET = "set";
	private static final String LIST = "list";
	private static final String MAP = "map";
	private static final String ARRAY = "array";


	@Override
	public String style(Object value) {
		if (value == null) {
			return NULL;
		}
		else if (value instanceof String) {
			return "\'" + value + "\'";
		}
		else if (value instanceof Class) {
			return ClassUtils.getShortName((Class<?>) value);
		}
		else if (value instanceof Method) {
			Method method = (Method) value;
			return method.getName() + "@" + ClassUtils.getShortName(method.getDeclaringClass());
		}
		else if (value instanceof Map) {
			return style((Map<?, ?>) value);
		}
		else if (value instanceof Map.Entry) {
			return style((Map.Entry<? ,?>) value);
		}
		else if (value instanceof Collection) {
			return style((Collection<?>) value);
		}
		else if (value.getClass().isArray()) {
			return styleArray(ObjectUtils.toObjectArray(value));
		}
		else {
			return String.valueOf(value);
		}
	}

	private <K, V> String style(Map<K, V> value) {
		StringBuilder result = new StringBuilder(value.size() * 8 + 16);
		result.append(MAP + "[");
		for (Iterator<Map.Entry<K, V>> it = value.entrySet().iterator(); it.hasNext();) {
			Map.Entry<K, V> entry = it.next();
			result.append(style(entry));
			if (it.hasNext()) {
				result.append(',').append(' ');
			}
		}
		if (value.isEmpty()) {
			result.append(EMPTY);
		}
		result.append("]");
		return result.toString();
	}

	private String style(Map.Entry<?, ?> value) {
		return style(value.getKey()) + " -> " + style(value.getValue());
	}

	private String style(Collection<?> value) {
		StringBuilder result = new StringBuilder(value.size() * 8 + 16);
		result.append(getCollectionTypeString(value)).append('[');
		for (Iterator<?> i = value.iterator(); i.hasNext();) {
			result.append(style(i.next()));
			if (i.hasNext()) {
				result.append(',').append(' ');
			}
		}
		if (value.isEmpty()) {
			result.append(EMPTY);
		}
		result.append("]");
		return result.toString();
	}

	private String getCollectionTypeString(Collection<?> value) {
		if (value instanceof List) {
			return LIST;
		}
		else if (value instanceof Set) {
			return SET;
		}
		else {
			return COLLECTION;
		}
	}

	private String styleArray(Object[] array) {
		StringBuilder result = new StringBuilder(array.length * 8 + 16);
		result.append(ARRAY + "<").append(ClassUtils.getShortName(array.getClass().getComponentType())).append(">[");
		for (int i = 0; i < array.length - 1; i++) {
			result.append(style(array[i]));
			result.append(',').append(' ');
		}
		if (array.length > 0) {
			result.append(style(array[array.length - 1]));
		}
		else {
			result.append(EMPTY);
		}
		result.append("]");
		return result.toString();
	}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章