Summary Table

Categories Total Count
PII 0
URL 0
DNS 0
EKL 0
IP 0
PORT 0
VsID 0
CF 0
AI 0
VPD 0
PL 0
Other 0

File Content

package gov.va.med.pbm.ampl.utility.operations.comparator;

import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import org.apache.commons.collections.comparators.NullComparator;
import org.apache.commons.lang3.builder.CompareToBuilder;

import gov.va.med.pbm.ampl.utility.operations.criteria.SortCriteria;
import gov.va.med.pbm.ampl.utility.PropertyReflection;

/**
* The AmplComparator is a custom Comparator class which allows the APO to perform searches on any data set.
*
* @author Ian Meinert
*
* @param <T> Generic class which identifies the object contained in the collection.
*/
public class AmplComparator<T> extends PropertyReflection implements Comparator<T> {

/**
* The sortParams is a collection of the criteria which to sort the collection on
*/
private List<SortCriteria> sortParams;

private static final NullComparator NULL_COMPARATOR = new NullComparator(false);

/**
* Constructor which expects a collection of SortCriteria.
*
* @param sortParams The criteria which to sort the collection
*/
public AmplComparator(List<SortCriteria> sortParams) {
this.sortParams = sortParams;
}

/**
* The implemented comparator which compares two Generics using the provided criteria and direction.
*
* @return
*/
@Override
public int compare(final T t1, final T t2) {
CompareToBuilder builder = new CompareToBuilder();

for (SortCriteria criteria : sortParams) {
String key = criteria.getKey();
String direction = criteria.getDirection().toUpperCase(Locale.ENGLISH);

Object t1Value = get(t1, key);
Object t2Value = get(t2, key);

if (t1Value == null || t2Value == null) {
return NULL_COMPARATOR.compare(t1Value, t2Value);
}

if ("DESC".equals(direction)) {
// if the criteria requires a DESC order
builder.append(t2Value, t1Value);
} else {
builder.append(t1Value, t2Value);
// if the criteria requires a ASC order by default
}
}

return builder.toComparison();
}
}