Java Code Examples for java.util.Arrays#deepEquals()

The following examples show how to use java.util.Arrays#deepEquals() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinarySerializedFieldComparator.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Compare arrays.
 *
 * @param val1 Value 1.
 * @param val2 Value 2.
 * @return Result.
 */
private static boolean compareArrays(Object val1, Object val2) {
    if (val1.getClass() == val2.getClass()) {
        if (val1 instanceof byte[])
            return Arrays.equals((byte[])val1, (byte[])val2);
        else if (val1 instanceof boolean[])
            return Arrays.equals((boolean[])val1, (boolean[])val2);
        else if (val1 instanceof short[])
            return Arrays.equals((short[])val1, (short[])val2);
        else if (val1 instanceof char[])
            return Arrays.equals((char[])val1, (char[])val2);
        else if (val1 instanceof int[])
            return Arrays.equals((int[])val1, (int[])val2);
        else if (val1 instanceof long[])
            return Arrays.equals((long[])val1, (long[])val2);
        else if (val1 instanceof float[])
            return Arrays.equals((float[])val1, (float[])val2);
        else if (val1 instanceof double[])
            return Arrays.equals((double[])val1, (double[])val2);
        else
            return Arrays.deepEquals((Object[])val1, (Object[])val2);
    }

    return false;
}
 
Example 2
Source File: DateFormatSymbols.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Override equals
 */
public boolean equals(Object obj)
{
    if (this == obj) return true;
    if (obj == null || getClass() != obj.getClass()) return false;
    DateFormatSymbols that = (DateFormatSymbols) obj;
    return (Arrays.equals(eras, that.eras)
            && Arrays.equals(months, that.months)
            && Arrays.equals(shortMonths, that.shortMonths)
            && Arrays.equals(weekdays, that.weekdays)
            && Arrays.equals(shortWeekdays, that.shortWeekdays)
            && Arrays.equals(ampms, that.ampms)
            && Arrays.deepEquals(getZoneStringsWrapper(), that.getZoneStringsWrapper())
            && ((localPatternChars != null
              && localPatternChars.equals(that.localPatternChars))
             || (localPatternChars == null
              && that.localPatternChars == null)));
}
 
Example 3
Source File: Objects.java    From streamsupport with GNU General Public License v2.0 6 votes vote down vote up
private static boolean deepEquals0(Object e1, Object e2) {
    boolean eq;
    if (e1 instanceof Object[] && e2 instanceof Object[])
        eq = Arrays.deepEquals ((Object[]) e1, (Object[]) e2);
    else if (e1 instanceof byte[] && e2 instanceof byte[])
        eq = equals((byte[]) e1, (byte[]) e2);
    else if (e1 instanceof short[] && e2 instanceof short[])
        eq = equals((short[]) e1, (short[]) e2);
    else if (e1 instanceof int[] && e2 instanceof int[])
        eq = equals((int[]) e1, (int[]) e2);
    else if (e1 instanceof long[] && e2 instanceof long[])
        eq = equals((long[]) e1, (long[]) e2);
    else if (e1 instanceof char[] && e2 instanceof char[])
        eq = equals((char[]) e1, (char[]) e2);
    else if (e1 instanceof float[] && e2 instanceof float[])
        eq = equals((float[]) e1, (float[]) e2);
    else if (e1 instanceof double[] && e2 instanceof double[])
        eq = equals((double[]) e1, (double[]) e2);
    else if (e1 instanceof boolean[] && e2 instanceof boolean[])
        eq = equals((boolean[]) e1, (boolean[]) e2);
    else
        eq = e1.equals(e2);
    return eq;
}
 
Example 4
Source File: CKInit.java    From InflatableDonkey with MIT License 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final CKInit other = (CKInit) obj;
    if (!Objects.equals(this.cloudKitDeviceUrl, other.cloudKitDeviceUrl)) {
        return false;
    }
    if (!Objects.equals(this.cloudKitDatabaseUrl, other.cloudKitDatabaseUrl)) {
        return false;
    }
    if (!Arrays.deepEquals(this.containers, other.containers)) {
        return false;
    }
    if (!Objects.equals(this.cloudKitShareUrl, other.cloudKitShareUrl)) {
        return false;
    }
    if (!Objects.equals(this.cloudKitUserId, other.cloudKitUserId)) {
        return false;
    }
    return true;
}
 
