1 33 package net.sf.jga.util; 34 35 import java.io.Serializable ; 36 import java.util.Comparator ; 37 38 54 55 57 public class NullComparator<T> implements Comparator <T>, Serializable { 58 59 private boolean _nullsAreLess = true; 61 62 private Comparator <T> _comp; 64 65 70 public NullComparator(Comparator <T> comp) { 71 this(comp, true); 72 } 73 74 79 public NullComparator(Comparator <T> comp, boolean flag) { 80 if (comp == null) 81 throw new IllegalArgumentException ("A Comparator is required"); 82 83 _comp = comp; 84 _nullsAreLess = flag; 85 } 86 87 88 91 public Comparator <T> getComparator() { return _comp; } 92 93 94 95 public int compare(T x, T y) { 96 if (x == null) 97 if (y == null) 98 return 0; 99 else 100 return _nullsAreLess ? -1 : 1; 101 else 102 if (y == null) 103 return _nullsAreLess ? 1 : -1; 104 else 105 return _comp.compare(x, y); 106 } 107 } 108 | Popular Tags |