All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.assertj.internal.ObjectArrayElementComparisonStrategy Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/**
 * 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.Arrays.isArray;

import java.util.Comparator;

public class ObjectArrayElementComparisonStrategy extends StandardComparisonStrategy {

  private Comparator elementComparator;

  public ObjectArrayElementComparisonStrategy(Comparator 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 T[]
    return isArray(actual) && isArray(other) && compareElementsOf((T[]) actual, (T[]) other);
  }

  private boolean compareElementsOf(T[] actual, T[] other) {
    if (actual.length != other.length) return false;
	// compare their elements with elementComparator
    for (int i = 0; i < actual.length; i++) {
      if (elementComparator.compare(actual[i], other[i]) != 0) return false;
	}
	return true;
  }

  @Override
  public String toString() {
    return "ObjectArrayElementComparisonStrategy 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