KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > style > ToStringCreator


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.core.style;
18
19 import org.springframework.util.Assert;
20
21 /**
22  * Utility class that builds pretty-printing <code>toString()</code> methods
23  * with pluggable styling conventions. By default, ToStringCreator adheres
24  * to Spring's <code>toString()</code> styling conventions.
25  *
26  * @author Keith Donald
27  * @author Juergen Hoeller
28  * @since 1.2.2
29  */

30 public class ToStringCreator {
31
32     /**
33      * Default ToStringStyler instance used by this ToStringCreator.
34      */

35     private static final ToStringStyler DEFAULT_TO_STRING_STYLER =
36             new DefaultToStringStyler(StylerUtils.DEFAULT_VALUE_STYLER);
37
38
39     private StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(512);
40
41     private ToStringStyler styler;
42
43     private Object JavaDoc object;
44
45     private boolean styledFirstField;
46
47
48     /**
49      * Create a ToStringCreator for the given object.
50      * @param obj the object to be stringified
51      */

52     public ToStringCreator(Object JavaDoc obj) {
53         this(obj, (ToStringStyler) null);
54     }
55
56     /**
57      * Create a ToStringCreator for the given object, using the provided style.
58      * @param obj the object to be stringified
59      * @param styler the ValueStyler encapsulating pretty-print instructions
60      */

61     public ToStringCreator(Object JavaDoc obj, ValueStyler styler) {
62         this(obj, new DefaultToStringStyler(styler != null ? styler : StylerUtils.DEFAULT_VALUE_STYLER));
63     }
64
65     /**
66      * Create a ToStringCreator for the given object, using the provided style.
67      * @param obj the object to be stringified
68      * @param styler the ToStringStyler encapsulating pretty-print instructions
69      */

70     public ToStringCreator(Object JavaDoc obj, ToStringStyler styler) {
71         Assert.notNull(obj, "The object to be styled must not be null");
72         this.object = obj;
73         this.styler = (styler != null ? styler : DEFAULT_TO_STRING_STYLER);
74         this.styler.styleStart(this.buffer, this.object);
75     }
76
77
78     /**
79      * Append a byte field value.
80      * @param fieldName the name of the field, usually the member variable name
81      * @param value the field value
82      * @return this, to support call-chaining
83      */

84     public ToStringCreator append(String JavaDoc fieldName, byte value) {
85         return append(fieldName, new Byte JavaDoc(value));
86     }
87
88     /**
89      * Append a short field value.
90      * @param fieldName the name of the field, usually the member variable name
91      * @param value the field value
92      * @return this, to support call-chaining
93      */

94     public ToStringCreator append(String JavaDoc fieldName, short value) {
95         return append(fieldName, new Short JavaDoc(value));
96     }
97
98     /**
99      * 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 JavaDoc fieldName, int value) {
105         return append(fieldName, new Integer JavaDoc(value));
106     }
107
108     /**
109      * Append a float 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 JavaDoc fieldName, float value) {
115         return append(fieldName, new Float JavaDoc(value));
116     }
117
118     /**
119      * Append a double 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 JavaDoc fieldName, double value) {
125         return append(fieldName, new Double JavaDoc(value));
126     }
127
128     /**
129      * Append a long 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 JavaDoc fieldName, long value) {
135         return append(fieldName, new Long JavaDoc(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 JavaDoc fieldName, boolean value) {
145         return append(fieldName, value ? Boolean.TRUE : Boolean.FALSE);
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 JavaDoc fieldName, Object JavaDoc 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 JavaDoc 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     public String JavaDoc toString() {
184         this.styler.styleEnd(this.buffer, this.object);
185         return this.buffer.toString();
186     }
187
188 }
189
Popular Tags