Example 5
Source File: AI2Solver.java    From codenjoy with GNU General Public License v3.0 5 votes vote down vote up
private boolean fixLiveLock(char[][] field) {
    boolean lock = Arrays.deepEquals(fieldPrevPrev, field) ||
            Arrays.deepEquals(fieldPrev, field);

    fieldPrevPrev = fieldPrev;
    fieldPrev = field;

    return lock;
}
 
Example 6
Source File: PhoenixArray.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean equals(Object obj) {
	if (this.numElements != ((PhoenixArray) obj).numElements) {
		return false;
	}
	if (this.baseType != ((PhoenixArray) obj).baseType) {
		return false;
	}
	return Arrays.deepEquals((Object[]) this.array,
			(Object[]) ((PhoenixArray) obj).array);
}
 
Example 7
Source File: ResultLogConfig.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (o == this) return true;
    if (!(o instanceof ResultLogConfig)) return false;
    final ResultLogConfig other = (ResultLogConfig)o;
    return Arrays.deepEquals(toArray(),other.toArray());
}
 
Example 8
Source File: TimeSeriesLinearRegressionModel.java    From java-timeseries with MIT License 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    TimeSeriesLinearRegressionModel that = (TimeSeriesLinearRegressionModel) o;

    if (!timeSeries.equals(that.timeSeries)) return false;
    if (!seasonalCycle.equals(that.seasonalCycle)) return false;
    if (intercept != that.intercept) return false;
    if (timeTrend != that.timeTrend) return false;
    if (seasonal != that.seasonal) return false;
    return Arrays.deepEquals(externalRegressors, that.externalRegressors);
}
 
Example 9
Source File: PSquarePercentile.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}.This equals method basically checks for marker array to
 * be deep equals.
 *
 * @param o is the other object
 * @return true if the object compares with this object are equivalent
 */
@Override
public boolean equals(Object o) {
    boolean result = false;
    if (this == o) {
        result = true;
    } else if (o != null && o instanceof Markers) {
        Markers that = (Markers) o;
        result = Arrays.deepEquals(markerArray, that.markerArray);
    }
    return result;
}
 
Example 10
Source File: ResultLogConfig.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (o == this) return true;
    if (!(o instanceof ResultLogConfig)) return false;
    final ResultLogConfig other = (ResultLogConfig)o;
    return Arrays.deepEquals(toArray(),other.toArray());
}
 
Example 11
Source File: Ideas_2009_12_11.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
@DesireWarning("EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS")
public boolean equals(Object that) {
    int[][] thatData;
    if (that instanceof Ideas_2009_12_11)
        thatData = ((Ideas_2009_12_11) that).data;
    else if (that instanceof int[][])
        thatData = (int[][]) that;
    else
        return false;
    return Arrays.deepEquals(data, thatData);
}
 
Example 12
Source File: ImmutableDescriptor.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compares this descriptor to the given object.  The objects are equal if
 * the given object is also a Descriptor, and if the two Descriptors have
 * the same field names (possibly differing in case) and the same
 * associated values.  The respective values for a field in the two
 * Descriptors are equal if the following conditions hold:
 *
 * <ul>
 * <li>If one value is null then the other must be too.</li>
 * <li>If one value is a primitive array then the other must be a primitive
 * array of the same type with the same elements.</li>
 * <li>If one value is an object array then the other must be too and
 * {@link Arrays#deepEquals(Object[],Object[])} must return true.</li>
 * <li>Otherwise {@link Object#equals(Object)} must return true.</li>
 * </ul>
 *
 * @param o the object to compare with.
 *
 * @return {@code true} if the objects are the same; {@code false}
 * otherwise.
 *
 */
// Note: this Javadoc is copied from javax.management.Descriptor
//       due to 6369229.
@Override
public boolean equals(Object o) {
    if (o == this)
        return true;
    if (!(o instanceof Descriptor))
        return false;
    String[] onames;
    if (o instanceof ImmutableDescriptor) {
        onames = ((ImmutableDescriptor) o).names;
    } else {
        onames = ((Descriptor) o).getFieldNames();
        Arrays.sort(onames, String.CASE_INSENSITIVE_ORDER);
    }
    if (names.length != onames.length)
        return false;
    for (int i = 0; i < names.length; i++) {
        if (!names[i].equalsIgnoreCase(onames[i]))
            return false;
    }
    Object[] ovalues;
    if (o instanceof ImmutableDescriptor)
        ovalues = ((ImmutableDescriptor) o).values;
    else
        ovalues = ((Descriptor) o).getFieldValues(onames);
    return Arrays.deepEquals(values, ovalues);
}
 
Example 13
Source File: ChangingNotifsTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void test(MBeanServer mbs, ObjectName name, boolean mx)
        throws Exception {
    Object mbean = mx ? new EmptyMX() : new Empty();
    String what = mx ? "MXBean" : "Standard MBean";
    mbs.registerMBean(mbean, name);
    try {
        MBeanInfo mbi = mbs.getMBeanInfo(name);
        Descriptor d = mbi.getDescriptor();
        String immut = (String) d.getFieldValue("immutableInfo");
        boolean immutable = (immut != null && immut.equals("true"));
        if (immutable != mx) {
            fail(what + " has immutableInfo=" + immut + ", should be " +
                 mx);
            return;
        }
        MBeanNotificationInfo[] n1 = mbi.getNotifications().clone();
        mbi = mbs.getMBeanInfo(name);
        boolean unchanged = Arrays.deepEquals(mbi.getNotifications(), n1);
        if (unchanged != mx) {
            fail(what + " notif info " +
                 (unchanged ? "did not change" : "changed"));
            return;
        }
        System.out.println("OK: " + what);
    } finally {
        mbs.unregisterMBean(name);
    }
}
 
Example 14
Source File: SessionManagerTest.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
private void testSerialize(String key, Object value) {
  AppEngineSession session = createSession();
  session.setAttribute(key, value);
  session.save();

  HttpSession httpSession = retrieveSession(session);
  assertNotNull(httpSession);
  Object result = httpSession.getAttribute(key);

  if (!value.getClass().isArray()) {
    assertEquals(value, result);
  } else {
    if (value instanceof Object[]) {
      Object[] valueAsArray = (Object[]) value;
      Object[] resultAsArray = (Object[]) result;

      if (!Arrays.deepEquals(valueAsArray, resultAsArray)) {
        throw new AssertionFailedError(
            "Expected: "
                + Arrays.deepToString(valueAsArray)
                + " Received: "
                + Arrays.deepToString(resultAsArray));
      }
    } else if (value instanceof byte[]) {
      assertArrayEquals((byte[]) value, (byte[]) result);
    } else {
      throw new RuntimeException("Unhandled array type, " + value.getClass());
    }
  }
}
 
Example 15
Source File: RowKey.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Override
public boolean equals( Object obj ) {
  if ( storeValues ) {
    // deep used because Binary type is a native byte[]
    return Arrays.deepEquals( storedFieldValues, ( (RowKey) obj ).storedFieldValues );
  } else {
    return true;
  }
}
 
Example 16
Source File: ImmutableDescriptor.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Compares this descriptor to the given object.  The objects are equal if
 * the given object is also a Descriptor, and if the two Descriptors have
 * the same field names (possibly differing in case) and the same
 * associated values.  The respective values for a field in the two
 * Descriptors are equal if the following conditions hold:
 *
 * <ul>
 * <li>If one value is null then the other must be too.</li>
 * <li>If one value is a primitive array then the other must be a primitive
 * array of the same type with the same elements.</li>
 * <li>If one value is an object array then the other must be too and
 * {@link Arrays#deepEquals(Object[],Object[])} must return true.</li>
 * <li>Otherwise {@link Object#equals(Object)} must return true.</li>
 * </ul>
 *
 * @param o the object to compare with.
 *
 * @return {@code true} if the objects are the same; {@code false}
 * otherwise.
 *
 */
// Note: this Javadoc is copied from javax.management.Descriptor
//       due to 6369229.
@Override
public boolean equals(Object o) {
    if (o == this)
        return true;
    if (!(o instanceof Descriptor))
        return false;
    String[] onames;
    if (o instanceof ImmutableDescriptor) {
        onames = ((ImmutableDescriptor) o).names;
    } else {
        onames = ((Descriptor) o).getFieldNames();
        Arrays.sort(onames, String.CASE_INSENSITIVE_ORDER);
    }
    if (names.length != onames.length)
        return false;
    for (int i = 0; i < names.length; i++) {
        if (!names[i].equalsIgnoreCase(onames[i]))
            return false;
    }
    Object[] ovalues;
    if (o instanceof ImmutableDescriptor)
        ovalues = ((ImmutableDescriptor) o).values;
    else
        ovalues = ((Descriptor) o).getFieldValues(onames);
    return Arrays.deepEquals(values, ovalues);
}
 
Example 17
Source File: ImmutableDescriptor.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
 * <p>Return an {@code ImmutableDescriptor} whose contents are the union of
 * the given descriptors.  Every field name that appears in any of
 * the descriptors will appear in the result with the
 * value that it has when the method is called.  Subsequent changes
 * to any of the descriptors do not affect the ImmutableDescriptor
 * returned here.</p>
 *
 * <p>In the simplest case, there is only one descriptor and the
 * returned {@code ImmutableDescriptor} is a copy of its fields at the
 * time this method is called:</p>
 *
 * <pre>
 * Descriptor d = something();
 * ImmutableDescriptor copy = ImmutableDescriptor.union(d);
 * </pre>
 *
 * @param descriptors the descriptors to be combined.  Any of the
 * descriptors can be null, in which case it is skipped.
 *
 * @return an {@code ImmutableDescriptor} that is the union of the given
 * descriptors.  The returned object may be identical to one of the
 * input descriptors if it is an ImmutableDescriptor that contains all of
 * the required fields.
 *
 * @throws IllegalArgumentException if two Descriptors contain the
 * same field name with different associated values.  Primitive array
 * values are considered the same if they are of the same type with
 * the same elements.  Object array values are considered the same if
 * {@link Arrays#deepEquals(Object[],Object[])} returns true.
 */
public static ImmutableDescriptor union(Descriptor... descriptors) {
    // Optimize the case where exactly one Descriptor is non-Empty
    // and it is immutable - we can just return it.
    int index = findNonEmpty(descriptors, 0);
    if (index < 0)
        return EMPTY_DESCRIPTOR;
    if (descriptors[index] instanceof ImmutableDescriptor
            && findNonEmpty(descriptors, index + 1) < 0)
        return (ImmutableDescriptor) descriptors[index];

    Map<String, Object> map =
        new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
    ImmutableDescriptor biggestImmutable = EMPTY_DESCRIPTOR;
    for (Descriptor d : descriptors) {
        if (d != null) {
            String[] names;
            if (d instanceof ImmutableDescriptor) {
                ImmutableDescriptor id = (ImmutableDescriptor) d;
                names = id.names;
                if (id.getClass() == ImmutableDescriptor.class
                        && names.length > biggestImmutable.names.length)
                    biggestImmutable = id;
            } else
                names = d.getFieldNames();
            for (String n : names) {
                Object v = d.getFieldValue(n);
                Object old = map.put(n, v);
                if (old != null) {
                    boolean equal;
                    if (old.getClass().isArray()) {
                        equal = Arrays.deepEquals(new Object[] {old},
                                                  new Object[] {v});
                    } else
                        equal = old.equals(v);
                    if (!equal) {
                        final String msg =
                            "Inconsistent values for descriptor field " +
                            n + ": " + old + " :: " + v;
                        throw new IllegalArgumentException(msg);
                    }
                }
            }
        }
    }
    if (biggestImmutable.names.length == map.size())
        return biggestImmutable;
    return new ImmutableDescriptor(map);
}
 
Example 18
Source File: CompositeDataSupport.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compares the specified <var>obj</var> parameter with this
 * <code>CompositeDataSupport</code> instance for equality.
 * <p>
 * Returns <tt>true</tt> if and only if all of the following statements are true:
 * <ul>
 * <li><var>obj</var> is non null,</li>
 * <li><var>obj</var> also implements the <code>CompositeData</code> interface,</li>
 * <li>their composite types are equal</li>
 * <li>their contents, i.e. (name, value) pairs are equal. If a value contained in
 * the content is an array, the value comparison is done as if by calling
 * the {@link java.util.Arrays#deepEquals(Object[], Object[]) deepEquals} method
 * for arrays of object reference types or the appropriate overloading of
 * {@code Arrays.equals(e1,e2)} for arrays of primitive types</li>
 * </ul>
 * <p>
 * This ensures that this <tt>equals</tt> method works properly for
 * <var>obj</var> parameters which are different implementations of the
 * <code>CompositeData</code> interface, with the restrictions mentioned in the
 * {@link java.util.Collection#equals(Object) equals}
 * method of the <tt>java.util.Collection</tt> interface.
 *
 * @param  obj  the object to be compared for equality with this
 * <code>CompositeDataSupport</code> instance.
 * @return  <code>true</code> if the specified object is equal to this
 * <code>CompositeDataSupport</code> instance.
 */
@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }

    // if obj is not a CompositeData, return false
    if (!(obj instanceof CompositeData)) {
        return false;
    }

    CompositeData other = (CompositeData) obj;

    // their compositeType should be equal
    if (!this.getCompositeType().equals(other.getCompositeType()) ) {
        return false;
    }

    if (contents.size() != other.values().size()) {
        return false;
    }

    for (Map.Entry<String,Object> entry : contents.entrySet()) {
        Object e1 = entry.getValue();
        Object e2 = other.get(entry.getKey());

        if (e1 == e2)
            continue;
        if (e1 == null)
            return false;

        boolean eq = e1.getClass().isArray() ?
            Arrays.deepEquals(new Object[] {e1}, new Object[] {e2}) :
            e1.equals(e2);

        if (!eq)
            return false;
    }

    // All tests for equality were successful
    //
    return true;
}
 
Example 19
Source File: ArrayConstructExpression.java    From RankPL with MIT License 4 votes vote down vote up
public boolean equals(Object o) {
	return (o instanceof ArrayConstructExpression) &&
			Arrays.deepEquals(((ArrayConstructExpression)o).values, values);
}
 
Example 20
Source File: ImmutableDescriptor.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>Return an {@code ImmutableDescriptor} whose contents are the union of
 * the given descriptors.  Every field name that appears in any of
 * the descriptors will appear in the result with the
 * value that it has when the method is called.  Subsequent changes
 * to any of the descriptors do not affect the ImmutableDescriptor
 * returned here.</p>
 *
 * <p>In the simplest case, there is only one descriptor and the
 * returned {@code ImmutableDescriptor} is a copy of its fields at the
 * time this method is called:</p>
 *
 * <pre>
 * Descriptor d = something();
 * ImmutableDescriptor copy = ImmutableDescriptor.union(d);
 * </pre>
 *
 * @param descriptors the descriptors to be combined.  Any of the
 * descriptors can be null, in which case it is skipped.
 *
 * @return an {@code ImmutableDescriptor} that is the union of the given
 * descriptors.  The returned object may be identical to one of the
 * input descriptors if it is an ImmutableDescriptor that contains all of
 * the required fields.
 *
 * @throws IllegalArgumentException if two Descriptors contain the
 * same field name with different associated values.  Primitive array
 * values are considered the same if they are of the same type with
 * the same elements.  Object array values are considered the same if
 * {@link Arrays#deepEquals(Object[],Object[])} returns true.
 */
public static ImmutableDescriptor union(Descriptor... descriptors) {
    // Optimize the case where exactly one Descriptor is non-Empty
    // and it is immutable - we can just return it.
    int index = findNonEmpty(descriptors, 0);
    if (index < 0)
        return EMPTY_DESCRIPTOR;
    if (descriptors[index] instanceof ImmutableDescriptor
            && findNonEmpty(descriptors, index + 1) < 0)
        return (ImmutableDescriptor) descriptors[index];

    Map<String, Object> map =
        new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
    ImmutableDescriptor biggestImmutable = EMPTY_DESCRIPTOR;
    for (Descriptor d : descriptors) {
        if (d != null) {
            String[] names;
            if (d instanceof ImmutableDescriptor) {
                ImmutableDescriptor id = (ImmutableDescriptor) d;
                names = id.names;
                if (id.getClass() == ImmutableDescriptor.class
                        && names.length > biggestImmutable.names.length)
                    biggestImmutable = id;
            } else
                names = d.getFieldNames();
            for (String n : names) {
                Object v = d.getFieldValue(n);
                Object old = map.put(n, v);
                if (old != null) {
                    boolean equal;
                    if (old.getClass().isArray()) {
                        equal = Arrays.deepEquals(new Object[] {old},
                                                  new Object[] {v});
                    } else
                        equal = old.equals(v);
                    if (!equal) {
                        final String msg =
                            "Inconsistent values for descriptor field " +
                            n + ": " + old + " :: " + v;
                        throw new IllegalArgumentException(msg);
                    }
                }
            }
        }
    }
    if (biggestImmutable.names.length == map.size())
        return biggestImmutable;
    return new ImmutableDescriptor(map);
}