1 23 package com.sun.appserv.management.util.misc; 24 25 import java.lang.reflect.Array ; 26 27 31 public final class ArrayUtil 32 { 33 private 34 ArrayUtil( ) 35 { 36 } 38 39 public static boolean 40 arraysEqual( final Object array1, final Object array2 ) 41 { 42 boolean equal = array1 == array2; 43 44 if ( equal ) 45 { 46 return( true ); 48 } 49 else if ( array1 == null || array2 == null) 50 { 51 return( false ); 52 } 53 54 55 if ( array1.getClass() == array2.getClass() && 56 ClassUtil.objectIsArray( array1 ) && 57 Array.getLength( array1 ) == Array.getLength( array2 ) ) 58 { 59 equal = true; 60 final int length = Array.getLength( array1 ); 61 62 for( int i = 0; i < length; ++ i ) 63 { 64 final Object a1 = Array.get( array1, i ); 65 final Object a2 = Array.get( array2, i ); 66 67 if ( a1 != a2) 68 { 69 if ( a1 == null || a2 == null ) 70 { 71 equal = false; 72 } 73 else if ( ClassUtil.objectIsArray( a1 ) ) 74 { 75 if ( ! arraysEqual( a1, a2 ) ) 76 { 77 equal = false; 78 } 79 } 80 } 81 82 if ( ! equal ) 83 break; 84 } 85 } 86 87 return( equal ); 88 } 89 90 public static boolean 91 arrayContainsNulls( final Object [] array ) 92 { 93 boolean containsNulls = false; 94 95 for( int i = 0; i < array.length; ++i ) 96 { 97 if ( array[ i ] == null ) 98 { 99 containsNulls = true; 100 break; 101 } 102 } 103 104 return( containsNulls ); 105 } 106 107 108 public static <T> T[] 109 newArray( final Class <T> theClass, int numItems ) 110 { 111 return (T[])( Array.newInstance( theClass, numItems ) ); 112 } 113 114 122 public static <T> T[] 123 newArray( 124 final T[] items, 125 final int startIndex, 126 final int numItems ) 127 { 128 final Class <T> theClass = TypeCast.asClass( items.getClass().getComponentType() ); 129 130 final T[] result = newArray( theClass, numItems ); 131 System.arraycopy( items, startIndex, result, 0, numItems ); 132 133 return( result ); 134 } 135 136 143 public static <T> T[] 144 newArray( 145 final T[] items1, 146 final T[] items2) 147 { 148 final Class <T> class1 = TypeCast.asClass( items1.getClass().getComponentType() ); 149 final Class <T> class2 = TypeCast.asClass( items2.getClass().getComponentType() ); 150 151 if ( ! class1.isAssignableFrom( class2 ) ) 152 { 153 throw new IllegalArgumentException (); 154 } 155 156 final int length1 = Array.getLength( items1 ); 157 final int length2 = Array.getLength( items2 ); 158 final T[] result = newArray( class1, length1 + length2 ); 159 System.arraycopy( items1, 0, result, 0, length1 ); 160 System.arraycopy( items2, 0, result, length1, length2 ); 161 162 return( result ); 163 } 164 165 172 public static <T> T[] 173 newArray( 174 final T[] items, 175 final T item) 176 { 177 final Class <T> theClass = TypeCast.asClass( items.getClass().getComponentType() ); 178 final T[] result = newArray( theClass, items.length + 1 ); 179 System.arraycopy( items, 0, result, 0, items.length ); 180 181 result[ result.length - 1 ] = item; 182 return( result ); 183 } 184 } 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | Popular Tags |