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






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 = (Collection) obj;
      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 = (Map) obj;
      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.