EqualsUtil.java
/*
* Licensed to The Apereo Foundation under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
*
* The Apereo Foundation licenses this file to you under the Educational
* Community License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License
* at:
*
* http://opensource.org/licenses/ecl2.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package org.opencastproject.util;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/** Utility function helping to implement equality. */
public final class EqualsUtil {
private EqualsUtil() {
}
/** Check if <code>a</code> and <code>b</code> have the same class ({@link Object#getClass()}). Each may be null. */
public static boolean eqClasses(Object a, Object b) {
return bothNotNull(a, b) && a.getClass().equals(b.getClass());
}
/**
* Compare the (distinct) elements of two lists for equality treating the lists as sets.
* <p>
* Sets by definition do not allow multiplicity of elements; a set is a (possibly empty) collection of distinct
* elements.
* As Lists may contain non-unique entries, this method removes duplicates before continuing with the comparison
* check.
*
* Examples of
* 1. equality: {1, 2} = {2, 1} = {1, 1, 2} = {1, 2, 2, 1, 2}, null = null
* 2. unequal: {1, 2, 2} != {1, 2, 3}, null != {}
*/
public static boolean eqListUnsorted(List<?> as, List<?> bs) {
if (as == null || bs == null) {
return Objects.equals(as, bs);
}
as = as.stream().distinct().collect(Collectors.toList());
bs = bs.stream().distinct().collect(Collectors.toList());
if (as.size() != bs.size()) {
return false;
}
for (Object a : as) {
if (!bs.contains(a)) {
return false;
}
}
return true;
}
public static boolean bothNotNull(Object a, Object b) {
return a != null && b != null;
}
}