1 16 package org.apache.commons.collections.comparators; 17 18 import java.io.Serializable ; 19 import java.util.Comparator ; 20 21 34 public final class BooleanComparator implements Comparator , Serializable { 35 36 37 private static final long serialVersionUID = 1830042991606340609L; 38 39 40 private static final BooleanComparator TRUE_FIRST = new BooleanComparator(true); 41 42 43 private static final BooleanComparator FALSE_FIRST = new BooleanComparator(false); 44 45 46 private boolean trueFirst = false; 47 48 61 public static BooleanComparator getTrueFirstComparator() { 62 return TRUE_FIRST; 63 } 64 65 77 public static BooleanComparator getFalseFirstComparator() { 78 return FALSE_FIRST; 79 } 80 81 96 public static BooleanComparator getBooleanComparator(boolean trueFirst) { 97 return trueFirst ? TRUE_FIRST : FALSE_FIRST; 98 } 99 100 109 public BooleanComparator() { 110 this(false); 111 } 112 113 123 public BooleanComparator(boolean trueFirst) { 124 this.trueFirst = trueFirst; 125 } 126 127 140 public int compare(Object obj1, Object obj2) { 141 return compare((Boolean )obj1, (Boolean )obj2); 142 } 143 144 153 public int compare(Boolean b1, Boolean b2) { 154 boolean v1 = b1.booleanValue(); 155 boolean v2 = b2.booleanValue(); 156 157 return (v1 ^ v2) ? ( (v1 ^ trueFirst) ? 1 : -1 ) : 0; 158 } 159 160 167 public int hashCode() { 168 int hash = "BooleanComparator".hashCode(); 169 return trueFirst ? -1 * hash : hash; 170 } 171 172 184 public boolean equals(Object object) { 185 return (this == object) || 186 ((object instanceof BooleanComparator) && 187 (this.trueFirst == ((BooleanComparator)object).trueFirst)); 188 } 189 190 201 public boolean sortsTrueFirst() { 202 return trueFirst; 203 } 204 205 } 206 | Popular Tags |