XYComparator.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:1k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * Created on 20.06.2005
  3.  *
  4.  */
  5. package org.jdesktop.demo.swingx.common;
  6. import java.awt.Dimension;
  7. import java.awt.Point;
  8. import java.util.Comparator;
  9. /**
  10.  * A custom comparator implementation for Point and Dimension.
  11.  * 
  12.  * @author Jeanette Winzenburg
  13.  */
  14. public class XYComparator implements Comparator {
  15.     public int compare(Object o1, Object o2) {
  16.         if ((o1 instanceof Point) && (o2 instanceof Point)) {
  17.             return comparePoint((Point) o1, (Point) o2);
  18.         } 
  19.         if ((o1 instanceof Dimension) && (o2 instanceof Dimension)) {
  20.             return compareDimension((Dimension) o1, (Dimension) o2);
  21.         }
  22.         return 0; // can't decide
  23.     }
  24.     private int compareDimension(Dimension dim1, Dimension dim2) {
  25.         if (dim1.width == dim2.width) {
  26.             if (dim1.height == dim2.height) return 0;
  27.             return dim1.height < dim2.height ? -1 : 1;
  28.         }
  29.         return dim1.width < dim2.width ? -1 : 1;
  30.     }
  31.     
  32.     private int comparePoint(Point p1, Point p2) {
  33.         if (p1.x == p2.x) {
  34.             if (p1.y == p2.y) return 0;
  35.             return p1.y < p2.y ? -1 : 1;
  36.         }
  37.         return p1.x < p2.x ? -1 : 1;
  38.     }
  39. }