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

br.com.jarch.faces.controller.LazySorter Maven / Gradle / Ivy

package br.com.jarch.faces.controller;

import br.com.jarch.core.exception.ValidationException;
import br.com.jarch.core.model.IIdentity;
import org.primefaces.model.SortOrder;

import java.util.Comparator;

public class LazySorter implements Comparator {

    private final String sortField;
    private final SortOrder sortOrder;

    public LazySorter(String sortField, SortOrder sortOrder) {
        this.sortField = sortField;
        this.sortOrder = sortOrder;
    }

    @Override
    public int compare(IIdentity entity1, IIdentity entity2) {
        try {
            Object value1 = entity1.getClass().getField(sortField).get(entity1);
            Object value2 = entity2.getClass().getField(sortField).get(entity2);

            int value = ((Comparable)value1).compareTo(value2);

            return SortOrder.ASCENDING.equals(sortOrder) ? value : -1 * value;
        }
        catch(Exception e) {
            throw new ValidationException(e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy