iscomar001
11/30/2018 - 11:26 PM

Lista Utils

Ordena lista Buscar en lista Array utils

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.moov.solution.util;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.beanutils.BeanPredicate;
import org.apache.commons.beanutils.BeanPropertyValueChangeClosure;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.apache.commons.collections.comparators.ReverseComparator;
import org.apache.commons.collections.functors.EqualPredicate;
import org.apache.commons.collections.functors.NullPredicate;

/**
 *B
 * @author omar
 */
public class MoovListUtils {

    public static final int ORDER_ASC = 1;
    public static final int ORDER_DESC = 2;


    public MoovListUtils() {
    }

    
    public static void orderByProperty(List lista, final String columnName, final int order){
        BeanComparator beanComparator = null;

        if (order == ORDER_ASC) {
            beanComparator = new BeanComparator(columnName);
        }else{
            beanComparator = new BeanComparator(columnName, new ReverseComparator(new ComparableComparator()));
        }
        Collections.sort(lista,beanComparator);
    }

    public static void orderByProperty(List lista, final String columnName){
        BeanComparator beanComparator =  new BeanComparator(columnName);
        Collections.sort(lista,beanComparator);
    }

    public static Collection filterByProperty(List lista, String propertyName, Object value){
        EqualPredicate nameEqlPredicate = new EqualPredicate(value);
        BeanPredicate beanPredicate = new BeanPredicate(propertyName, nameEqlPredicate);
        Collection filteredCollection = CollectionUtils.select(lista, beanPredicate);
        return filteredCollection;
    }

    public static Object findObject(List lista, String propertyName, Object value){
        EqualPredicate nameEqlPredicate = new EqualPredicate(value);
        BeanPredicate beanPredicate = new BeanPredicate(propertyName, nameEqlPredicate);
        return  CollectionUtils.find(lista, beanPredicate);
    }

    public static int existNull(List lista, String propertyName){
        BeanPredicate beanPredicate = new BeanPredicate(propertyName, NullPredicate.INSTANCE);
        return  CollectionUtils.countMatches(lista, beanPredicate);
    }

    public static void addValue(List lista, String propertyName, Object value){
        BeanPropertyValueChangeClosure closure = new BeanPropertyValueChangeClosure( propertyName, value );
        CollectionUtils.forAllDo(lista, closure);
    }

}