001package net.gdface.utils;
002
003import java.lang.reflect.Method;
004import java.util.ArrayList;
005import java.util.Arrays;
006import java.util.Collection;
007import java.util.Collections;
008import java.util.Comparator;
009import java.util.HashMap;
010import java.util.List;
011import java.util.Map;
012
013import org.apache.commons.beanutils.BeanUtilsBean;
014import org.apache.commons.beanutils.PropertyUtilsBean;
015public class BeanRelativeUtilits {
016
017        public static class FieldComparator implements Comparator<Object> {
018                private static final Map<String, FieldComparator> comparators = new HashMap<String, FieldComparator>();
019                private final String name;
020        
021                private FieldComparator(String name) {
022                        if(null == name || name.isEmpty()){
023                                throw new IllegalArgumentException("name is null or empty");
024                        }
025                        this.name = name;
026                }
027        
028                public static final FieldComparator getComparator(String name) {
029                        FieldComparator comparator=comparators.get(name);
030                        if (null != comparator )
031                                return comparator;
032                        comparator = new FieldComparator(name);
033                        comparators.put(name, comparator);
034                        return comparator;
035                }
036                public static final void clearComparators() {
037                        comparators.clear();
038                }
039                @Override
040                public int compare(Object o1, Object o2) {
041                        Object v1 = getPropertyFrom(o1, name);
042                        Object v2 = getPropertyFrom(o2, name);
043                        if (v1 == v2)
044                                return 0;
045                        if (v1 == null)
046                                return 1;
047                        if (v2 == null)
048                                return -1;
049                        try {
050                                Method compareTo = v1.getClass().getMethod("compareTo", v2.getClass());
051                                return (Integer) compareTo.invoke(v1, v2);
052                        } catch (Exception e) {
053                                throw new RuntimeException(e);
054                        }
055                }
056        }
057
058        @SuppressWarnings("unchecked")
059        public static final <T> T getPropertyFrom(Object obj,String name){
060                BeanUtilsBean beanUtils = BeanUtilsBean.getInstance();
061                PropertyUtilsBean propertyUtils = beanUtils.getPropertyUtils();
062                try {
063                        return (T) propertyUtils.getProperty(obj, name);
064                } catch (Exception e) {
065                        throw new RuntimeException(e);
066                } 
067        }
068
069        public static final <T>List<T> sortByField(Collection<T> collection,String fieldName) {
070                if(null == collection){
071                        throw new NullPointerException("collection is null ");
072                }
073                ArrayList<T> list = new ArrayList<T>(collection);
074                Collections.sort(list, FieldComparator.getComparator(fieldName));
075                return list;
076        }
077
078        public static final <T> T[] sortByField(T[] array,String fieldName) {
079                if(null == array){
080                        throw new NullPointerException("array is null ");
081                }
082                T[] newArray = Arrays.copyOf(array, array.length);
083                Arrays.sort(newArray, FieldComparator.getComparator(fieldName));
084                return newArray;
085        }
086}