001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.lang3.builder;
018
019import java.util.Collection;
020
021import org.apache.commons.lang3.ClassUtils;
022
023/**
024 * <p>Works with {@link ToStringBuilder} to create a "deep" <code>toString</code>.</p>
025 *
026 * <p>To use this class write code as follows:</p>
027 *
028 * <pre>
029 * public class Job {
030 *   String title;
031 *   ...
032 * }
033 *
034 * public class Person {
035 *   String name;
036 *   int age;
037 *   boolean smoker;
038 *   Job job;
039 *
040 *   ...
041 *
042 *   public String toString() {
043 *     return new ReflectionToStringBuilder(this, new RecursiveToStringStyle()).toString();
044 *   }
045 * }
046 * </pre>
047 *
048 * <p>This will produce a toString of the format:
049 * <code>Person@7f54[name=Stephen,age=29,smoker=false,job=Job@43cd2[title=Manager]]</code></p>
050 *
051 * @since 3.2
052 */
053public class RecursiveToStringStyle extends ToStringStyle {
054
055    /**
056     * Required for serialization support.
057     *
058     * @see java.io.Serializable
059     */
060    private static final long serialVersionUID = 1L;
061
062    /**
063     * <p>Constructor.</p>
064     */
065    public RecursiveToStringStyle() {
066        super();
067    }
068
069    @Override
070    public void appendDetail(final StringBuffer buffer, final String fieldName, final Object value) {
071        if (!ClassUtils.isPrimitiveWrapper(value.getClass()) &&
072            !String.class.equals(value.getClass()) &&
073            accept(value.getClass())) {
074            buffer.append(ReflectionToStringBuilder.toString(value, this));
075        } else {
076            super.appendDetail(buffer, fieldName, value);
077        }
078    }
079
080    @Override
081    protected void appendDetail(final StringBuffer buffer, final String fieldName, final Collection<?> coll) {
082        appendClassName(buffer, coll);
083        appendIdentityHashCode(buffer, coll);
084        appendDetail(buffer, fieldName, coll.toArray());
085    }
086
087    /**
088     * Returns whether or not to recursively format the given <code>Class</code>.
089     * By default, this method always returns {@code true}, but may be overwritten by
090     * sub-classes to filter specific classes.
091     *
092     * @param clazz
093     *            The class to test.
094     * @return Whether or not to recursively format the given <code>Class</code>.
095     */
096    protected boolean accept(final Class<?> clazz) {
097        return true;
098    }
099}