|
@@ -1,6 +1,6 @@ |
1 |
1 |
|
package org.litesoft.core.typeutils; |
2 |
2 |
|
|
3 |
|
- |
import org.litesoft.core.util.*; |
|
3 |
+ |
import java.util.*; |
4 |
4 |
|
|
5 |
5 |
|
public class Objects |
6 |
6 |
|
{ |
|
@@ -100,7 +100,7 @@ |
100 |
100 |
|
{ |
101 |
101 |
|
for ( int i = pThis.length; --i >= 0; ) |
102 |
102 |
|
{ |
103 |
|
- |
if ( !UtilsCommon.areEqual( pThis[i], pThat[i] ) ) |
|
103 |
+ |
if ( !areEqual( pThis[i], pThat[i] ) ) |
104 |
104 |
|
{ |
105 |
105 |
|
return false; |
106 |
106 |
|
} |
|
@@ -369,4 +369,239 @@ |
369 |
369 |
|
} |
370 |
370 |
|
return pTo; |
371 |
371 |
|
} |
|
372 |
+ |
|
|
373 |
+ |
public static boolean isEmptyString( Object pObject ) |
|
374 |
+ |
{ |
|
375 |
+ |
return pObject == null || ((pObject instanceof String) && Strings.isBlank( (String) pObject )); |
|
376 |
+ |
} |
|
377 |
+ |
|
|
378 |
+ |
public static boolean areBothEmptyStrings( Object pThis, Object pThat ) |
|
379 |
+ |
{ |
|
380 |
+ |
return isEmptyString( pThis ) && isEmptyString( pThat ); |
|
381 |
+ |
} |
|
382 |
+ |
|
|
383 |
+ |
public static boolean areBothBooleanNotTrue( Object pThis, Object pThat ) |
|
384 |
+ |
{ |
|
385 |
+ |
return Booleans.isBooleanNotTrue( pThis ) && Booleans.isBooleanNotTrue( pThat ); |
|
386 |
+ |
} |
|
387 |
+ |
|
|
388 |
+ |
public static boolean isNullEquivalent( Object pObject ) |
|
389 |
+ |
{ |
|
390 |
+ |
return isEmptyString( pObject ) || Booleans.isBooleanNotTrue( pObject ); |
|
391 |
+ |
} |
|
392 |
+ |
|
|
393 |
+ |
public static boolean areBothNullEquivalent( Object pThis, Object pThat ) |
|
394 |
+ |
{ |
|
395 |
+ |
return isNullEquivalent( pThis ) && isNullEquivalent( pThat ); |
|
396 |
+ |
} |
|
397 |
+ |
|
|
398 |
+ |
public static boolean areEqual( Object pThis, Object pThat ) |
|
399 |
+ |
{ |
|
400 |
+ |
if ( areNonArraysEqual( pThis, pThat ) ) |
|
401 |
+ |
{ |
|
402 |
+ |
return true; |
|
403 |
+ |
} |
|
404 |
+ |
// Both CAN'T be null |
|
405 |
+ |
if ( (pThis instanceof Object[]) && (pThat instanceof Object[]) ) |
|
406 |
+ |
{ |
|
407 |
+ |
return areArraysEqual( (Object[]) pThis, (Object[]) pThat ); |
|
408 |
+ |
} |
|
409 |
+ |
return (pThis instanceof int[]) && (pThat instanceof int[]) && Integers.areArraysEqual( (int[]) pThis, (int[]) pThat ); |
|
410 |
+ |
} |
|
411 |
+ |
|
|
412 |
+ |
public static String deNullToString( Object value, Object defaultValue ) |
|
413 |
+ |
{ |
|
414 |
+ |
return Strings.deNull( noEmptyToString( value ), noEmptyToString( defaultValue ) ); |
|
415 |
+ |
} |
|
416 |
+ |
|
|
417 |
+ |
public static String noEmptyToString( Object value ) |
|
418 |
+ |
{ |
|
419 |
+ |
return Strings.noEmpty( Strings.nullOKtoString( value ) ); |
|
420 |
+ |
} |
|
421 |
+ |
|
|
422 |
+ |
public static String combine( String pSeparator, List<?> pObjects ) |
|
423 |
+ |
{ |
|
424 |
+ |
return combine( pSeparator, pObjects.toArray() ); |
|
425 |
+ |
} |
|
426 |
+ |
|
|
427 |
+ |
public static String combine( String pSeparator, Object... pObjects ) |
|
428 |
+ |
{ |
|
429 |
+ |
if ( isNullOrEmpty( pObjects ) ) |
|
430 |
+ |
{ |
|
431 |
+ |
return null; |
|
432 |
+ |
} |
|
433 |
+ |
StringBuilder sb = new StringBuilder(); |
|
434 |
+ |
|
|
435 |
+ |
sb.append( pObjects[0] ); |
|
436 |
+ |
|
|
437 |
+ |
for ( int i = 1; i < pObjects.length; i++ ) |
|
438 |
+ |
{ |
|
439 |
+ |
sb.append( pSeparator ); |
|
440 |
+ |
sb.append( pObjects[i] ); |
|
441 |
+ |
} |
|
442 |
+ |
return sb.toString(); |
|
443 |
+ |
} |
|
444 |
+ |
|
|
445 |
+ |
/** |
|
446 |
+ |
* Assert that "value" is not null or when converted to a String is not |
|
447 |
+ |
* empty and then return resulting "value" trimmed. |
|
448 |
+ |
* |
|
449 |
+ |
* @param what "field" was the problem on |
|
450 |
+ |
* @param value to check |
|
451 |
+ |
* |
|
452 |
+ |
* @return value converted to a String minus any leading and trailing spaces |
|
453 |
+ |
*/ |
|
454 |
+ |
public static String assertNoEmptyToString( String what, Object value ) |
|
455 |
+ |
{ |
|
456 |
+ |
String strValue = noEmptyToString( value ); |
|
457 |
+ |
if ( strValue == null ) |
|
458 |
+ |
{ |
|
459 |
+ |
Strings.errorNullOrEmpty( what, String.valueOf( justClassNameOf( value ) ) ); |
|
460 |
+ |
} |
|
461 |
+ |
return strValue; |
|
462 |
+ |
} |
|
463 |
+ |
|
|
464 |
+ |
/** |
|
465 |
+ |
* Assert that "value" is not null or empty (if it is a String) and return |
|
466 |
+ |
* "value" trimmed (if it is a String). |
|
467 |
+ |
* |
|
468 |
+ |
* @param what "field" was the problem on |
|
469 |
+ |
* @param value to check |
|
470 |
+ |
* |
|
471 |
+ |
* @return value minus any leading and trailing spaces |
|
472 |
+ |
*/ |
|
473 |
+ |
public static <T> T assertNotNullOrEmpty( String what, T value ) |
|
474 |
+ |
{ |
|
475 |
+ |
if ( value == null ) |
|
476 |
+ |
{ |
|
477 |
+ |
Strings.errorNullOrEmpty( what, String.valueOf( justClassNameOf( value ) ) ); |
|
478 |
+ |
} |
|
479 |
+ |
if ( value instanceof String ) |
|
480 |
+ |
{ |
|
481 |
+ |
value = Cast.it( Strings.assertNotNullNotEmpty( what, value.toString() ) ); |
|
482 |
+ |
} |
|
483 |
+ |
return value; |
|
484 |
+ |
} |
|
485 |
+ |
|
|
486 |
+ |
public static String toStringNullToEmpty( Object pObject ) |
|
487 |
+ |
{ |
|
488 |
+ |
if ( pObject != null ) |
|
489 |
+ |
{ |
|
490 |
+ |
String rv = pObject.toString(); |
|
491 |
+ |
if ( rv != null ) |
|
492 |
+ |
{ |
|
493 |
+ |
return rv; |
|
494 |
+ |
} |
|
495 |
+ |
} |
|
496 |
+ |
return ""; |
|
497 |
+ |
} |
|
498 |
+ |
|
|
499 |
+ |
public static String toStringOrNull( Object pObject ) |
|
500 |
+ |
{ |
|
501 |
+ |
return Strings.noEmpty( (pObject != null) ? pObject.toString() : null ); |
|
502 |
+ |
} |
|
503 |
+ |
|
|
504 |
+ |
public static String toString( Object pObject ) |
|
505 |
+ |
{ |
|
506 |
+ |
if ( pObject != null ) |
|
507 |
+ |
{ |
|
508 |
+ |
String rv = pObject.toString(); |
|
509 |
+ |
if ( rv != null ) |
|
510 |
+ |
{ |
|
511 |
+ |
return rv; |
|
512 |
+ |
} |
|
513 |
+ |
} |
|
514 |
+ |
return null; |
|
515 |
+ |
} |
|
516 |
+ |
|
|
517 |
+ |
public static boolean isOneOf( Object pToFind, Object[] pToSearch ) |
|
518 |
+ |
{ |
|
519 |
+ |
if ( pToSearch != null ) |
|
520 |
+ |
{ |
|
521 |
+ |
for ( int i = pToSearch.length; --i >= 0; ) |
|
522 |
+ |
{ |
|
523 |
+ |
if ( areEqual( pToFind, pToSearch[i] ) ) |
|
524 |
+ |
{ |
|
525 |
+ |
return true; |
|
526 |
+ |
} |
|
527 |
+ |
} |
|
528 |
+ |
} |
|
529 |
+ |
return false; |
|
530 |
+ |
} |
|
531 |
+ |
|
|
532 |
+ |
public static String optionsToString( Object[] pObjects ) |
|
533 |
+ |
{ |
|
534 |
+ |
if ( pObjects == null ) |
|
535 |
+ |
{ |
|
536 |
+ |
return "No Options Available"; |
|
537 |
+ |
} |
|
538 |
+ |
StringBuilder sb = new StringBuilder( "Valid Options are" ); |
|
539 |
+ |
String prefix = ": "; |
|
540 |
+ |
//noinspection ForLoopReplaceableByForEach |
|
541 |
+ |
for ( int i = 0; i < pObjects.length; i++ ) |
|
542 |
+ |
{ |
|
543 |
+ |
sb.append( prefix ).append( pObjects[i] ); |
|
544 |
+ |
prefix = ", "; |
|
545 |
+ |
} |
|
546 |
+ |
return sb.toString(); |
|
547 |
+ |
} |
|
548 |
+ |
|
|
549 |
+ |
public static String toString( Object[] pObjects ) |
|
550 |
+ |
{ |
|
551 |
+ |
if ( pObjects == null ) |
|
552 |
+ |
{ |
|
553 |
+ |
return "null"; |
|
554 |
+ |
} |
|
555 |
+ |
return new StringBuilder().append( '<' ).append( pObjects.length ).append( ':' ).append( toString( pObjects, "," ) ).append( '>' ).toString(); |
|
556 |
+ |
} |
|
557 |
+ |
|
|
558 |
+ |
public static String toString( Object[] pObjects, String separator ) |
|
559 |
+ |
{ |
|
560 |
+ |
if ( pObjects == null ) |
|
561 |
+ |
{ |
|
562 |
+ |
return "null"; |
|
563 |
+ |
} |
|
564 |
+ |
StringBuilder sb = new StringBuilder(); |
|
565 |
+ |
if ( pObjects.length > 0 ) |
|
566 |
+ |
{ |
|
567 |
+ |
sb.append( pObjects[0] ); |
|
568 |
+ |
//noinspection ForLoopReplaceableByForEach |
|
569 |
+ |
for ( int i = 1; i < pObjects.length; i++ ) |
|
570 |
+ |
{ |
|
571 |
+ |
sb.append( separator ).append( pObjects[i] ); |
|
572 |
+ |
} |
|
573 |
+ |
} |
|
574 |
+ |
return sb.toString(); |
|
575 |
+ |
} |
|
576 |
+ |
|
|
577 |
+ |
public static String padIt( int pMinDesiredLength, Object pIt ) |
|
578 |
+ |
{ |
|
579 |
+ |
return Strings.padIt( pMinDesiredLength, "" + pIt ); |
|
580 |
+ |
} |
|
581 |
+ |
|
|
582 |
+ |
public static String iTpad( Object pIt, int pMinDesiredLength ) |
|
583 |
+ |
{ |
|
584 |
+ |
return Strings.iTpad( "" + pIt, pMinDesiredLength ); |
|
585 |
+ |
} |
|
586 |
+ |
|
|
587 |
+ |
public static boolean areNotNull( Object... pToChecks ) |
|
588 |
+ |
{ |
|
589 |
+ |
if ( pToChecks == null ) |
|
590 |
+ |
{ |
|
591 |
+ |
return false; |
|
592 |
+ |
} |
|
593 |
+ |
for ( Object toCheck : pToChecks ) |
|
594 |
+ |
{ |
|
595 |
+ |
if ( toCheck == null ) |
|
596 |
+ |
{ |
|
597 |
+ |
return false; |
|
598 |
+ |
} |
|
599 |
+ |
} |
|
600 |
+ |
return true; |
|
601 |
+ |
} |
|
602 |
+ |
|
|
603 |
+ |
public static boolean hasText( Object pValue ) |
|
604 |
+ |
{ |
|
605 |
+ |
return (pValue != null) && (pValue.toString().trim().length() != 0); |
|
606 |
+ |
} |
372 |
607 |
|
} |