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

ch.lambdaj.function.compare.ArgumentComparator Maven / Gradle / Ivy

There is a newer version: 2.3.3
Show newest version
// Modified or written by Ex Machina SAGL for inclusion with lambdaj.
// Copyright (c) 2009 Mario Fusco.
// Licensed under the Apache License, Version 2.0 (the "License")

package ch.lambdaj.function.compare;

import static ch.lambdaj.function.argument.ArgumentsFactory.*;
import static ch.lambdaj.function.compare.ComparatorUtil.*;

import java.util.*;
import java.io.*;

import ch.lambdaj.function.argument.*;

/**
 * Compares two objects by comparing the values returned by an Argument call on them.
 * @author Mario Fusco
 */
public class ArgumentComparator implements Comparator, Serializable {

	private final Argument argument;
	private final Comparator comparator;
	
    /**
     * Creates a comparator that compares two objects by comparing the values returned by an Argument call on them
     * @param argument The argument identifying the property to be compared
     */
	public ArgumentComparator(A argument) {
		this(actualArgument(argument));
	}

    /**
     * Creates a comparator that compares two objects by comparing the values returned by an Argument call on them
     * @param argument The argument identifying the property to be compared
     */
	public ArgumentComparator(Argument argument) {
		this(argument, null);
	}
	
    /**
     * Creates a comparator that compares two objects by comparing the values returned by an Argument call on them
     * @param argument The argument identifying the property to be compared
     * @param comparator The comparator used to compare the values of the arguments
     */
	public ArgumentComparator(A argument, Comparator comparator) {
		this(actualArgument(argument), comparator);
	}
	
    /**
     * Creates a comparator that compares two objects by comparing the values returned by the invocations on the given Argument
     * @param argument The argument identifying the property to be compared
     * @param comparator The comparator used to compare the values of the arguments
     */
    @SuppressWarnings("unchecked")
	public ArgumentComparator(Argument argument, Comparator comparator) {
		this.argument = argument;
		this.comparator = comparator != null ? (Comparator)comparator : DEFAULT_ARGUMENT_COMPARATOR;
	}
	
    /**
     * {@inheritDoc}
     */
	public int compare(T o1, T o2) {
		return nullSafeCompare(comparator, argument.evaluate(o1), argument.evaluate(o2));
	}
}