Constructs pretty string representation of object value : toString « Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Class » toStringScreenshots 
Constructs pretty string representation of object value
   
// Copyright (c) 2003-2009, Jodd Team (jodd.org). All Rights Reserved.


import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

/**
 * Constructs pretty string representation of object value.
 */
public class PrettyStringBuilder {

  protected int maxArrayLen = 10;
  protected int maxDeep = 3;
  protected int deep;
  protected String nullValue = "<null>";

  public PrettyStringBuilder() {
  }

  public int getMaxArrayLen() {
    return maxArrayLen;
  }

  public void setMaxArrayLen(int maxArrayLen) {
    this.maxArrayLen = maxArrayLen;
  }

  public int getMaxDeep() {
    return maxDeep;
  }

  public void setMaxDeep(int maxDeep) {
    this.maxDeep = maxDeep;
  }

  public String getNullValue() {
    return nullValue;
  }

  public void setNullValue(String nullValue) {
    this.nullValue = nullValue;
  }

  /**
   * Returns pretty value from object value.
   */
  protected String toPrettyString(Object obj) {
    deep++;
    if (obj == null) {
      deep--;
      return nullValue;
    }
    if (deep == maxDeep) {
      deep--;
      return obj.toString();
    }
    StringBuilder s = new StringBuilder();
    Class c = obj.getClass();
    if (c.isArray()) {
      int arrayLen = Array.getLength(obj);
      int len = Math.min(arrayLen, maxArrayLen);
      s.append('[');
      for (int i = 0; i < len; i++) {
        s.append(toPrettyString(Array.get(obj, i)));
        if (i != len - 1) {
          s.append(',');
        }
      }
      if (len < arrayLen) {
        s.append("...");
      }
      s.append(']');
    else if (obj instanceof Collection) {
      Collection coll = (Collectionobj;
      Iterator it = coll.iterator();
      int i = 0;
      s.append('(');
      while ((it.hasNext() && (i < maxArrayLen))) {
        s.append(toPrettyString(it.next()));
        i++;
      }
      if (i < coll.size()) {
        s.append("...");
      }
      s.append(')');
    else if (obj instanceof Map) {
      Map map = (Mapobj;
      Iterator it = map.keySet().iterator();
      int i = 0;
      s.append('{');
      while ((it.hasNext() && (i < maxArrayLen))) {
        Object key = it.next();
        s.append(key).append(':');
        s.append(toPrettyString(map.get(key)));
        i++;
      }
      if (i < map.size()) {
        s.append("...");
      }
      s.append('}');
    else {
      s.append(obj.toString());
    }
    deep--;
    return s.toString();
  }

  /**
   * Returns pretty string representation of the object.
   */
  public String toString(Object value) {
    return toPrettyString(value);
  }
}

   
    
    
  
Related examples in the same category
1.ShowToString -- demo program to show default toString methodsShowToString -- demo program to show default toString methods
2.ToString -- demo program to show a toString method
3.Demonstrate toString() without an overrideDemonstrate toString() without an override
4.To String DemoTo String Demo
5.Reflection based toString() utilities
6.Use a generic toString()
7.Null Safe To String
8.toString(Object[] array)
9.Array To String
10.Gets the toString of an Object returning an empty string ("") if null input.
11.Gets the toString that would be produced by Object if a class did not override toString itself.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.