/*
 * Copyright (c) 2011, Patrik Dufresne. All rights reserved.
 * Patrik Dufresne PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.planimod.core.comparators;

import java.util.Comparator;

import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.comparators.ComparatorChain;

import com.planimod.core.Employee;

/**
 * Utility class
 * 
 * @author Patrik Dufresne
 * 
 */
public class EmployeeComparators {
	/**
	 * Seniority comparator. It use the hireDate and employee's name for
	 * comparison. The smaller hireDate is first. A null hireDate is last.
	 * 
	 * @return
	 */
	// TODO This comparator should be removed
	@Deprecated
	public static Comparator<Employee> bySeniority() {

		ComparatorChain chain = new ComparatorChain();

		chain.addComparator(ComparatorUtils.nullHighComparator(ComparatorUtils
				.transformedComparator(
						ComparatorUtils.nullHighComparator(ComparatorUtils
								.naturalComparator()), new Transformer() {
							@Override
							public Object transform(Object arg0) {
								return ((Employee) arg0).getHireDate();
							}
						})));

		chain.addComparator(ComparatorUtils.nullHighComparator(ComparatorUtils
				.transformedComparator(
						ComparatorUtils.nullHighComparator(ComparatorUtils
								.naturalComparator()), new Transformer() {
							@Override
							public Object transform(Object arg0) {
								return ((Employee) arg0).getFirstname();
							}
						})));

		chain.addComparator(ComparatorUtils.nullHighComparator(ComparatorUtils
				.transformedComparator(
						ComparatorUtils.nullHighComparator(ComparatorUtils
								.naturalComparator()), new Transformer() {
							@Override
							public Object transform(Object arg0) {
								return ((Employee) arg0).getLastname();
							}
						})));

		return chain;
	}

	/**
	 * Comparator to sort employee by name.
	 * 
	 * @return
	 */
	public static Comparator<Employee> byName() {
		ComparatorChain chain = new ComparatorChain();

		chain.addComparator(ComparatorUtils.nullHighComparator(ComparatorUtils
				.transformedComparator(ComparatorUtils
						.nullLowComparator(ComparatorUtils
								.nullHighComparator(ComparatorUtils
										.naturalComparator())),
						new Transformer() {
							@Override
							public Object transform(Object arg0) {
								return ((Employee) arg0).getFirstname();
							}
						})));

		chain.addComparator(ComparatorUtils.nullHighComparator(ComparatorUtils
				.transformedComparator(
						ComparatorUtils.nullHighComparator(ComparatorUtils
								.naturalComparator()), new Transformer() {
							@Override
							public Object transform(Object arg0) {
								return ((Employee) arg0).getLastname();
							}
						})));
		return chain;
	}
}
