<commit_before>/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * 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.
 */ 

package org.springframework.beans.support;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;

/**
 * PropertyComparator performs a comparison of two beans,
 * using the specified bean property via a BeanWrapper.
 * @author <NAME>
 * @author <NAME>
 * @since 19.05.2003
 */
public class PropertyComparator implements Comparator {

	protected final Log logger = LogFactory.getLog(getClass());

	private SortDefinition sortDefinition;

	private Map cachedBeanWrappers = new HashMap();

	public PropertyComparator(SortDefinition sortDefinition) {
		this.sortDefinition = sortDefinition;
	}

	public int compare(Object o1, Object o2) {
		Object v1 = getPropertyValue(o1);
		Object v2 = getPropertyValue(o2);
		if (this.sortDefinition.isIgnoreCase() && (v1 instanceof String) && (v2 instanceof String)) {
			v1 = ((String) v1).toLowerCase();
			v2 = ((String) v2).toLowerCase();
		}
		int result;
		try {
			if (v1 != null) {
				result = ((Comparable) v1).compareTo(v2);
			}
			else {
				if (v2 != null) {
					result = - ((Comparable) v2).compareTo(v1);
				} else {
					return 0;
				}
			}
		}
		catch (RuntimeException ex) {
			logger.warn("Could not sort objects [" + o1 + "] and [" + o2 + "]", ex);
			return 0;
		}
		return (this.sortDefinition.isAscending() ? result : -result);
	}

	private Object getPropertyValue(Object o) throws BeansException {
		BeanWrapper bw = (BeanWrapper) this.cachedBeanWrappers.get(o);
		if (bw == null) {
			bw = new BeanWrapperImpl(o);
			this.cachedBeanWrappers.put(o, bw);
		}
		return bw.getPropertyValue(this.sortDefinition.getProperty());
	}


	/**
	 * Sorts the given List according to the given sort definition.
	 * <p>Note: Contained objects have to provide the given property
	 * in the form of a bean property, i.e. a getXXX method.
	 * @param source the input List
	 * @param sortDefinition the parameters to sort by
	 * @throws java.lang.IllegalArgumentException in case of a missing propertyName
	 */
	public static void sort(List source, SortDefinition sortDefinition) throws BeansException {
		Collections.sort(source, new PropertyComparator(sortDefinition));
	}

	/**
	 * Sorts the given source according to the given sort definition.
	 * <p>Note: Contained objects have to provide the given property
	 * in the form of a bean property, i.e. a getXXX method.
	 * @param source input source
	 * @param sortDefinition the parameters to sort by<commit_msg>Bug fixes (better handling of null properties or with null ancestror in path)<commit_after>/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * 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.
 */ 

package org.springframework.beans.support;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.beans.NullValueInNestedPathException;

/**
 * PropertyComparator performs a comparison of two beans,
 * using the specified bean property via a BeanWrapper.
 * @author <NAME>
 * @author <NAME>
 * @since 19.05.2003
 */
public class PropertyComparator implements Comparator {

	protected final Log logger = LogFactory.getLog(getClass());

	private SortDefinition sortDefinition;

	private Map cachedBeanWrappers = new HashMap();

	public PropertyComparator(SortDefinition sortDefinition) {
		this.sortDefinition = sortDefinition;
	}

	public int compare(Object o1, Object o2) {
		Object v1 = getPropertyValue(o1);
		Object v2 = getPropertyValue(o2);
		if (this.sortDefinition.isIgnoreCase() && (v1 instanceof String) && (v2 instanceof String)) {
			v1 = ((String) v1).toLowerCase();
			v2 = ((String) v2).toLowerCase();
		}
		int result;
		
		// Put the null property at the end of the sort
		try {
			if (v1 != null) {
				if (v2 != null) {
					result = ((Comparable) v1).compareTo(v2);
				} else {
					result = -1;
				}
			}
			else {
				if (v2 != null) {
					result = 1;
				} else {
					result = 0;
				}
			}
		}
		catch (RuntimeException ex) {
			logger.warn("Could not sort objects [" + o1 + "] and [" + o2 + "]", ex);
			return 0;
		}
		return (this.sortDefinition.isAscending() ? result : -result);
	}

	private Object getPropertyValue(Object o) throws BeansException {
		BeanWrapper bw = (BeanWrapper) this.cachedBeanWrappers.get(o);
		if (bw == null) {
			bw = new BeanWrapperImpl(o);
			this.cachedBeanWrappers.put(o, bw);
		}

		// If a parent is null, simply done the property as null
		try {
			return bw.getPropertyValue(this.sortDefinition.getProperty());
		} catch (NullValueInNestedPathException e) {
			logger.info("Null value in nested properties path");
			return null;
		}
	}


	/**
	 * Sorts the given List according to the given sort definition.
	 * <p>Note: Contained objects have to provide the given property
	 * in the form of a bean property, i.e. a getXXX method.
	 * @param source the input List
	 * @param sortDefinition the parameters to sort by
	 * @throws java.lang.IllegalArgumentException in case of a missing propertyName
	 */
	public static void sort(List source, SortDefinition sortDefinition) throws BeansException {
		Collections.sort(source, new PropertyComparator(sortDefinition));
	}

	/**
	 * Sorts the given source according to the given sort definition.
	 * <p>Note: Contained objects have to provide the given property
	 * in the form of a bean property, i.e. a getXXX method.
	 * @param source input source
	 * @param sortDefinition the parameters to sort by
{'fraction_non_alphanumeric': 0.09898229471629723, 'fraction_numerical': 0.011849993029415865, 'mean_word_length': 3.8802721088435375, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3870821', 'n_tokens_mistral': 2241, 'n_tokens_neox': 2035, 'n_words': 729}