1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.argeo.osgi.boot.internal.springutil;
18
19 import java.lang.reflect.Array;
20 import java.util.Arrays;
21
22
23
24
25
26
27
28
29
30
31
32
33
34 @SuppressWarnings({ "rawtypes", "unchecked" })
35 public abstract class ObjectUtils {
36
37 private static final int INITIAL_HASH = 7;
38 private static final int MULTIPLIER = 31;
39
40 private static final String EMPTY_STRING = "";
41 private static final String NULL_STRING = "null";
42 private static final String ARRAY_START = "{";
43 private static final String ARRAY_END = "}";
44 private static final String EMPTY_ARRAY = ARRAY_START + ARRAY_END;
45 private static final String ARRAY_ELEMENT_SEPARATOR = ", ";
46
47
48
49
50
51
52
53
54
55
56
57 public static boolean isCheckedException(Throwable ex) {
58 return !(ex instanceof RuntimeException || ex instanceof Error);
59 }
60
61
62
63
64
65
66
67
68 public static boolean isCompatibleWithThrowsClause(Throwable ex, Class[] declaredExceptions) {
69 if (!isCheckedException(ex)) {
70 return true;
71 }
72 if (declaredExceptions != null) {
73 for (int i = 0; i < declaredExceptions.length; i++) {
74 if (declaredExceptions[i].isAssignableFrom(ex.getClass())) {
75 return true;
76 }
77 }
78 }
79 return false;
80 }
81
82
83
84
85
86
87
88 public static boolean isEmpty(Object[] array) {
89 return (array == null || array.length == 0);
90 }
91
92
93
94
95
96
97
98
99 public static boolean containsElement(Object[] array, Object element) {
100 if (array == null) {
101 return false;
102 }
103 for (int i = 0; i < array.length; i++) {
104 if (nullSafeEquals(array[i], element)) {
105 return true;
106 }
107 }
108 return false;
109 }
110
111
112
113
114
115
116
117
118 public static Object[] addObjectToArray(Object[] array, Object obj) {
119 Class compType = Object.class;
120 if (array != null) {
121 compType = array.getClass().getComponentType();
122 }
123 else if (obj != null) {
124 compType = obj.getClass();
125 }
126 int newArrLength = (array != null ? array.length + 1 : 1);
127 Object[] newArr = (Object[]) Array.newInstance(compType, newArrLength);
128 if (array != null) {
129 System.arraycopy(array, 0, newArr, 0, array.length);
130 }
131 newArr[newArr.length - 1] = obj;
132 return newArr;
133 }
134
135
136
137
138
139
140
141
142
143
144 public static Object[] toObjectArray(Object source) {
145 if (source instanceof Object[]) {
146 return (Object[]) source;
147 }
148 if (source == null) {
149 return new Object[0];
150 }
151 if (!source.getClass().isArray()) {
152 throw new IllegalArgumentException("Source is not an array: " + source);
153 }
154 int length = Array.getLength(source);
155 if (length == 0) {
156 return new Object[0];
157 }
158 Class wrapperType = Array.get(source, 0).getClass();
159 Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);
160 for (int i = 0; i < length; i++) {
161 newArray[i] = Array.get(source, i);
162 }
163 return newArray;
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 public static boolean nullSafeEquals(Object o1, Object o2) {
183 if (o1 == o2) {
184 return true;
185 }
186 if (o1 == null || o2 == null) {
187 return false;
188 }
189 if (o1.equals(o2)) {
190 return true;
191 }
192 if (o1.getClass().isArray() && o2.getClass().isArray()) {
193 if (o1 instanceof Object[] && o2 instanceof Object[]) {
194 return Arrays.equals((Object[]) o1, (Object[]) o2);
195 }
196 if (o1 instanceof boolean[] && o2 instanceof boolean[]) {
197 return Arrays.equals((boolean[]) o1, (boolean[]) o2);
198 }
199 if (o1 instanceof byte[] && o2 instanceof byte[]) {
200 return Arrays.equals((byte[]) o1, (byte[]) o2);
201 }
202 if (o1 instanceof char[] && o2 instanceof char[]) {
203 return Arrays.equals((char[]) o1, (char[]) o2);
204 }
205 if (o1 instanceof double[] && o2 instanceof double[]) {
206 return Arrays.equals((double[]) o1, (double[]) o2);
207 }
208 if (o1 instanceof float[] && o2 instanceof float[]) {
209 return Arrays.equals((float[]) o1, (float[]) o2);
210 }
211 if (o1 instanceof int[] && o2 instanceof int[]) {
212 return Arrays.equals((int[]) o1, (int[]) o2);
213 }
214 if (o1 instanceof long[] && o2 instanceof long[]) {
215 return Arrays.equals((long[]) o1, (long[]) o2);
216 }
217 if (o1 instanceof short[] && o2 instanceof short[]) {
218 return Arrays.equals((short[]) o1, (short[]) o2);
219 }
220 }
221 return false;
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240 public static int nullSafeHashCode(Object obj) {
241 if (obj == null) {
242 return 0;
243 }
244 if (obj.getClass().isArray()) {
245 if (obj instanceof Object[]) {
246 return nullSafeHashCode((Object[]) obj);
247 }
248 if (obj instanceof boolean[]) {
249 return nullSafeHashCode((boolean[]) obj);
250 }
251 if (obj instanceof byte[]) {
252 return nullSafeHashCode((byte[]) obj);
253 }
254 if (obj instanceof char[]) {
255 return nullSafeHashCode((char[]) obj);
256 }
257 if (obj instanceof double[]) {
258 return nullSafeHashCode((double[]) obj);
259 }
260 if (obj instanceof float[]) {
261 return nullSafeHashCode((float[]) obj);
262 }
263 if (obj instanceof int[]) {
264 return nullSafeHashCode((int[]) obj);
265 }
266 if (obj instanceof long[]) {
267 return nullSafeHashCode((long[]) obj);
268 }
269 if (obj instanceof short[]) {
270 return nullSafeHashCode((short[]) obj);
271 }
272 }
273 return obj.hashCode();
274 }
275
276
277
278
279
280 public static int nullSafeHashCode(Object[] array) {
281 if (array == null) {
282 return 0;
283 }
284 int hash = INITIAL_HASH;
285 int arraySize = array.length;
286 for (int i = 0; i < arraySize; i++) {
287 hash = MULTIPLIER * hash + nullSafeHashCode(array[i]);
288 }
289 return hash;
290 }
291
292
293
294
295
296 public static int nullSafeHashCode(boolean[] array) {
297 if (array == null) {
298 return 0;
299 }
300 int hash = INITIAL_HASH;
301 int arraySize = array.length;
302 for (int i = 0; i < arraySize; i++) {
303 hash = MULTIPLIER * hash + hashCode(array[i]);
304 }
305 return hash;
306 }
307
308
309
310
311
312 public static int nullSafeHashCode(byte[] array) {
313 if (array == null) {
314 return 0;
315 }
316 int hash = INITIAL_HASH;
317 int arraySize = array.length;
318 for (int i = 0; i < arraySize; i++) {
319 hash = MULTIPLIER * hash + array[i];
320 }
321 return hash;
322 }
323
324
325
326
327
328 public static int nullSafeHashCode(char[] array) {
329 if (array == null) {
330 return 0;
331 }
332 int hash = INITIAL_HASH;
333 int arraySize = array.length;
334 for (int i = 0; i < arraySize; i++) {
335 hash = MULTIPLIER * hash + array[i];
336 }
337 return hash;
338 }
339
340
341
342
343
344 public static int nullSafeHashCode(double[] array) {
345 if (array == null) {
346 return 0;
347 }
348 int hash = INITIAL_HASH;
349 int arraySize = array.length;
350 for (int i = 0; i < arraySize; i++) {
351 hash = MULTIPLIER * hash + hashCode(array[i]);
352 }
353 return hash;
354 }
355
356
357
358
359
360 public static int nullSafeHashCode(float[] array) {
361 if (array == null) {
362 return 0;
363 }
364 int hash = INITIAL_HASH;
365 int arraySize = array.length;
366 for (int i = 0; i < arraySize; i++) {
367 hash = MULTIPLIER * hash + hashCode(array[i]);
368 }
369 return hash;
370 }
371
372
373
374
375
376 public static int nullSafeHashCode(int[] array) {
377 if (array == null) {
378 return 0;
379 }
380 int hash = INITIAL_HASH;
381 int arraySize = array.length;
382 for (int i = 0; i < arraySize; i++) {
383 hash = MULTIPLIER * hash + array[i];
384 }
385 return hash;
386 }
387
388
389
390
391
392 public static int nullSafeHashCode(long[] array) {
393 if (array == null) {
394 return 0;
395 }
396 int hash = INITIAL_HASH;
397 int arraySize = array.length;
398 for (int i = 0; i < arraySize; i++) {
399 hash = MULTIPLIER * hash + hashCode(array[i]);
400 }
401 return hash;
402 }
403
404
405
406
407
408 public static int nullSafeHashCode(short[] array) {
409 if (array == null) {
410 return 0;
411 }
412 int hash = INITIAL_HASH;
413 int arraySize = array.length;
414 for (int i = 0; i < arraySize; i++) {
415 hash = MULTIPLIER * hash + array[i];
416 }
417 return hash;
418 }
419
420
421
422
423
424 public static int hashCode(boolean bool) {
425 return bool ? 1231 : 1237;
426 }
427
428
429
430
431
432 public static int hashCode(double dbl) {
433 long bits = Double.doubleToLongBits(dbl);
434 return hashCode(bits);
435 }
436
437
438
439
440
441 public static int hashCode(float flt) {
442 return Float.floatToIntBits(flt);
443 }
444
445
446
447
448
449 public static int hashCode(long lng) {
450 return (int) (lng ^ (lng >>> 32));
451 }
452
453
454
455
456
457
458
459
460
461
462
463
464 public static String identityToString(Object obj) {
465 if (obj == null) {
466 return EMPTY_STRING;
467 }
468 return obj.getClass().getName() + "@" + getIdentityHexString(obj);
469 }
470
471
472
473
474
475
476 public static String getIdentityHexString(Object obj) {
477 return Integer.toHexString(System.identityHashCode(obj));
478 }
479
480
481
482
483
484
485
486
487
488
489 public static String getDisplayString(Object obj) {
490 if (obj == null) {
491 return EMPTY_STRING;
492 }
493 return nullSafeToString(obj);
494 }
495
496
497
498
499
500
501
502 public static String nullSafeClassName(Object obj) {
503 return (obj != null ? obj.getClass().getName() : NULL_STRING);
504 }
505
506
507
508
509
510
511
512
513 public static String nullSafeToString(Object obj) {
514 if (obj == null) {
515 return NULL_STRING;
516 }
517 if (obj instanceof String) {
518 return (String) obj;
519 }
520 if (obj instanceof Object[]) {
521 return nullSafeToString((Object[]) obj);
522 }
523 if (obj instanceof boolean[]) {
524 return nullSafeToString((boolean[]) obj);
525 }
526 if (obj instanceof byte[]) {
527 return nullSafeToString((byte[]) obj);
528 }
529 if (obj instanceof char[]) {
530 return nullSafeToString((char[]) obj);
531 }
532 if (obj instanceof double[]) {
533 return nullSafeToString((double[]) obj);
534 }
535 if (obj instanceof float[]) {
536 return nullSafeToString((float[]) obj);
537 }
538 if (obj instanceof int[]) {
539 return nullSafeToString((int[]) obj);
540 }
541 if (obj instanceof long[]) {
542 return nullSafeToString((long[]) obj);
543 }
544 if (obj instanceof short[]) {
545 return nullSafeToString((short[]) obj);
546 }
547 String str = obj.toString();
548 return (str != null ? str : EMPTY_STRING);
549 }
550
551
552
553
554
555
556
557
558
559
560 public static String nullSafeToString(Object[] array) {
561 if (array == null) {
562 return NULL_STRING;
563 }
564 int length = array.length;
565 if (length == 0) {
566 return EMPTY_ARRAY;
567 }
568 StringBuffer buffer = new StringBuffer();
569 for (int i = 0; i < length; i++) {
570 if (i == 0) {
571 buffer.append(ARRAY_START);
572 }
573 else {
574 buffer.append(ARRAY_ELEMENT_SEPARATOR);
575 }
576 buffer.append(String.valueOf(array[i]));
577 }
578 buffer.append(ARRAY_END);
579 return buffer.toString();
580 }
581
582
583
584
585
586
587
588
589
590
591 public static String nullSafeToString(boolean[] array) {
592 if (array == null) {
593 return NULL_STRING;
594 }
595 int length = array.length;
596 if (length == 0) {
597 return EMPTY_ARRAY;
598 }
599 StringBuffer buffer = new StringBuffer();
600 for (int i = 0; i < length; i++) {
601 if (i == 0) {
602 buffer.append(ARRAY_START);
603 }
604 else {
605 buffer.append(ARRAY_ELEMENT_SEPARATOR);
606 }
607
608 buffer.append(array[i]);
609 }
610 buffer.append(ARRAY_END);
611 return buffer.toString();
612 }
613
614
615
616
617
618
619
620
621
622
623 public static String nullSafeToString(byte[] array) {
624 if (array == null) {
625 return NULL_STRING;
626 }
627 int length = array.length;
628 if (length == 0) {
629 return EMPTY_ARRAY;
630 }
631 StringBuffer buffer = new StringBuffer();
632 for (int i = 0; i < length; i++) {
633 if (i == 0) {
634 buffer.append(ARRAY_START);
635 }
636 else {
637 buffer.append(ARRAY_ELEMENT_SEPARATOR);
638 }
639 buffer.append(array[i]);
640 }
641 buffer.append(ARRAY_END);
642 return buffer.toString();
643 }
644
645
646
647
648
649
650
651
652
653
654 public static String nullSafeToString(char[] array) {
655 if (array == null) {
656 return NULL_STRING;
657 }
658 int length = array.length;
659 if (length == 0) {
660 return EMPTY_ARRAY;
661 }
662 StringBuffer buffer = new StringBuffer();
663 for (int i = 0; i < length; i++) {
664 if (i == 0) {
665 buffer.append(ARRAY_START);
666 }
667 else {
668 buffer.append(ARRAY_ELEMENT_SEPARATOR);
669 }
670 buffer.append("'").append(array[i]).append("'");
671 }
672 buffer.append(ARRAY_END);
673 return buffer.toString();
674 }
675
676
677
678
679
680
681
682
683
684
685 public static String nullSafeToString(double[] array) {
686 if (array == null) {
687 return NULL_STRING;
688 }
689 int length = array.length;
690 if (length == 0) {
691 return EMPTY_ARRAY;
692 }
693 StringBuffer buffer = new StringBuffer();
694 for (int i = 0; i < length; i++) {
695 if (i == 0) {
696 buffer.append(ARRAY_START);
697 }
698 else {
699 buffer.append(ARRAY_ELEMENT_SEPARATOR);
700 }
701
702 buffer.append(array[i]);
703 }
704 buffer.append(ARRAY_END);
705 return buffer.toString();
706 }
707
708
709
710
711
712
713
714
715
716
717 public static String nullSafeToString(float[] array) {
718 if (array == null) {
719 return NULL_STRING;
720 }
721 int length = array.length;
722 if (length == 0) {
723 return EMPTY_ARRAY;
724 }
725 StringBuffer buffer = new StringBuffer();
726 for (int i = 0; i < length; i++) {
727 if (i == 0) {
728 buffer.append(ARRAY_START);
729 }
730 else {
731 buffer.append(ARRAY_ELEMENT_SEPARATOR);
732 }
733
734 buffer.append(array[i]);
735 }
736 buffer.append(ARRAY_END);
737 return buffer.toString();
738 }
739
740
741
742
743
744
745
746
747
748
749 public static String nullSafeToString(int[] array) {
750 if (array == null) {
751 return NULL_STRING;
752 }
753 int length = array.length;
754 if (length == 0) {
755 return EMPTY_ARRAY;
756 }
757 StringBuffer buffer = new StringBuffer();
758 for (int i = 0; i < length; i++) {
759 if (i == 0) {
760 buffer.append(ARRAY_START);
761 }
762 else {
763 buffer.append(ARRAY_ELEMENT_SEPARATOR);
764 }
765 buffer.append(array[i]);
766 }
767 buffer.append(ARRAY_END);
768 return buffer.toString();
769 }
770
771
772
773
774
775
776
777
778
779
780 public static String nullSafeToString(long[] array) {
781 if (array == null) {
782 return NULL_STRING;
783 }
784 int length = array.length;
785 if (length == 0) {
786 return EMPTY_ARRAY;
787 }
788 StringBuffer buffer = new StringBuffer();
789 for (int i = 0; i < length; i++) {
790 if (i == 0) {
791 buffer.append(ARRAY_START);
792 }
793 else {
794 buffer.append(ARRAY_ELEMENT_SEPARATOR);
795 }
796 buffer.append(array[i]);
797 }
798 buffer.append(ARRAY_END);
799 return buffer.toString();
800 }
801
802
803
804
805
806
807
808
809
810
811 public static String nullSafeToString(short[] array) {
812 if (array == null) {
813 return NULL_STRING;
814 }
815 int length = array.length;
816 if (length == 0) {
817 return EMPTY_ARRAY;
818 }
819 StringBuffer buffer = new StringBuffer();
820 for (int i = 0; i < length; i++) {
821 if (i == 0) {
822 buffer.append(ARRAY_START);
823 }
824 else {
825 buffer.append(ARRAY_ELEMENT_SEPARATOR);
826 }
827 buffer.append(array[i]);
828 }
829 buffer.append(ARRAY_END);
830 return buffer.toString();
831 }
832
833 }