1 4 package com.tc.util; 5 6 import org.apache.commons.lang.ClassUtils; 7 8 12 public class StandardStringifier implements Stringifier { 13 14 public static final StandardStringifier INSTANCE = new StandardStringifier(); 15 16 private StandardStringifier() { 17 } 19 20 public String toString(Object o) { 21 if (o == null) return "<null>"; 22 else if (o instanceof Object []) { 23 return toString((Object []) o); 24 } else if (o instanceof byte[]) { 25 return toString((byte[]) o); 26 } else if (o instanceof short[]) { 27 return toString((short[]) o); 28 } else if (o instanceof int[]) { 29 return toString((int[]) o); 30 } else if (o instanceof long[]) { 31 return toString((long[]) o); 32 } else if (o instanceof boolean[]) { 33 return toString((boolean[]) o); 34 } else if (o instanceof char[]) { 35 return toString((char[]) o); 36 } else if (o instanceof float[]) { 37 return toString((float[]) o); 38 } else if (o instanceof double[]) { 39 return toString((double[]) o); 40 } else { 41 return o.toString(); 42 } 43 } 44 45 private static final int MAX_ITEMS_TO_SHOW = 20; 46 47 private String toString(Object [] arr) { 48 StringBuffer out = new StringBuffer (); 49 50 out.append(ClassUtils.getShortClassName(arr.getClass().getComponentType()) + "[" + arr.length + "]"); 51 if (arr.length > 0) { 52 out.append(": "); 53 int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW); 54 for (int i = 0; i < count; ++i) { 55 if (i > 0) out.append(", "); 56 out.append(toString(arr[i])); 57 } 58 59 if (arr.length > count) { 60 out.append(", ..."); 61 } 62 } 63 64 return out.toString(); 65 } 66 67 private String toString(byte[] arr) { 68 return HexDump.dump(arr); 69 } 70 71 private String toString(short[] arr) { 72 StringBuffer out = new StringBuffer (); 73 74 out.append(ClassUtils.getShortClassName(arr.getClass().getComponentType()) + "[" + arr.length + "]"); 75 if (arr.length > 0) { 76 out.append(": "); 77 int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW); 78 for (int i = 0; i < count; ++i) { 79 if (i > 0) out.append(", "); 80 out.append("" + arr[i]); 81 } 82 83 if (arr.length > count) { 84 out.append(", ..."); 85 } 86 } 87 88 return out.toString(); 89 } 90 91 private String toString(int[] arr) { 92 StringBuffer out = new StringBuffer (); 93 94 out.append(ClassUtils.getShortClassName(arr.getClass().getComponentType()) + "[" + arr.length + "]"); 95 if (arr.length > 0) { 96 out.append(": "); 97 int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW); 98 for (int i = 0; i < count; ++i) { 99 if (i > 0) out.append(", "); 100 out.append("" + arr[i]); 101 } 102 103 if (arr.length > count) { 104 out.append(", ..."); 105 } 106 } 107 108 return out.toString(); 109 } 110 111 private String toString(long[] arr) { 112 StringBuffer out = new StringBuffer (); 113 114 out.append(ClassUtils.getShortClassName(arr.getClass().getComponentType()) + "[" + arr.length + "]"); 115 if (arr.length > 0) { 116 out.append(": "); 117 int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW); 118 for (int i = 0; i < count; ++i) { 119 if (i > 0) out.append(", "); 120 out.append("" + arr[i]); 121 } 122 123 if (arr.length > count) { 124 out.append(", ..."); 125 } 126 } 127 128 return out.toString(); 129 } 130 131 private String toString(char[] arr) { 132 StringBuffer out = new StringBuffer (); 133 134 out.append(ClassUtils.getShortClassName(arr.getClass().getComponentType()) + "[" + arr.length + "]"); 135 if (arr.length > 0) { 136 out.append(": "); 137 int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW); 138 for (int i = 0; i < count; ++i) { 139 if (i > 0) out.append(", "); 140 out.append("" + arr[i]); 141 } 142 143 if (arr.length > count) { 144 out.append(", ..."); 145 } 146 } 147 148 return out.toString(); 149 } 150 151 private String toString(float[] arr) { 152 StringBuffer out = new StringBuffer (); 153 154 out.append(ClassUtils.getShortClassName(arr.getClass().getComponentType()) + "[" + arr.length + "]"); 155 if (arr.length > 0) { 156 out.append(": "); 157 int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW); 158 for (int i = 0; i < count; ++i) { 159 if (i > 0) out.append(", "); 160 out.append("" + arr[i]); 161 } 162 163 if (arr.length > count) { 164 out.append(", ..."); 165 } 166 } 167 168 return out.toString(); 169 } 170 171 private String toString(double[] arr) { 172 StringBuffer out = new StringBuffer (); 173 174 out.append(ClassUtils.getShortClassName(arr.getClass().getComponentType()) + "[" + arr.length + "]"); 175 if (arr.length > 0) { 176 out.append(": "); 177 int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW); 178 for (int i = 0; i < count; ++i) { 179 if (i > 0) out.append(", "); 180 out.append("" + arr[i]); 181 } 182 183 if (arr.length > count) { 184 out.append(", ..."); 185 } 186 } 187 188 return out.toString(); 189 } 190 191 private String toString(boolean[] arr) { 192 StringBuffer out = new StringBuffer (); 193 194 out.append(ClassUtils.getShortClassName(arr.getClass().getComponentType()) + "[" + arr.length + "]"); 195 if (arr.length > 0) { 196 out.append(": "); 197 int count = Math.min(arr.length, MAX_ITEMS_TO_SHOW); 198 for (int i = 0; i < count; ++i) { 199 if (i > 0) out.append(", "); 200 out.append("" + arr[i]); 201 } 202 203 if (arr.length > count) { 204 out.append(", ..."); 205 } 206 } 207 208 return out.toString(); 209 } 210 211 } 212 | Popular Tags |