<commit_before>package fr.openwide.core.jpa.business.generic.model.comparator;

import java.io.Serializable;
import java.util.Comparator;

import org.springframework.util.Assert;

import com.google.common.collect.Ordering;

import fr.openwide.core.commons.util.ordering.AbstractNullSafeComparator;
import fr.openwide.core.jpa.business.generic.model.GenericEntity;

/**
 * A {@link GenericEntity} comparator that compares IDs.
 * <p>The {@link GenericEntityComparator default implementation} mimics the behavior of {@link GenericEntity#compareTo(GenericEntity)}, except that it is
 * null-safe, both when comparing entities and when comparing their IDs. It assumes a null entity is less than a non-null one, and that an entity with a null ID
 * is less than an entity with a non-null ID.
 * <p>This comparator is consistent with equals.
 */
public class GenericEntityComparator<K extends Comparable<K> & Serializable, E extends GenericEntity<K,?>> extends AbstractNullSafeComparator<E> {

	private static final long serialVersionUID = -5933751018161012653L;
	
	private final Comparator<? super K> KEY_COMPARATOR;

	/**
	 *  
	 */
	public GenericEntityComparator() {
		this(true, Ordering.natural().nullsFirst());
	}

	/**
	 * @param nullIsLow Whether a null entity is lower than a non-null entity. This only applies to the entity object, not to its id.
	 * @param keyComparator The key comparator.
	 * 		Must be null-safe in order for this comparator to be null-safe.
	 * 		Must be serializable in order for this comparator to be serializable.
	 */
	public GenericEntityComparator(boolean nullIsLow, Comparator<? super K> keyComparator) {
		super(nullIsLow);
		Assert.notNull(keyComparator);
		this.KEY_COMPARATOR = keyComparator;
	}
	
	@Override
	protected boolean equalsNotNullObjects(E left, E right) {
		return left.equals(right);
	}

	@Override
	protected int compareNotNullObjects(E left, E right) {
		return KEY_COMPARATOR.compare(left.getId(), right.getId());
	}

}<commit_msg>Mise en cohérence de GenericEntityComparator avec GenericEntity#equals() (deux entités avec un id null sont différentes, mais incomparables) et GenericEntity#isNew() (une entité avec un ID null est plus récente qu'une entité avec un ID non-null).<commit_after>package fr.openwide.core.jpa.business.generic.model.comparator;

import java.io.Serializable;
import java.util.Comparator;

import org.springframework.util.Assert;

import com.google.common.collect.Ordering;

import fr.openwide.core.commons.util.ordering.AbstractNullSafeComparator;
import fr.openwide.core.jpa.business.generic.model.GenericEntity;

/**
 * A {@link GenericEntity} comparator that compares IDs.
 * <p>The {@link #GenericEntityComparator() default implementation} mimics the behavior of {@link GenericEntity#compareTo(GenericEntity)},
 * except that it is null-safe, both when comparing entities and when comparing their IDs. It assumes a null entity is <em>more</em>
 * than a non-null one, and that an entity with a null ID is <em>more</em> than an entity with a non-null ID. This behavior is consistent with
 * {@link GenericEntity#isNew()}: an entity without an id is new, hence more recent than an entity with an id, which is not new.
 * <p>This comparator is consistent with equals.
 * <p><strong>WARNING:</strong> trying to compare two not-null entities with null IDs which are different according to
 * {@link #equalsNotNullObjects(GenericEntity, GenericEntity)} will result in an {@link IllegalArgumentException}
 * being thrown, since such objects are inherently incomparable. This behavior may be overridden in subclasses by
 * comparing other attributes of the entity.
 */
public class GenericEntityComparator<K extends Comparable<K> & Serializable, E extends GenericEntity<K,?>> extends AbstractNullSafeComparator<E> {

	private static final long serialVersionUID = -5933751018161012653L;
	
	private final Comparator<? super K> KEY_COMPARATOR;

	/**
	 *  
	 */
	public GenericEntityComparator() {
		this(false, Ordering.natural().nullsLast());
	}

	/**
	 * @param nullIsLow Whether a null entity is lower than a non-null entity. This only applies to the entity object, not to its id.
	 * @param keyComparator The key comparator.
	 * 		Must be null-safe in order for this comparator to be null-safe.
	 * 		Must be serializable in order for this comparator to be serializable.
	 */
	public GenericEntityComparator(boolean nullIsLow, Comparator<? super K> keyComparator) {
		super(nullIsLow);
		Assert.notNull(keyComparator);
		this.KEY_COMPARATOR = keyComparator;
	}
	
	@Override
	protected boolean equalsNotNullObjects(E left, E right) {
		return left.equals(right);
	}

	@Override
	protected int compareNotNullObjects(E left, E right) {
		K leftId = left.getId();
		K rightId = right.getId();
		if (leftId == null && rightId == null) {
			throw new IllegalArgumentException("Cannot compare two different entities will non-null IDs");
		}
		return KEY_COMPARATOR.compare(leftId, rightId);
	}

}
{'fraction_non_alphanumeric': 0.09063444108761329, 'fraction_numerical': 0.007653575025176234, 'mean_word_length': 4.842352941176471, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3640284', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1390, 'n_words': 501}