001/*
002 * Copyright 2002-2016 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;
020
021/**
022 * Utility class that builds pretty-printing {@code toString()} methods
023 * with pluggable styling conventions. By default, ToStringCreator adheres
024 * to Spring's {@code toString()} styling conventions.
025 *
026 * @author Keith Donald
027 * @author Juergen Hoeller
028 * @since 1.2.2
029 */
030public class ToStringCreator {
031
032        /**
033         * Default ToStringStyler instance used by this ToStringCreator.
034         */
035        private static final ToStringStyler DEFAULT_TO_STRING_STYLER =
036                        new DefaultToStringStyler(StylerUtils.DEFAULT_VALUE_STYLER);
037
038
039        private final StringBuilder buffer = new StringBuilder(256);
040
041        private final ToStringStyler styler;
042
043        private final Object object;
044
045        private boolean styledFirstField;
046
047
048        /**
049         * Create a ToStringCreator for the given object.
050         * @param obj the object to be stringified
051         */
052        public ToStringCreator(Object obj) {
053                this(obj, (ToStringStyler) null);
054        }
055
056        /**
057         * Create a ToStringCreator for the given object, using the provided style.
058         * @param obj the object to be stringified
059         * @param styler the ValueStyler encapsulating pretty-print instructions
060         */
061        public ToStringCreator(Object obj, ValueStyler styler) {
062                this(obj, new DefaultToStringStyler(styler != null ? styler : StylerUtils.DEFAULT_VALUE_STYLER));
063        }
064
065        /**
066         * Create a ToStringCreator for the given object, using the provided style.
067         * @param obj the object to be stringified
068         * @param styler the ToStringStyler encapsulating pretty-print instructions
069         */
070        public ToStringCreator(Object obj, ToStringStyler styler) {
071                Assert.notNull(obj, "The object to be styled must not be null");
072                this.object = obj;
073                this.styler = (styler != null ? styler : DEFAULT_TO_STRING_STYLER);
074                this.styler.styleStart(this.buffer, this.object);
075        }
076
077
078        /**
079         * Append a byte field value.
080         * @param fieldName the name of the field, usually the member variable name
081         * @param value the field value
082         * @return this, to support call-chaining
083         */
084        public ToStringCreator append(String fieldName, byte value) {
085                return append(fieldName, Byte.valueOf(value));
086        }
087
088        /**
089         * Append a short field value.
090         * @param fieldName the name of the field, usually the member variable name
091         * @param value the field value
092         * @return this, to support call-chaining
093         */
094        public ToStringCreator append(String fieldName, short value) {
095                return append(fieldName, Short.valueOf(value));
096        }
097
098        /**
099         * Append a integer field value.
100         * @param fieldName the name of the field, usually the member variable name
101         * @param value the field value
102         * @return this, to support call-chaining
103         */
104        public ToStringCreator append(String fieldName, int value) {
105                return append(fieldName, Integer.valueOf(value));
106        }
107
108        /**
109         * Append a long field value.
110         * @param fieldName the name of the field, usually the member variable name
111         * @param value the field value
112         * @return this, to support call-chaining
113         */
114        public ToStringCreator append(String fieldName, long value) {
115                return append(fieldName, Long.valueOf(value));
116        }
117
118        /**
119         * Append a float field value.
120         * @param fieldName the name of the field, usually the member variable name
121         * @param value the field value
122         * @return this, to support call-chaining
123         */
124        public ToStringCreator append(String fieldName, float value) {
125                return append(fieldName, Float.valueOf(value));
126        }
127
128        /**
129         * Append a double field value.
130         * @param fieldName the name of the field, usually the member variable name
131         * @param value the field value
132         * @return this, to support call-chaining
133         */
134        public ToStringCreator append(String fieldName, double value) {
135                return append(fieldName, Double.valueOf(value));
136        }
137
138        /**
139         * Append a boolean field value.
140         * @param fieldName the name of the field, usually the member variable name
141         * @param value the field value
142         * @return this, to support call-chaining
143         */
144        public ToStringCreator append(String fieldName, boolean value) {
145                return append(fieldName, Boolean.valueOf(value));
146        }
147
148        /**
149         * Append a field value.
150         * @param fieldName the name of the field, usually the member variable name
151         * @param value the field value
152         * @return this, to support call-chaining
153         */
154        public ToStringCreator append(String fieldName, Object value) {
155                printFieldSeparatorIfNecessary();
156                this.styler.styleField(this.buffer, fieldName, value);
157                return this;
158        }
159
160        private void printFieldSeparatorIfNecessary() {
161                if (this.styledFirstField) {
162                        this.styler.styleFieldSeparator(this.buffer);
163                }
164                else {
165                        this.styledFirstField = true;
166                }
167        }
168
169        /**
170         * Append the provided value.
171         * @param value The value to append
172         * @return this, to support call-chaining.
173         */
174        public ToStringCreator append(Object value) {
175                this.styler.styleValue(this.buffer, value);
176                return this;
177        }
178
179
180        /**
181         * Return the String representation that this ToStringCreator built.
182         */
183        @Override
184        public String toString() {
185                this.styler.styleEnd(this.buffer, this.object);
186                return this.buffer.toString();
187        }
188
189}