Java 反射类:ReflexUtil

public class ReflexUtil {
static Logger logger = LoggerFactory.getLogger(ReflexUtil.class); //getMethod
static public Object invokeMethod(String propertiesName, Object object) {
try {
if(object==null) return null;
if (!propertiesName.contains(".")) {
String methodName = "get"+getMethodName(propertiesName);
Method method = object.getClass().getMethod(methodName);
return method.invoke(object);
}
String methodName = "get"+getMethodName(propertiesName.substring(0,propertiesName.indexOf(".")));
Method method = object.getClass().getMethod(methodName);
return invokeMethod(propertiesName.substring(propertiesName.indexOf(".")+1), method.invoke(object)); } catch (Exception e) {
logger.error(e.toString(), e);
return null;
}
} private static String getMethodName(String fildeName) {
byte[] items = fildeName.getBytes();
items[0] = (byte) ((char) items[0] - 'a' + 'A');
return new String(items);
} public static void main(String args[]) {
Video video = new Video();
Album album = new Album();
album.setAlbumId(346l);
video.setAlbum(album);
video.setVideoId(126l);
System.out.println(ReflexUtil.invokeMethod("album.albumId", video));
}
}

Java 对象排序 : CompareUtil ()

package com.gochinatv.vrs.framework.util;

import com.alibaba.fastjson.JSONObject;
import com.gochinatv.vrs.framework.bean.LabelAlbum;
import com.gochinatv.vrs.framework.bean.Video; import java.util.*; /**
 * Created by shhao
 * Date: 14-5-6.
 * Time:下午4:48
 */
public class CompareUtil {     //sort 1正序 -1 倒序  filed 多字段排序
    public static <T> Comparator createComparator(int sort, String... filed) {
        return new ImComparator(sort, filed);
    }     public static class ImComparator implements Comparator {
        int sort = 1;
        String[] filed;         public ImComparator(int sort, String... filed) {
            this.sort = sort == -1 ? -1 : 1;
            this.filed = filed;
        }         @Override
        public int compare(Object o1, Object o2) {
            int result = 0;
            for (String file : filed) {
                Object value1 = ReflexUtil.invokeMethod(file, o1);
                Object value2 = ReflexUtil.invokeMethod(file, o2);
                if (value1 == null || value2 == null) {
                    continue;
                }
                if (value1 instanceof Integer) { //Integer整数序排序
                    int v1 = Integer.valueOf(value1.toString());
                    int v2 = Integer.valueOf(value2.toString());
                    if (v1 == v2) continue;
                    if (sort == 1) {
                        return v1 - v2;
                    } else if (sort == -1) {
                        return v2 - v1;
                    }
                } else if (value1 instanceof Date) {  //Date类型排序
                    Date d1 = (Date) value1;
                    Date d2 = (Date) value2;
                    if (d1.compareTo(d2) == 0) continue;
                    if (sort == 1) {
                        return d1.compareTo(d2);
                    } else if (sort == -1) {
                        return d2.compareTo(d1);
                    }
                } else if (value1 instanceof Long) { //Long排序
                    long v1 = Long.valueOf(value1.toString());
                    long v2 = Long.valueOf(value2.toString());
                    if (v1 == v2) continue;
                    if (sort == 1) {
                        return v1 > v2 ? 1 : -1;
                    } else if (sort == -1) {
                        return v2 > v1 ? 1 : -1;
                    }
                } else if (value1 instanceof Double) { //Double排序
                    Double v1 = Double.valueOf(value1.toString());
                    Double v2 = Double.valueOf(value2.toString());
                    if (v1 == v2) continue;
                    if (sort == 1) {
                        return v1 > v2 ? 1 : -1;
                    } else if (sort == -1) {
                        return v2 > v1 ? 1 : -1;
                    }
                }             }             return result;
        }
    }     public static void main(String args[]) {
        Video label1 = new Video();
        label1.setDisplayOrder(1);
        label1.setCreateTime(DateUtils.convert("2014-01-01"));
        Video label2 = new Video();
        label2.setDisplayOrder(3);
        label2.setCreateTime(DateUtils.convert("2014-01-02"));
        Video label3 = new Video();
        label3.setDisplayOrder(2);
        label3.setCreateTime(DateUtils.convert("2014-01-03"));
        Video label4 = new Video();
        label4.setDisplayOrder(2);
        label4.setCreateTime(DateUtils.convert("2014-01-01"));
        Video label5 = new Video();
        label5.setDisplayOrder(4);
        label5.setCreateTime(DateUtils.convert("2014-01-05"));
        List<Video> list = new ArrayList<Video>();
        list.add(label1);
        list.add(label2);
        list.add(label3);
        list.add(label4);
        list.add(label5);
        Collections.sort(list, CompareUtil.createComparator(1, "createTime", "displayOrder"));
        for (int i = 0; i < list.size(); i++) {
            Video labelAlbum = list.get(i);
            System.out.println("displayorder:" + labelAlbum.getDisplayOrder() + "  sequence:" + DateUtils.convert(labelAlbum.getCreateTime()));
        }
    }
}

最新文章

  1. 使用jsp/servlet简单实现文件上传与下载
  2. C 语言sscanf
  3. 【leetcode】Best Time to Buy and Sell (easy)
  4. BZOJ1485: [HNOI2009]有趣的数列
  5. SourceTree 免登录跳过初始设置
  6. 10 harsh truths that will help you grow
  7. dom4j修改,获取,增加xml中某个元素的属性值
  8. Linux学习之路:shell变量(一)
  9. oschina大数据开源软件
  10. Myeclipse自动生成javabean的get和set方法
  11. UVA 10391 Compound Words
  12. redis 链表
  13. WebService技术简介
  14. vue2.5.2版本 :MAC设置应用在127.0.0.1:80端口访问; 并将127.0.0.1指向www.yours.com ;问题“ Invalid Host header”
  15. luogu 4042 有后效性的dp
  16. oracle select in超过1000条报错解决方法
  17. JSP中三大指令
  18. Archlinux/Manjaro使用笔记-使用makepkg安装软件 报错:未找到strip分割所需的二进制文件 的解决方法
  19. 洛谷 P1078 文化之旅 解题报告
  20. LG4454 【[CQOI2018]破解D-H协议】

热门文章

  1. JY01-KX-01
  2. Js实现简单的联动,无数据库版本
  3. SQL2008 存储过程参数相关
  4. IP V4地址分类
  5. checking for oracle home incompatibilities failed
  6. ajax+FormData+javascript 实现无刷新上传附件
  7. JavaScript 显示弹出窗口
  8. 在QT中使用Irrlicht引擎的方法与步骤
  9. Linux wget下载https类型文件报错解决方法 转自老左博客
  10. linux笔记2.21