KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > utils > comparator > DateComparator


1 /*
2  * @(#)DateComparator.java 1.0 2004-12-20
3  *
4  * Copyright 2003 Daffodil Software Ltd. All rights reserved.
5  */

6
7 package com.daffodilwoods.daffodildb.utils.comparator;
8
9 import java.sql.*;
10
11 import com.daffodilwoods.daffodildb.utils.*;
12 import com.daffodilwoods.database.resource.*;
13
14 /**
15   This class <code>DateComparator </code> compare the two dateValues regardless of there Time values
16  <P>
17  This class come in picture when we compare the value of Date dataType.This class perform its
18  work on the bytes of the date Values.Internally, the date is represented by a long value .
19  When we compare two Dates, we discard their time-values .The GetbyteComparator.getLong() method
20  coverts the byte value into a long which represents the Date .To discard the time value we divide the
21   this value by MS_OF_ONE_DAY . Now we can easily compare these two values and Return the appropriate Result.
22  </P>
23  <p> Prior to this class <code>sameTypeComparator<code> was used to comapre two Dates .But there was
24  problems when we use the date value in <b>Group by </b> clause.see Bug-12663,12664
25  @CCzufDpowfsufs
26  @author Sandeep Kadiyan
27  @since 2004-12-20
28  */

29
30 public class DateComparator extends SuperComparator {
31    /**
32     * Allocates a <code>DateComparator</code> object with nullSortedHigh flag
33     */

34    public DateComparator(boolean nullSortedHigh) {
35       super(nullSortedHigh);
36    }
37
38    /**
39     * Allocates a <code>DateComparator</code> object with nullSortedHigh flag
40     *@Usage : GetByteComparator
41     */

42
43    public DateComparator() {
44    }
45
46    /**
47     * Compares two Dates for ordering.
48     *
49     * @return the value <code>0</code> if the argument b1 is equal to
50     * this b2; a value less than <code>-1</code> if b1 date
51     * is before the b2 date; and a value greater than
52     * <code>1</code> if b2 Date is after the b1 Date.
53     * @since 20-12-2004
54     */

55    public int compare(_DComparator b1, _DComparator b2) throws DException {
56       long l1 = getLongValue(b1.getBytes(), 0);
57       long l2 = getLongValue(b2.getBytes(), 0);
58       return comparedates(new Date(l1), new Date(l2));
59    }
60
61    private int comparedates(Date d1, Date d2) {
62       return ( (d1.getYear() == d2.getYear()) ?
63               (d1.getMonth() == d2.getMonth()) ?
64               (d1.getDate() == d2.getDate()) ? 0 :
65               ( (d1.getDate() > d2.getDate()) ? 1 : -1)
66               : (d1.getMonth() > d2.getMonth() ? 1 : -1)
67               : d1.getYear() > d2.getYear() ? 1 : -1);
68    }
69
70    private long getLongValue(byte[] bytes, int position) throws DException {
71       long a = ( (bytes[position + 7] & 0xFFL) << 0) |
72           ( (bytes[position + 6] & 0xFFL) << 8) |
73           ( (bytes[position + 5] & 0xFFL) << 16) |
74           ( (bytes[position + 4] & 0xFFL) << 24) |
75           ( (bytes[position + 3] & 0xFFL) << 32) |
76           ( (bytes[position + 2] & 0xFFL) << 40) |
77           ( (bytes[position + 1] & 0xFFL) << 48) |
78           ( (bytes[position + 0] & 0xFFL) << 56);
79
80       return a;
81    }
82
83
84 }
85
Popular Tags