KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > types > CompareValues


1 package com.quadcap.sql.types;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.ByteArrayInputStream JavaDoc;
42 import java.io.ByteArrayOutputStream JavaDoc;
43
44 import com.quadcap.sql.index.Comparator;
45
46 import com.quadcap.sql.io.ObjectInputStream;
47 import com.quadcap.sql.io.ObjectOutputStream;
48
49 import com.quadcap.util.Debug;
50 import com.quadcap.util.Util;
51
52 /**
53  * Generic multi-field comparator implementation
54  *
55  * @author Stan Bailes
56  */

57 public class CompareValues extends Comparator {
58     int directions;
59     int cnt;
60     
61     public CompareValues() {}
62
63     public CompareValues(int count) {
64     this.cnt = count;
65     this.directions = ~((~0) << count);
66     }
67     
68     public void addField(boolean asc) {
69     directions <<= 1;
70     if (asc) directions |= 1;
71     cnt++;
72     }
73
74     //#ifdef NOTDEF
75
//- public int compare2(byte[] a, int aoff, int alen,
76
//- byte[] b, int boff, int blen) {
77
//- try {
78
//- int alim = aoff + alen;
79
//- int blim = boff + blen;
80
//- for (int i = 0; i < cnt; i++) {
81
//- int as = a[aoff++] & 0xff;
82
//- if ((as & 0x80) == 0) {
83
//- as <<= 8;
84
//- as |= a[aoff++] & 0xff;
85
//- } else {
86
//- as &= 0x7f;
87
//- }
88
//- int bs = b[boff++] & 0xff;
89
//- if ((bs & 0x80) == 0) {
90
//- bs <<= 8;
91
//- bs |= b[boff++] & 0xff;
92
//- } else {
93
//- bs &= 0x7f;
94
//- }
95
//- for (int j = 0; j < as && j < bs; j++) {
96
//- int av = a[aoff++] & 0xff;
97
//- int bv = b[boff++] & 0xff;
98
//- if (av == bv) continue;
99
//- boolean asc = ((directions >> (cnt - i - 1)) & 1) != 0;
100
//- return ((av < bv) ^ asc) ? -1 : 1;
101
//- }
102
//- }
103
//- return 0;
104
//- } catch (Exception e) {
105
//- throw new RuntimeException(e.toString());
106
//- }
107
//- }
108
//#endif
109

110
111     public int compare(byte[] a, int aoff, int alen,
112                byte[] b, int boff, int blen) {
113     try {
114         ByteArrayInputStream JavaDoc ai = new ByteArrayInputStream JavaDoc(a, aoff, alen);
115         ObjectInputStream aoi = new ObjectInputStream(ai);
116         ByteArrayInputStream JavaDoc bi = new ByteArrayInputStream JavaDoc(b, boff, blen);
117         ObjectInputStream boi = new ObjectInputStream(bi);
118         for (int i = 0; i < cnt; i++) {
119         Value va = (Value)aoi.readObject();
120         Value vb = (Value)boi.readObject();
121         Value cmp = Value.binop(Op.COMPARE, va, vb);
122         //Debug.println("[" + i + "]: " + cmp + " <= (" + va + ") vs (" + vb + ")");
123
ValueInteger vi = (ValueInteger)cmp;
124         int ret = vi.intValue();
125         if (ret != 0) {
126             boolean asc = ((directions >> (cnt - i - 1)) & 1) != 0;
127             ret = asc ? ret : 0 - ret;
128             return ret;
129         }
130         }
131         return 0;
132     } catch (Exception JavaDoc e) {
133             //#ifdef DEBUG
134
Debug.print(e);
135         Debug.println("a = " + Util.hexBytes(a, aoff, alen));
136         Debug.println("b = " + Util.hexBytes(b, boff, blen));
137             //#endif
138
throw new RuntimeException JavaDoc(e.toString());
139     }
140     }
141 }
142
Popular Tags