1 26 27 29 package de.nava.informa.utils; 30 31 import java.util.Comparator ; 32 33 import de.nava.informa.core.ItemIF; 34 35 41 public class ItemComparator implements Comparator { 42 43 private boolean reverseOrder; 44 private boolean useFoundDate; 45 46 51 public ItemComparator() { 52 this(false, false); 53 } 54 55 public ItemComparator(boolean reverseOrder) { 56 this(reverseOrder, false); 57 } 58 59 public ItemComparator(boolean reverseOrder, boolean useFoundDate) { 60 this.reverseOrder = reverseOrder; 61 this.useFoundDate = useFoundDate; 62 } 63 64 public int compare(Object obj1, Object obj2) { 65 if (obj1 instanceof ItemIF) { 66 ItemIF item1 = (ItemIF) obj1; 67 if (obj2 instanceof ItemIF) { 68 ItemIF item2 = (ItemIF) obj2; 69 int cmp = 0; 70 if (useFoundDate) { 71 if (item1.getFound() != null && item2.getFound() != null) { 72 cmp = item1.getFound().compareTo(item2.getFound()); 73 } 74 } else { 75 if (item1.getDate() != null && item2.getDate() != null) { 76 cmp = item1.getDate().compareTo(item2.getDate()); 77 } 78 } 79 if (reverseOrder) { 80 return -1 * cmp; 81 } else { 82 return cmp; 83 } 84 } else { 85 throw new IllegalArgumentException ("Not instance of ItemIF " + obj2); 86 } 87 } else { 88 throw new IllegalArgumentException ("Not instance of ItemIF " + obj1); 89 } 90 } 91 92 public boolean getReverseOrder() { 93 return reverseOrder; 94 } 95 96 public void setReverseOrder(boolean reverseOrder) { 97 this.reverseOrder = reverseOrder; 98 } 99 100 public boolean getUseFoundDate() { 101 return useFoundDate; 102 } 103 104 public void setUseFoundDate(boolean useFoundDate) { 105 this.useFoundDate = useFoundDate; 106 } 107 108 } 109 | Popular Tags |