⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propertycomparator.java

📁 办公自动化项目
💻 JAVA
字号:
package hong.javanet.util;

import org.apache.commons.beanutils.PropertyUtils;
import java.util.*;
import java.lang.reflect.*;

/**
 *
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author 洪桃李
 * @version 1.0
 */
public class PropertyComparator
    implements Comparator {
  public PropertyComparator(String property) {
    this.property = property;
  }

  public PropertyComparator(String property,boolean ignoreStringCase) {
    this.property = property;
    this.ignoreStringCase = ignoreStringCase;
  }

  protected boolean ignoreStringCase = false;
  protected String property;

  public int compare(Object o1, Object o2) {
    try {
      Comparable p1 = (Comparable) PropertyUtils.getProperty(o1, this.property);
      Comparable p2 = (Comparable) PropertyUtils.getProperty(o2, this.property);
      if (p1 == null && p2 == null) {
        return 0;
      }
      else if (p1 == null) {
        return -1;
      }
      else if (p2 == null) {
        return 1;
      }
      else {
        if(this.ignoreStringCase && (p1 instanceof String) && (p2 instanceof String)) {
          return ((String)p1).compareToIgnoreCase((String)p2);
        }
        return p1.compareTo(p2);
      }
    }
    catch (Exception ex) {//log it
      throw new RuntimeException(ex.getMessage(),ex);
    }
  }


//  public static void main(String args[]) throws Exception {
//    hong.xxx.po.Job job = new hong.xxx.po.Job();
//    job.setMax_lvl( (short) 2);
//    Object obj = PropertyUtils.getProperty(job, "max_lvl");
//    System.out.println(obj);
//    System.out.println(obj.getClass());
//    Short s = null;
//  }
  public boolean isIgnoreStringCase() {
    return ignoreStringCase;
  }
  public String getProperty() {
    return property;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -