Debian Codegraph @

src/org/apache/commons/lang3/builder/MultilineRecursiveToStringStyle.java

raw symbols SCIP
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.lang3.builder;
19
20 import org.apache.commons.lang3.ClassUtils;
21 import org.apache.commons.lang3.StringUtils;
22
23 /**
24 * Works with {@link ToStringBuilder} to create a "deep" {@code toString}.
25 * But instead a single line like the {@link RecursiveToStringStyle} this creates a multiline String
26 * similar to the {@link ToStringStyle#MULTI_LINE_STYLE}.
27 *
28 * <p>To use this class write code as follows:</p>
29 *
30 * <pre>
31 * public class Job {
32 * String title;
33 * ...
34 * }
35 *
36 * public class Person {
37 * String name;
38 * int age;
39 * boolean smoker;
40 * Job job;
41 *
42 * ...
43 *
44 * public String toString() {
45 * return new ReflectionToStringBuilder(this, new MultilineRecursiveToStringStyle()).toString();
46 * }
47 * }
48 * </pre>
49 *
50 * <p>
51 * This will produce a toString of the format:<br>
52 * {@code Person@7f54[ <br>
53 * &nbsp; name=Stephen, <br>
54 * &nbsp; age=29, <br>
55 * &nbsp; smokealse, <br>
56 * &nbsp; job=Job@43cd2[ <br>
57 * &nbsp; &nbsp; title=Manager <br>
58 * &nbsp; ] <br>
59 * ]
60 * }
61 * </p>
62 *
63 * @since 3.4
64 */
65 public class MultilineRecursiveToStringStyle extends RecursiveToStringStyle {
66
67 /**
68 * Required for serialization support.
69 * @see java.io.Serializable
70 */
71 private static final long serialVersionUID = 1L;
72
73 /** Indenting of inner lines. */
74 private static final int INDENT = 2;
75
76 /** Current indenting. */
77 private int spaces = 2;
78
79 /**
80 * Constructs a new instance.
81 */
82 public MultilineRecursiveToStringStyle() {
83 resetIndent();
84 }
85
86 @Override
87 protected void appendDetail(final StringBuffer buffer, final String fieldName, final boolean[] array) {
88 spaces += INDENT;
89 resetIndent();
90 super.appendDetail(buffer, fieldName, array);
91 spaces -= INDENT;
92 resetIndent();
93 }
94
95 @Override
96 protected void appendDetail(final StringBuffer buffer, final String fieldName, final byte[] array) {
97 spaces += INDENT;
98 resetIndent();
99 super.appendDetail(buffer, fieldName, array);
100 spaces -= INDENT;
101 resetIndent();
102 }
103
104 @Override
105 protected void appendDetail(final StringBuffer buffer, final String fieldName, final char[] array) {
106 spaces += INDENT;
107 resetIndent();
108 super.appendDetail(buffer, fieldName, array);
109 spaces -= INDENT;
110 resetIndent();
111 }
112
113 @Override
114 protected void appendDetail(final StringBuffer buffer, final String fieldName, final double[] array) {
115 spaces += INDENT;
116 resetIndent();
117 super.appendDetail(buffer, fieldName, array);
118 spaces -= INDENT;
119 resetIndent();
120 }
121
122 @Override
123 protected void appendDetail(final StringBuffer buffer, final String fieldName, final float[] array) {
124 spaces += INDENT;
125 resetIndent();
126 super.appendDetail(buffer, fieldName, array);
127 spaces -= INDENT;
128 resetIndent();
129 }
130
131 @Override
132 protected void appendDetail(final StringBuffer buffer, final String fieldName, final int[] array) {
133 spaces += INDENT;
134 resetIndent();
135 super.appendDetail(buffer, fieldName, array);
136 spaces -= INDENT;
137 resetIndent();
138 }
139
140 @Override
141 protected void appendDetail(final StringBuffer buffer, final String fieldName, final long[] array) {
142 spaces += INDENT;
143 resetIndent();
144 super.appendDetail(buffer, fieldName, array);
145 spaces -= INDENT;
146 resetIndent();
147 }
148
149 @Override
150 public void appendDetail(final StringBuffer buffer, final String fieldName, final Object value) {
151 if (!ClassUtils.isPrimitiveWrapper(value.getClass()) && !String.class.equals(value.getClass())
152 && accept(value.getClass())) {
153 spaces += INDENT;
154 resetIndent();
155 buffer.append(ReflectionToStringBuilder.toString(value, this));
156 spaces -= INDENT;
157 resetIndent();
158 } else {
159 super.appendDetail(buffer, fieldName, value);
160 }
161 }
162
163 @Override
164 protected void appendDetail(final StringBuffer buffer, final String fieldName, final Object[] array) {
165 spaces += INDENT;
166 resetIndent();
167 super.appendDetail(buffer, fieldName, array);
168 spaces -= INDENT;
169 resetIndent();
170 }
171
172 @Override
173 protected void appendDetail(final StringBuffer buffer, final String fieldName, final short[] array) {
174 spaces += INDENT;
175 resetIndent();
176 super.appendDetail(buffer, fieldName, array);
177 spaces -= INDENT;
178 resetIndent();
179 }
180
181 @Override
182 protected void reflectionAppendArrayDetail(final StringBuffer buffer, final String fieldName, final Object array) {
183 spaces += INDENT;
184 resetIndent();
185 super.reflectionAppendArrayDetail(buffer, fieldName, array);
186 spaces -= INDENT;
187 resetIndent();
188 }
189
190 /**
191 * Resets the fields responsible for the line breaks and indenting.
192 * Must be invoked after changing the {@link #spaces} value.
193 */
194 private void resetIndent() {
195 setArrayStart("{" + System.lineSeparator() + spacer(spaces));
196 setArraySeparator("," + System.lineSeparator() + spacer(spaces));
197 setArrayEnd(System.lineSeparator() + spacer(spaces - INDENT) + "}");
198
199 setContentStart("[" + System.lineSeparator() + spacer(spaces));
200 setFieldSeparator("," + System.lineSeparator() + spacer(spaces));
201 setContentEnd(System.lineSeparator() + spacer(spaces - INDENT) + "]");
202 }
203
204 /**
205 * Creates a StringBuilder responsible for the indenting.
206 *
207 * @param spaces how far to indent
208 * @return a StringBuilder with {spaces} leading space characters.
209 */
210 private String spacer(final int spaces) {
211 return StringUtils.repeat(' ', spaces);
212 }
213
214 }