litesoft
@ 811
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
package org.litesoft.core.typeutils; import org.litesoft.core.util.*; public class Objects { public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; public static void assertNull( String pObjectName, Object pToBeAssert ) throws IllegalArgumentException { if ( pToBeAssert != null ) { throw new IllegalArgumentException( pObjectName + ": Expected null, but was'" + pToBeAssert + "'" ); } } public static <T> T assertNotNull( String pObjectName, T pToBeAssert ) throws IllegalArgumentException { if ( pToBeAssert == null ) { throw new IllegalArgumentException( pObjectName + UtilsCommon.NOT_ALLOWED_TO_BE_NULL ); } return pToBeAssert; } public static void assertEqual( String pObjectName, Object pExpected, Object pActual ) throws IllegalArgumentException { if ( null == pExpected && null == pActual ) { return; } if ( null != pExpected && !pExpected.equals( pActual ) ) { throw new IllegalArgumentException( pObjectName + ": Expected '" + pExpected + "', but was '" + pActual + "'" ); } } public static boolean areNonArraysEqual( Object pThis, Object pThat ) { if ( pThis == pThat ) // Same or both null { return true; } // Both CAN'T be null return (pThis != null) ? pThis.equals( pThat ) : pThat.equals( pThis ); } public static <T> T deNull( T pToCheck, T pDefault ) { return (pToCheck != null) ? pToCheck : pDefault; } public static Object[] appendObjectArrays( Object[] pArray1, Object[] pArray2 ) { if ( isNullOrEmpty( pArray2 ) ) { return pArray1; } if ( isNullOrEmpty( pArray1 ) ) { return pArray2; } Object[] joined = new Object[pArray1.length + pArray2.length]; System.arraycopy( pArray1, 0, joined, 0, pArray1.length ); System.arraycopy( pArray2, 0, joined, pArray1.length, pArray2.length ); return joined; } public static Object[] prependObject( Object pNewFirst, Object[] pTheRest ) { return appendObjectArrays( new Object[]{pNewFirst}, pTheRest ); } public static Object[] appendObject( Object[] pCurArray, Object pNewLast ) { return appendObjectArrays( pCurArray, new Object[]{pNewLast} ); } public static boolean isNotNullOrEmpty( Object[] pArrayToCheck ) { return ((pArrayToCheck != null) && (pArrayToCheck.length != 0)); } public static boolean isNullOrEmpty( Object[] pArrayToCheck ) { return ((pArrayToCheck == null) || (pArrayToCheck.length == 0)); } public static boolean areArraysEqual( Object[] pThis, Object[] pThat ) { if ( pThis == pThat ) // handles if both are null { return true; } if ( (pThis != null) && (pThat != null) && (pThis.length == pThat.length) ) { for ( int i = pThis.length; --i >= 0; ) { if ( !UtilsCommon.areEqual( pThis[i], pThat[i] ) ) { return false; } } return true; } return false; } private static void assertElementsNotNull( String pErrorMessage, Object[] pArrayToAssert ) throws IllegalArgumentException { for ( int i = pArrayToAssert.length; --i >= 0; ) { if ( pArrayToAssert[i] == null ) { Strings.errorNullOrEmpty( pErrorMessage, "Object[" + i + "]" ); } } } public static void assertNotNullAndElementsNotNull( String pErrorMessage, Object[] pArrayToAssert ) throws IllegalArgumentException { if ( pArrayToAssert == null ) { Strings.errorNullOrEmpty( pErrorMessage, "Object[]" ); } assertElementsNotNull( pErrorMessage, pArrayToAssert ); } public static void assertNotNullNotEmptyAndElementsNotNull( String pErrorMessage, Object[] pArrayToAssert ) throws IllegalArgumentException { assertNotNullNotEmpty( pErrorMessage, pArrayToAssert ); assertElementsNotNull( pErrorMessage, pArrayToAssert ); } public static void assertNotNullNotEmpty( String pErrorMessage, Object[] pArrayToAssert ) throws IllegalArgumentException { if ( isNullOrEmpty( pArrayToAssert ) ) { Strings.errorNullOrEmpty( pErrorMessage, "Object[]" ); } } public static boolean isNotNull( Object pToCheck ) { return (pToCheck != null); } public static int getNonNullEntryCount( Object[] pArrayToCheck ) { int rv = 0; if ( pArrayToCheck != null ) { for ( int i = pArrayToCheck.length; --i >= 0; ) { if ( pArrayToCheck[i] != null ) { rv++; } } } return rv; } public static boolean hasEntries( Object[] pArrayToCheck ) { if ( pArrayToCheck != null ) { for ( int i = pArrayToCheck.length; --i >= 0; ) { if ( pArrayToCheck[i] != null ) { return true; } } } return false; } /** * This method strips the package name off a fully qualified class name returning just the substring * beginning one character beyond the last ".". * * @return the substring beginning one character beyond the last "."; null or no "." just returns pFullyQualifiedClassName */ public static String justClassName( String pFullyQualifiedClassName ) { int zAt = (pFullyQualifiedClassName != null) ? pFullyQualifiedClassName.lastIndexOf( '.' ) : -1; return (zAt != -1) ? pFullyQualifiedClassName.substring( zAt + 1 ) : pFullyQualifiedClassName; } /** * This method strips the package name off a fully qualified class name returning just the substring * beginning one character beyond the last ".". * * @return the substring beginning one character beyond the last "."; null or no "." just returns */ public static String justClassName( Class<?> pClass ) { return (pClass != null) ? justClassName( pClass.getName() ) : null; } /** * This method strips the package name off the class name of the pObject returning just the substring * beginning one character beyond the last ".". * * @see Objects#justClassName(Class */ public static String justClassNameOf( Object pObject ) { return (pObject != null) ? justClassName( pObject.getClass() ) : null; } /** * This method strips the package name off the class name of the pObject returning just the substring * beginning one character beyond the last ".", and if it ends with "Impl" remove that. * * @see Objects#justClassName(Class */ public static String justClassNameNoImplOf( Object pObject ) { String zName = justClassNameOf( pObject ); return ((zName != null) && zName.endsWith( "Impl" )) ? zName.substring( 0, zName.length() - 4 ) : zName; } public static String justClassNameIfPackage( String pClassName, String pPackage ) { if ( pClassName != null ) { if ( (pPackage != null) && pPackage.endsWith( "." ) && (pPackage.length() < pClassName.length()) ) { String zSimpleName = justClassName( pClassName ); if ( pClassName.equals( pPackage + zSimpleName ) ) { return zSimpleName; } } } return pClassName; } public static String justClassNameIfPackage( Class<?> pClass, String pPackage ) { return (pClass != null) ? justClassNameIfPackage( pClass.getName(), pPackage ) : null; } /** * This method strips the package name off a fully qualified class name returning just the substring * beginning one character beyond the last "." (And removes the wrapping Class names if Any). * * @return the substring beginning one character beyond the last "$"; null or no "$" just returns justClassName( pFullyQualifiedClassName ) */ public static String justSimpleName( String pFullyQualifiedClassName ) { int zAt = (pFullyQualifiedClassName != null) ? pFullyQualifiedClassName.lastIndexOf( '$' ) : -1; return (zAt != -1) ? pFullyQualifiedClassName.substring( zAt + 1 ) : justClassName( pFullyQualifiedClassName ); } /** * This method strips the package name off the fully qualified class name of the pObject returning just the substring * beginning one character beyond the last "." (And removes the wrapping Class names if Any). * * @return the substring beginning one character beyond the last "$"; null or no "$" just returns justClassName( pFullyQualifiedClassName ) */ public static String justSimpleName( Class pClass ) { return justSimpleName( (pClass != null) ? pClass.getName() : null ); } /** * This method strips the package name off the fully qualified class name of the pObject returning just the substring * beginning one character beyond the last "." (And removes the wrapping Class names if Any). * * @return the substring beginning one character beyond the last "$"; null or no "$" just returns justClassName( pFullyQualifiedClassName ) */ public static String justSimpleName( Object pObject ) { return justSimpleName( (pObject != null) ? pObject.getClass() : null ); } public static String classNameOf( Object pObject ) { return (pObject != null) ? pObject.getClass().getName() : null; } /** * @param pCount - < 1 means copy nothing * @param pTo - is NOT extended * @param pFromIndex - 0 based, < 0 indicates from end (eg -1 == Last) * @param pToIndex - 0 based, < 0 indicates from end (eg -1 == Last) * * @return pTo */ public static Object[] copySubArrayTo( int pCount, // Object[] pFrom, int pFromIndex, // Object[] pTo, int pToIndex ) { if ( (pCount > 0) && // isNotNullOrEmpty( pFrom ) && (pFromIndex < pFrom.length) && // isNotNullOrEmpty( pTo ) && (pToIndex < pTo.length) ) { pCount = lesserOf( pCount, pTo, pToIndex = unNegateIndex( pTo, pToIndex ) ); pCount = lesserOf( pCount, pFrom, pFromIndex = unNegateIndex( pFrom, pFromIndex ) ); while ( pCount-- > 0 ) { pTo[pToIndex++] = pFrom[pFromIndex++]; } } return pTo; } private static int lesserOf( int pCount, Object[] pObjects, int pIndex ) { if ( pCount > 0 ) { int zMoveLen = pObjects.length - pIndex; if ( zMoveLen < pCount ) { return zMoveLen; } } return pCount; } private static int unNegateIndex( Object[] pFrom, int pFromIndex ) { if ( pFromIndex < 0 ) { if ( (pFromIndex = pFrom.length - pFromIndex) < 0 ) { pFromIndex = 0; } } return pFromIndex; } /** * @param pTo - is NOT extended * @param pToIndex - 0 based, < 0 indicates from end (eg -1 == Last) * * @return pTo */ public static Object[] copyArrayTo( Object[] pFrom, Object[] pTo, int pToIndex ) { if ( (pFrom != null) && (pTo != null) ) { copySubArrayTo( Math.min( pFrom.length, pTo.length - pToIndex ), pFrom, 0, pTo, pToIndex ); } return pTo; } /** * @param pTo - is NOT extended * * @return pTo */ public static Object[] copyArrayTo( Object[] pFrom, Object[] pTo ) { if ( (pFrom != null) && (pTo != null) ) { copySubArrayTo( Math.min( pFrom.length, pTo.length ), pFrom, 0, pTo, 0 ); } return pTo; } } |