A convenient framework for toString()


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public static <T> Map<String,Object> formatArg(String name,T value)
  2. {
  3. if(name == null || value == null)
  4. {
  5. throw new IllegalArgumentException("either name " + name + " or value " + value + " was null.");
  6. }
  7. Map<String,Object> m = newHashMap();
  8. m.put(Util.KEY_FORMAT_OBJECT_NAME,name);
  9. m.put(Util.KEY_FORMAT_OBJECT_VALUE,value);
  10. return m;
  11. }
  12.  
  13. public static <T> String formatObjectToString(
  14. Class<T> clss,List<Map<String,Object>> maps)
  15. {
  16.  
  17. StringBuilder buf = new StringBuilder();
  18. if(clss != null)
  19. {
  20. buf.append(clss.getName() + "\n");
  21. for(Map<String,Object> map:maps)
  22. {
  23. String name = (String)map.get(KEY_FORMAT_OBJECT_NAME);
  24. Object value = map.get(KEY_FORMAT_OBJECT_VALUE);
  25. String id = name + "(" + value.getClass().getName() + ")";
  26. buf.append(String.format("%1$-60s %2$s\n",id,value));
  27. }
  28. }
  29. return buf.toString();
  30. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.