1 15 package org.apache.hivemind.util; 16 17 24 public class ToStringBuilder 25 { 26 private StringBuffer _buffer = new StringBuffer (); 27 28 private int _mode; 29 private int _attributeCount; 30 31 private static int _defaultMode; 32 33 public static final int INCLUDE_PACKAGE_PREFIX = 0x1; 34 public static final int INCLUDE_HASHCODE = 0x02; 35 36 public ToStringBuilder(Object target) 37 { 38 this(target, _defaultMode); 39 } 40 41 public ToStringBuilder(Object target, int mode) 42 { 43 _mode = mode; 44 45 appendClassName(target); 46 appendHashCode(target); 47 } 48 49 private void appendHashCode(Object target) 50 { 51 if ((_mode & INCLUDE_HASHCODE) == 0) 52 return; 53 54 _buffer.append('@'); 55 _buffer.append(Integer.toHexString(target.hashCode())); 56 } 57 58 private void appendClassName(Object target) 59 { 60 String className = target.getClass().getName(); 61 62 if ((_mode & INCLUDE_PACKAGE_PREFIX) != 0) 63 { 64 _buffer.append(className); 65 return; 66 } 67 68 int lastdotx = className.lastIndexOf('.'); 69 70 _buffer.append(className.substring(lastdotx + 1)); 71 } 72 73 public static int getDefaultMode() 74 { 75 return _defaultMode; 76 } 77 78 public static void setDefaultMode(int i) 79 { 80 _defaultMode = i; 81 } 82 83 87 public String toString() 88 { 89 if (_attributeCount > 0) 90 _buffer.append(']'); 91 92 String result = _buffer.toString(); 93 94 _buffer = null; 95 96 return result; 97 } 98 99 public void append(String attributeName, boolean value) 100 { 101 append(attributeName, String.valueOf(value)); 102 } 103 104 public void append(String attributeName, byte value) 105 { 106 append(attributeName, String.valueOf(value)); 107 108 } 109 public void append(String attributeName, short value) 110 { 111 append(attributeName, String.valueOf(value)); 112 } 113 114 public void append(String attributeName, int value) 115 { 116 append(attributeName, String.valueOf(value)); 117 } 118 119 public void append(String attributeName, Object value) 120 { 121 append(attributeName, String.valueOf(value)); 122 } 123 124 public void append(String attributeName, String value) 125 { 126 if (_attributeCount++ == 0) 127 _buffer.append('['); 128 129 else 130 _buffer.append(' '); 131 132 _buffer.append(attributeName); 133 134 _buffer.append('='); 135 136 _buffer.append(value); 137 } 138 } 139 | Popular Tags |