1 package net.sf.saxon.sort; 2 import net.sf.saxon.value.AtomicValue; 3 import net.sf.saxon.value.StringValue; 4 import net.sf.saxon.value.UntypedAtomicValue; 5 import net.sf.saxon.value.CalendarValue; 6 import net.sf.saxon.ConversionContext; 7 8 import java.text.Collator ; 9 import java.util.Comparator ; 10 11 20 21 public class AtomicComparer implements Comparator , java.io.Serializable { 22 23 25 private Comparator collator; 26 private ConversionContext conversion; 27 28 public AtomicComparer(Comparator collator, ConversionContext conversion) { 29 this.collator = collator; 30 if (collator == null) { 31 this.collator = CodepointCollator.getInstance(); 32 } 33 this.conversion = conversion; 34 } 35 36 49 50 public int compare(Object a, Object b) { 51 52 54 if (a instanceof AtomicValue && !((AtomicValue)a).hasBuiltInType()) { 55 a = ((AtomicValue)a).getPrimitiveValue(); 56 } 57 if (b instanceof AtomicValue && !((AtomicValue)b).hasBuiltInType()) { 58 b = ((AtomicValue)b).getPrimitiveValue(); 59 } 60 61 if (a instanceof UntypedAtomicValue) { 62 return ((UntypedAtomicValue)a).compareTo(b, collator, conversion); 63 } else if (b instanceof UntypedAtomicValue) { 64 return -((UntypedAtomicValue)b).compareTo(a, collator, conversion); 65 } else if (a instanceof CalendarValue && b instanceof CalendarValue) { 66 return ((CalendarValue)a).compareTo((CalendarValue)b, conversion); 67 } else if (a instanceof Comparable ) { 68 return ((Comparable )a).compareTo(b); 69 } else if (a instanceof StringValue) { 70 return collator.compare(((StringValue)a).getStringValue(), ((StringValue)b).getStringValue()); 71 } else { 72 throw new ClassCastException ("Objects are not comparable (" + a.getClass() + ", " + b.getClass() + ')'); 73 } 74 } 75 76 87 88 public boolean comparesEqual(Object a, Object b) { 89 91 if (a instanceof AtomicValue && !((AtomicValue)a).hasBuiltInType()) { 92 a = ((AtomicValue)a).getPrimitiveValue(); 93 } 94 if (b instanceof AtomicValue && !((AtomicValue)b).hasBuiltInType()) { 95 b = ((AtomicValue)b).getPrimitiveValue(); 96 } 97 98 if (a instanceof UntypedAtomicValue) { 99 return ((UntypedAtomicValue)a).compareTo(b, collator, conversion) == 0; 100 } else if (b instanceof UntypedAtomicValue) { 101 return ((UntypedAtomicValue)b).compareTo(a, collator, conversion) == 0; 102 } else if (a instanceof StringValue) { 103 return collator.compare(((StringValue)a).getStringValue(), ((StringValue)b).getStringValue()) == 0; 104 } else if (a instanceof String ) { 105 return collator.compare(a, b) == 0; 106 } else { 107 return a.equals(b); 108 } 109 } 110 111 116 117 public Object getComparisonKey(Object a) { 118 119 if (a instanceof AtomicValue && !((AtomicValue)a).hasBuiltInType()) { 120 a = ((AtomicValue)a).getPrimitiveValue(); 121 } 122 123 if (a instanceof StringValue) { 124 if (collator instanceof Collator ) { 125 return ((Collator )collator).getCollationKey(((StringValue)a).getStringValue()); 126 } else { 127 return ((StringValue)a).getStringValue(); 128 } 129 } else { 130 return a; 131 } 132 } 133 134 135 } 136 137 138 | Popular Tags |