KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > iterator > table > DistinctComparator


1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table;
2
3 import com.daffodilwoods.daffodildb.utils.comparator.*;
4 import com.daffodilwoods.daffodildb.utils.field.*;
5 import com.daffodilwoods.database.resource.*;
6
7 /**
8  * <p>Title: DistinctComparator </p>
9  * <p>Description: This Class is used for returing the records from a query
10  * having the DISTINCT clause. This is used to compare the one set of values of
11  * selected columns with that of other set. </p>
12  * <p>Copyright: Copyright (c) 2003</p>
13  * <p>Company: </p>
14  * @author unascribed
15  * @version 1.0
16  */

17
18 public class DistinctComparator extends SuperComparator {
19
20   /**
21    * An array representing the comparators correspponding to each selected
22    * column specified in the select list.
23    */

24   private SuperComparator[] comparators;
25
26    public DistinctComparator(SuperComparator[] comparators0) {
27       comparators = comparators0;
28    }
29
30    /**
31     * It compares the one set of values of selected columns with that of other.
32     * This method checks the each comparator and returns accordingly.
33     * @param o1 selected column values from left iterator
34     * @param o2 selected column values from right iterator
35     * @return 0, if all of the values are same
36     * -1, if selected column values from left iterator are smaller w.r.t right
37     * 1, if selected column values from right iterator are smaller w.r.t left
38     * 2 or -2, if any one of the value is null while other is non-null.
39     * @throws DException
40     */

41    public int compare(Object JavaDoc o1, Object JavaDoc o2) throws DException {
42       Object JavaDoc[] leftFieldBases = (Object JavaDoc[]) o1;
43       Object JavaDoc[] rightFieldBases = (Object JavaDoc[]) o2;
44       for (int i = 0, length = leftFieldBases.length, j = 0, k = 0, lengthK = comparators.length, length1 = rightFieldBases.length; i < length && j < length1 && k < lengthK; i++, j++, k++) {
45          FieldBase fb1 = (FieldBase) leftFieldBases[i];
46          FieldBase fb2 = (FieldBase) rightFieldBases[j];
47          int cmp = fb1.getNull() ? fb2.getNull() ? 0 : nullSortedHigh ? 2 : -2
48              : fb2.getNull() ? nullSortedHigh ? -2 : 2 : comparators[k].compare(fb1, rightFieldBases[j]);
49          if (cmp != 0) {
50             return cmp;
51          }
52       }
53       return 0;
54    }
55
56 }
57
Popular Tags