001package org.unix4j.util; 002 003import java.util.Arrays; 004 005/** 006 * Utility class with static methods for arrays. 007 */ 008public class ArrayUtil { 009 010 /** 011 * Returns a (deep) string representation for the given array or throws an 012 * exception if {@code array} is not an array or null. 013 * <p> 014 * The method returns {@code "null"} if the given {@code array} is null. 015 * 016 * @param array 017 * the array to return as a string 018 * @return a (deep) string representation of the array 019 * @throws IllegalArgumentException 020 * if {@code array} is not an array 021 * @see Arrays#deepToString(Object[]) 022 */ 023 public static String toString(Object array) { 024 if (array != null) { 025 if (array instanceof Object[]) { 026 return Arrays.deepToString((Object[]) array); 027 } 028 if (!array.getClass().isArray()) { 029 throw new IllegalArgumentException(array.getClass().getName() + " is not an array: " + array); 030 } 031 final String s = Arrays.deepToString(new Object[] { array }); 032 return s.substring(1, s.length() - 1);// cut off leading [ and 033 // trailing ] 034 } 035 return String.valueOf(array); 036 } 037 038 // no instances 039 private ArrayUtil() { 040 super(); 041 } 042 043 //TODO BJW Write unit tests 044 public static String join(final String[] array, final String delim){ 045 final StringBuilder sb = new StringBuilder(); 046 for(final String element: array){ 047 if(sb.length() > 0) sb.append(delim); 048 sb.append(element); 049 } 050 return sb.toString(); 051 } 052}