| 52 | | |
| 53 | | |
| | 53 | |
| | 54 | /** |
| | 55 | * Returns a string representation of the contents of given |
| | 56 | * object with special care for potential arrays. |
| | 57 | * For none array objects the toString method is invoked, for |
| | 58 | * arrays the content of the array is converted. |
| | 59 | * |
| | 60 | * If the array contains other arrays as elements, they are converted |
| | 61 | * to strings by the {@link Object#toString}method inherited |
| | 62 | * from <tt>Object</tt>, which describes their <i>identities</i> |
| | 63 | * rather than their contents. |
| | 64 | * <p> |
| | 65 | * The value returned by this method is equal to the value that would be |
| | 66 | * returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>array</tt> |
| | 67 | * is <tt>null</tt>, in which case <tt>"null"</tt> is returned. |
| | 68 | * <p> |
| | 69 | * This method is useful to dump the content of the array in a tracing |
| | 70 | * method, e.g.: |
| | 71 | * <pre> |
| | 72 | * logger.entering(CLASSNAME, "foo(Object[])", ArraysUtil.toString(array)); |
| | 73 | * </pre> |
| | 74 | * @param array The array whose string representation to return. |
| | 75 | * @return A string representation of <tt>array</tt>. |
| | 76 | */ |
| | 77 | public static String toString (Object array) |
| | 78 | { |
| | 79 | final String result; |
| | 80 | |
| | 81 | if (array == null) |
| | 82 | { |
| | 83 | result = "null"; |
| | 84 | } |
| | 85 | else if (!array.getClass().isArray()) |
| | 86 | { |
| | 87 | result = String.valueOf(array); |
| | 88 | } |
| | 89 | else if (array instanceof Object[]) |
| | 90 | { |
| | 91 | result = ArraysUtil.toString((Object[]) array); |
| | 92 | } |
| | 93 | else if (array instanceof boolean[]) |
| | 94 | { |
| | 95 | result = ArraysUtil.toString((boolean[]) array); |
| | 96 | } |
| | 97 | else if (array instanceof byte[]) |
| | 98 | { |
| | 99 | result = ArraysUtil.toString((byte[]) array); |
| | 100 | } |
| | 101 | else if (array instanceof short[]) |
| | 102 | { |
| | 103 | result = ArraysUtil.toString((short[]) array); |
| | 104 | } |
| | 105 | else if (array instanceof char[]) |
| | 106 | { |
| | 107 | result = ArraysUtil.toString((char[]) array); |
| | 108 | } |
| | 109 | else if (array instanceof int[]) |
| | 110 | { |
| | 111 | result = ArraysUtil.toString((int[]) array); |
| | 112 | } |
| | 113 | else if (array instanceof long[]) |
| | 114 | { |
| | 115 | result = ArraysUtil.toString((long[]) array); |
| | 116 | } |
| | 117 | else // no float/double |
| | 118 | { |
| | 119 | result = String.valueOf(array); |
| | 120 | } |
| | 121 | return result; |
| | 122 | } |
| | 123 | |
| | 124 | |
| | 176 | |
| | 177 | |
| | 178 | /** |
| | 179 | * Returns a string representation of the contents of the specified array. |
| | 180 | * <p> |
| | 181 | * The value dumps all int stored in the given array. |
| | 182 | * <p> |
| | 183 | * This method is useful to dump the content of the array in a tracing |
| | 184 | * method, e.g.: |
| | 185 | * <pre> |
| | 186 | * logger.entering(CLASSNAME, "foo(int[])", ArraysUtil.toString(array)); |
| | 187 | * </pre> |
| | 188 | * @param array The array whose string representation to return. |
| | 189 | * @return A string representation of <tt>array</tt>. |
| | 190 | */ |
| | 191 | public static String toString (int[] array) |
| | 192 | { |
| | 193 | final String result; |
| | 194 | |
| | 195 | if (array == null) |
| | 196 | { |
| | 197 | result = "null"; |
| | 198 | } |
| | 199 | else if (array.length == 0) |
| | 200 | { |
| | 201 | result = "[]"; |
| | 202 | } |
| | 203 | else |
| | 204 | { |
| | 205 | final StringBuffer buf = new StringBuffer(); |
| | 206 | for (int i = 0; i < array.length; i++) |
| | 207 | { |
| | 208 | if (i == 0) |
| | 209 | { |
| | 210 | buf.append('['); |
| | 211 | } |
| | 212 | else |
| | 213 | { |
| | 214 | buf.append(", "); |
| | 215 | } |
| | 216 | buf.append(String.valueOf(array[i])); |
| | 217 | } |
| | 218 | buf.append(']'); |
| | 219 | result = buf.toString(); |
| | 220 | } |
| | 221 | return result; |
| | 222 | } |
| | 223 | |
| | 224 | /** |
| | 225 | * Returns a string representation of the contents of the specified array. |
| | 226 | * <p> |
| | 227 | * The value dumps all short stored in the given array. |
| | 228 | * <p> |
| | 229 | * This method is useful to dump the content of the array in a tracing |
| | 230 | * method, e.g.: |
| | 231 | * <pre> |
| | 232 | * logger.entering(CLASSNAME, "foo(short[])", ArraysUtil.toString(array)); |
| | 233 | * </pre> |
| | 234 | * @param array The array whose string representation to return. |
| | 235 | * @return A string representation of <tt>array</tt>. |
| | 236 | */ |
| | 237 | public static String toString (short[] array) |
| | 238 | { |
| | 239 | final String result; |
| | 240 | |
| | 241 | if (array == null) |
| | 242 | { |
| | 243 | result = "null"; |
| | 244 | } |
| | 245 | else if (array.length == 0) |
| | 246 | { |
| | 247 | result = "[]"; |
| | 248 | } |
| | 249 | else |
| | 250 | { |
| | 251 | final StringBuffer buf = new StringBuffer(); |
| | 252 | for (int i = 0; i < array.length; i++) |
| | 253 | { |
| | 254 | if (i == 0) |
| | 255 | { |
| | 256 | buf.append('['); |
| | 257 | } |
| | 258 | else |
| | 259 | { |
| | 260 | buf.append(", "); |
| | 261 | } |
| | 262 | buf.append(String.valueOf(array[i])); |
| | 263 | } |
| | 264 | buf.append(']'); |
| | 265 | result = buf.toString(); |
| | 266 | } |
| | 267 | return result; |
| | 268 | } |
| | 269 | |
| | 270 | /** |
| | 271 | * Returns a string representation of the contents of the specified array. |
| | 272 | * <p> |
| | 273 | * The value dumps all long stored in the given array. |
| | 274 | * <p> |
| | 275 | * This method is useful to dump the content of the array in a tracing |
| | 276 | * method, e.g.: |
| | 277 | * <pre> |
| | 278 | * logger.entering(CLASSNAME, "foo(long[])", ArraysUtil.toString(array)); |
| | 279 | * </pre> |
| | 280 | * @param array The array whose string representation to return. |
| | 281 | * @return A string representation of <tt>array</tt>. |
| | 282 | */ |
| | 283 | public static String toString (long[] array) |
| | 284 | { |
| | 285 | final String result; |
| | 286 | |
| | 287 | if (array == null) |
| | 288 | { |
| | 289 | result = "null"; |
| | 290 | } |
| | 291 | else if (array.length == 0) |
| | 292 | { |
| | 293 | result = "[]"; |
| | 294 | } |
| | 295 | else |
| | 296 | { |
| | 297 | final StringBuffer buf = new StringBuffer(); |
| | 298 | for (int i = 0; i < array.length; i++) |
| | 299 | { |
| | 300 | if (i == 0) |
| | 301 | { |
| | 302 | buf.append('['); |
| | 303 | } |
| | 304 | else |
| | 305 | { |
| | 306 | buf.append(", "); |
| | 307 | } |
| | 308 | buf.append(String.valueOf(array[i])); |
| | 309 | } |
| | 310 | buf.append(']'); |
| | 311 | result = buf.toString(); |
| | 312 | } |
| | 313 | return result; |
| | 314 | } |
| | 315 | |
| | 316 | /** |
| | 317 | * Returns a string representation of the contents of the specified array. |
| | 318 | * <p> |
| | 319 | * The value dumps all byte values stored in the given array. |
| | 320 | * <p> |
| | 321 | * This method is useful to dump the content of the array in a tracing |
| | 322 | * method, e.g.: |
| | 323 | * <pre> |
| | 324 | * logger.entering(CLASSNAME, "foo(byte[])", ArraysUtil.toString(array)); |
| | 325 | * </pre> |
| | 326 | * @param array The array whose string representation to return. |
| | 327 | * @return A string representation of <tt>array</tt>. |
| | 328 | */ |
| | 329 | public static String toString (byte[] array) |
| | 330 | { |
| | 331 | final String result; |
| | 332 | |
| | 333 | if (array == null) |
| | 334 | { |
| | 335 | result = "null"; |
| | 336 | } |
| | 337 | else if (array.length == 0) |
| | 338 | { |
| | 339 | result = "[]"; |
| | 340 | } |
| | 341 | else |
| | 342 | { |
| | 343 | final StringBuffer buf = new StringBuffer(); |
| | 344 | for (int i = 0; i < array.length; i++) |
| | 345 | { |
| | 346 | if (i == 0) |
| | 347 | { |
| | 348 | buf.append('['); |
| | 349 | } |
| | 350 | else |
| | 351 | { |
| | 352 | buf.append(", "); |
| | 353 | } |
| | 354 | buf.append(String.valueOf(array[i])); |
| | 355 | } |
| | 356 | buf.append(']'); |
| | 357 | result = buf.toString(); |
| | 358 | } |
| | 359 | return result; |
| | 360 | } |
| | 361 | |
| | 362 | /** |
| | 363 | * Returns a string representation of the contents of the specified array. |
| | 364 | * <p> |
| | 365 | * The value dumps all char values stored in the given array. |
| | 366 | * <p> |
| | 367 | * This method is useful to dump the content of the array in a tracing |
| | 368 | * method, e.g.: |
| | 369 | * <pre> |
| | 370 | * logger.entering(CLASSNAME, "foo(char[])", ArraysUtil.toString(array)); |
| | 371 | * </pre> |
| | 372 | * @param array The array whose string representation to return. |
| | 373 | * @return A string representation of <tt>array</tt>. |
| | 374 | */ |
| | 375 | public static String toString (char[] array) |
| | 376 | { |
| | 377 | final String result; |
| | 378 | |
| | 379 | if (array == null) |
| | 380 | { |
| | 381 | result = "null"; |
| | 382 | } |
| | 383 | else if (array.length == 0) |
| | 384 | { |
| | 385 | result = "[]"; |
| | 386 | } |
| | 387 | else |
| | 388 | { |
| | 389 | final StringBuffer buf = new StringBuffer(); |
| | 390 | for (int i = 0; i < array.length; i++) |
| | 391 | { |
| | 392 | if (i == 0) |
| | 393 | { |
| | 394 | buf.append('['); |
| | 395 | } |
| | 396 | else |
| | 397 | { |
| | 398 | buf.append(", "); |
| | 399 | } |
| | 400 | buf.append(String.valueOf(array[i])); |
| | 401 | } |
| | 402 | buf.append(']'); |
| | 403 | result = buf.toString(); |
| | 404 | } |
| | 405 | return result; |
| | 406 | } |
| | 407 | |
| | 408 | /** |
| | 409 | * Returns a string representation of the contents of the specified array. |
| | 410 | * <p> |
| | 411 | * The value dumps all boolean values stored in the given array. |
| | 412 | * <p> |
| | 413 | * This method is useful to dump the content of the array in a tracing |
| | 414 | * method, e.g.: |
| | 415 | * <pre> |
| | 416 | * logger.entering(CLASSNAME, "foo(boolean[])", ArraysUtil.toString(array)); |
| | 417 | * </pre> |
| | 418 | * @param array The array whose string representation to return. |
| | 419 | * @return A string representation of <tt>array</tt>. |
| | 420 | */ |
| | 421 | public static String toString (boolean[] array) |
| | 422 | { |
| | 423 | final String result; |
| | 424 | |
| | 425 | if (array == null) |
| | 426 | { |
| | 427 | result = "null"; |
| | 428 | } |
| | 429 | else if (array.length == 0) |
| | 430 | { |
| | 431 | result = "[]"; |
| | 432 | } |
| | 433 | else |
| | 434 | { |
| | 435 | final StringBuffer buf = new StringBuffer(); |
| | 436 | for (int i = 0; i < array.length; i++) |
| | 437 | { |
| | 438 | if (i == 0) |
| | 439 | { |
| | 440 | buf.append('['); |
| | 441 | } |
| | 442 | else |
| | 443 | { |
| | 444 | buf.append(", "); |
| | 445 | } |
| | 446 | buf.append(String.valueOf(array[i])); |
| | 447 | } |
| | 448 | buf.append(']'); |
| | 449 | result = buf.toString(); |
| | 450 | } |
| | 451 | return result; |
| | 452 | } |
| | 453 | |
| | 454 | |