001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.core.style;
018
019import org.springframework.util.Assert;
020import org.springframework.util.ClassUtils;
021import org.springframework.util.ObjectUtils;
022
023/**
024 * Spring's default {@code toString()} styler.
025 *
026 * <p>This class is used by {@link ToStringCreator} to style {@code toString()}
027 * output in a consistent manner according to Spring conventions.
028 *
029 * @author Keith Donald
030 * @author Juergen Hoeller
031 * @since 1.2.2
032 */
033public class DefaultToStringStyler implements ToStringStyler {
034
035        private final ValueStyler valueStyler;
036
037
038        /**
039         * Create a new DefaultToStringStyler.
040         * @param valueStyler the ValueStyler to use
041         */
042        public DefaultToStringStyler(ValueStyler valueStyler) {
043                Assert.notNull(valueStyler, "ValueStyler must not be null");
044                this.valueStyler = valueStyler;
045        }
046
047        /**
048         * Return the ValueStyler used by this ToStringStyler.
049         */
050        protected final ValueStyler getValueStyler() {
051                return this.valueStyler;
052        }
053
054
055        @Override
056        public void styleStart(StringBuilder buffer, Object obj) {
057                if (!obj.getClass().isArray()) {
058                        buffer.append('[').append(ClassUtils.getShortName(obj.getClass()));
059                        styleIdentityHashCode(buffer, obj);
060                }
061                else {
062                        buffer.append('[');
063                        styleIdentityHashCode(buffer, obj);
064                        buffer.append(' ');
065                        styleValue(buffer, obj);
066                }
067        }
068
069        private void styleIdentityHashCode(StringBuilder buffer, Object obj) {
070                buffer.append('@');
071                buffer.append(ObjectUtils.getIdentityHexString(obj));
072        }
073
074        @Override
075        public void styleEnd(StringBuilder buffer, Object o) {
076                buffer.append(']');
077        }
078
079        @Override
080        public void styleField(StringBuilder buffer, String fieldName, Object value) {
081                styleFieldStart(buffer, fieldName);
082                styleValue(buffer, value);
083                styleFieldEnd(buffer, fieldName);
084        }
085
086        protected void styleFieldStart(StringBuilder buffer, String fieldName) {
087                buffer.append(' ').append(fieldName).append(" = ");
088        }
089
090        protected void styleFieldEnd(StringBuilder buffer, String fieldName) {
091        }
092
093        @Override
094        public void styleValue(StringBuilder buffer, Object value) {
095                buffer.append(this.valueStyler.style(value));
096        }
097
098        @Override
099        public void styleFieldSeparator(StringBuilder buffer) {
100                buffer.append(',');
101        }
102
103}