1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.common
.core
;
15 import java
.lang
.reflect
.Array
;
16 import java
.util
.Collection
;
17 import java
.util
.Iterator
;
19 import java
.util
.SortedSet
;
21 import org
.eclipse
.jdt
.annotation
.Nullable
;
24 * Utility methods for arbitrary objects.
26 * @author Patrick Tasse
29 public final class ObjectUtils
{
31 private ObjectUtils() {}
34 * Checks deep equality of two objects that may be arrays or collections.
35 * The objects (and each of their respective elements, if applicable) must
36 * be of the same class to be considered equal. Deep equality is recursively
37 * called on each element of an array or collection, if applicable.
40 * the first object to compare
42 * the second object to compare
43 * @return true if the objects are deeply equal, false otherwise
45 public static boolean deepEquals(final @Nullable Object o1
, @Nullable final Object o2
) {
49 if (o1
== null || o2
== null) {
52 Class
<?
> class1
= o1
.getClass();
53 Class
<?
> class2
= o2
.getClass();
54 if (!class1
.equals(class2
)) {
57 if (class1
.isArray()) {
58 int length
= Array
.getLength(o1
);
59 if (Array
.getLength(o2
) != length
) {
62 for (int i
= 0; i
< length
; i
++) {
63 if (!deepEquals(Array
.get(o1
, i
), Array
.get(o2
, i
))) {
68 } else if (o1
instanceof Collection
) {
69 Collection
<?
> c1
= (Collection
<?
>) o1
;
70 Collection
<?
> c2
= (Collection
<?
>) o2
;
72 if (c2
.size() != size
) {
75 if (o1
instanceof Set
&& !(o1
instanceof SortedSet
<?
>)) {
76 /* Iteration order is undefined */
77 for (Object e1
: c1
) {
78 boolean contains
= false;
79 for (Object e2
: c2
) {
80 if (deepEquals(e1
, e2
)) {
91 Iterator
<?
> i1
= c1
.iterator();
92 Iterator
<?
> i2
= c2
.iterator();
93 while (i1
.hasNext()) {
94 if (!deepEquals(i1
.next(), i2
.next())) {
100 return o1
.equals(o2
);
104 * Returns a hash code value for an object which may be an array or a
105 * collection. The deep hash code is recursively computed for each element
106 * of an array or collection, if applicable.
110 * @return a hash code value for this object.
112 public static int deepHashCode(final @Nullable Object o
) {
116 if (o
.getClass().isArray()) {
117 final int prime
= 31;
119 for (int i
= 0; i
< Array
.getLength(o
); i
++) {
120 result
= prime
* result
+ deepHashCode(Array
.get(o
, i
));
124 if (o
instanceof Collection
) {
125 Collection
<?
> c
= (Collection
<?
>) o
;
126 if (o
instanceof Set
&& !(o
instanceof SortedSet
<?
>)) {
127 /* Iteration order is undefined */
130 result
+= deepHashCode(e
);
134 final int prime
= 31;
137 result
= prime
* result
+ deepHashCode(e
);
This page took 0.033995 seconds and 5 git commands to generate.