01: /*
02: * Copyright Javelin Software, All rights reserved.
03: */
04:
05: package com.javelin.swinglets.table;
06:
07: import java.util.Comparator; //import com.sun.java.util.collections.Comparator;
08:
09: import com.javelin.swinglets.*;
10:
11: /**
12: * This class implements the Comparator interface for JDK 1.1.x
13: * classes that have the Comparator interface implemented in
14: * JDK 1.2.x classes.<br><br>
15: *
16: * This class is used by classes that implement the SortedMap
17: * and SortedSet interface.<br><br>
18: *
19: * @author Robin Sharp
20: * @version 1.0
21: * @see com.sun.java.util.collections.Comparator
22: */
23:
24: public class JDK11Comparator implements Comparator {
25: /**
26: * This Compares, Numbers, Strings, util.Date, and sql Date/Time.
27: */
28: public int compare(Object o1, Object o2) throws ClassCastException {
29: if (o1 == null)
30: return -1;
31: if (o2 == null)
32: return 1;
33:
34: if (o1 instanceof Number && o2 instanceof Number) {
35: return (((Number) o1).doubleValue() < ((Number) o2)
36: .doubleValue() ? -1
37: : (((Number) o1).doubleValue() == ((Number) o2)
38: .doubleValue() ? 0 : 1));
39: }
40:
41: if (o1 instanceof String && o2 instanceof String) {
42: return (((String) o1).compareTo((String) o2));
43: }
44:
45: if (o1 instanceof java.util.Date
46: && o2 instanceof java.util.Date) {
47: return (((java.util.Date) o1).getTime() < ((java.util.Date) o2)
48: .getTime() ? -1
49: : (((java.util.Date) o1).getTime() == ((java.util.Date) o2)
50: .getTime() ? 0 : 1));
51: }
52:
53: if (o1 instanceof java.sql.Date && o2 instanceof java.sql.Date) {
54: return (((java.sql.Date) o1).getTime() < ((java.sql.Date) o2)
55: .getTime() ? -1
56: : (((java.sql.Date) o1).getTime() == ((java.sql.Date) o2)
57: .getTime() ? 0 : 1));
58: }
59:
60: if (o1 instanceof java.sql.Time && o2 instanceof java.sql.Time) {
61: return (((java.sql.Time) o1).getTime() < ((java.sql.Time) o2)
62: .getTime() ? -1
63: : (((java.sql.Time) o1).getTime() == ((java.sql.Time) o2)
64: .getTime() ? 0 : 1));
65: }
66:
67: if (o1 instanceof SIcon && o2 instanceof SIcon) {
68: return ((SIcon) o1).getUrl().compareTo(
69: ((SIcon) o2).getUrl());
70: }
71:
72: return o1.toString().compareTo(o2.toString());
73:
74: //throw new ClassCastException( "Comparator compare " + o1.getClass().getName() + " and " + o2.getClass().getName() );
75: }
76: }
|