com.fitbur.assertj.internal.IterableElementComparisonStrategy Maven / Gradle / Ivy
/**
* Licensed under the Apache 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://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
* Copyright 2012-2016 the original author or authors.
*/
package com.fitbur.assertj.internal;
import static com.fitbur.assertj.presentation.StandardRepresentation.STANDARD_REPRESENTATION;
import static com.fitbur.assertj.util.IterableUtil.sizeOf;
import java.util.Comparator;
import java.util.Iterator;
public class IterableElementComparisonStrategy extends StandardComparisonStrategy {
private Comparator super T> elementComparator;
public IterableElementComparisonStrategy(Comparator super T> elementComparator) {
this.elementComparator = elementComparator;
}
@SuppressWarnings("unchecked")
@Override
public boolean areEqual(Object actual, Object other) {
if (actual == null && other == null) return true;
if (actual == null || other == null) return false;
// expecting actual and other to be iterable
return actual instanceof Iterable && other instanceof Iterable
&& compareElementsOf((Iterable) actual, (Iterable) other);
}
private boolean compareElementsOf(Iterable actual, Iterable other) {
if (sizeOf(actual) != sizeOf(other)) return false;
// compare their elements with elementComparator
Iterator iterator = other.iterator();
for (T actualElement : actual) {
T otherElement = iterator.next();
if (elementComparator.compare(actualElement, otherElement) != 0) return false;
}
return true;
}
@Override
public String toString() {
return "IterableElementComparisonStrategy using " + STANDARD_REPRESENTATION.toStringOf(elementComparator);
}
@Override
public String asText() {
return "when comparing elements using " + STANDARD_REPRESENTATION.toStringOf(elementComparator);
}
@Override
public boolean isStandard() {
return false;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy