KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > sort > NumericComparer


1 package net.sf.saxon.sort;
2 import net.sf.saxon.om.Item;
3 import net.sf.saxon.trans.XPathException;
4 import net.sf.saxon.value.NumericValue;
5 import net.sf.saxon.value.Value;
6
7 import java.util.Comparator JavaDoc;
8
9 /**
10  * A Comparer used for comparing sort keys when data-type="number". The items to be
11  * compared are converted to numbers, and the numbers are then compared directly
12  *
13  * @author Michael H. Kay
14  *
15  */

16
17 public class NumericComparer implements Comparator JavaDoc, java.io.Serializable JavaDoc {
18
19     public NumericComparer() {
20     }
21
22     /**
23     * Compare two Items by converting them to numbers and comparing the numeric values. If either
24     * value cannot be converted to a number, it is treated as NaN, and compares less that the other
25     * (two NaN values compare equal).
26     * @param a the first Item to be compared.
27     * @param b the second Item to be compared.
28     * @return <0 if a<b, 0 if a=b, >0 if a>b
29     * @throws ClassCastException if the objects are not Items
30     */

31
32     public int compare(Object JavaDoc a, Object JavaDoc b) {
33
34
35         double d1, d2;
36
37         if (a instanceof NumericValue) {
38             d1 = ((NumericValue)a).getDoubleValue();
39         } else {
40             try {
41                 String JavaDoc s1 = (a instanceof String JavaDoc ? (String JavaDoc)a : ((Item)a).getStringValue());
42                 d1 = Value.stringToNumber(s1);
43             //} catch (XPathException err) {
44
// d1 = Double.NaN;
45
} catch (NumberFormatException JavaDoc err) {
46                 d1 = Double.NaN;
47             }
48         }
49
50         if (b instanceof NumericValue) {
51             d2 = ((NumericValue)b).getDoubleValue();
52         } else {
53             try {
54                 String JavaDoc s2 = (b instanceof String JavaDoc ? (String JavaDoc)b : ((Item)b).getStringValue());
55                 d2 = Value.stringToNumber(s2);
56             //} catch (XPathException err) {
57
// d2 = Double.NaN;
58
} catch (NumberFormatException JavaDoc err) {
59                 d2 = Double.NaN;
60             }
61         }
62
63         if (Double.isNaN(d1)) {
64             if (Double.isNaN(d2)) {
65                 return 0;
66             } else {
67                 return -1;
68             }
69         }
70         if (Double.isNaN(d2)) {
71             return +1;
72         }
73         if (d1 < d2) return -1;
74         if (d1 > d2) return +1;
75         return 0;
76
77     }
78
79 }
80
81 //
82
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
83
// you may not use this file except in compliance with the License. You may obtain a copy of the
84
// License at http://www.mozilla.org/MPL/
85
//
86
// Software distributed under the License is distributed on an "AS IS" basis,
87
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
88
// See the License for the specific language governing rights and limitations under the License.
89
//
90
// The Original Code is: all this file.
91
//
92
// The Initial Developer of this module is Michael H. Kay
93
//
94
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
95
//
96
// Contributor(s): none.
97
//
Popular Tags