KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > sql > Order


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.db.sql;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.IntArray;
33
34 import java.sql.SQLException JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 abstract class Order {
38   private static final Logger JavaDoc log = Log.open(Order.class);
39
40   private boolean _isAscending = true;
41
42   protected Order _next;
43
44   /**
45    * Set true for ascending.
46    */

47   public boolean isAscending()
48   {
49     return _isAscending;
50   }
51
52   /**
53    * Set true for ascending.
54    */

55   public void setAscending(boolean isAscending)
56   {
57     _isAscending = isAscending;
58   }
59
60   /**
61    * Append the next value.
62    */

63   static Order append(Order order, Order next)
64   {
65     if (order == null)
66       return next;
67     else {
68       order._next = next;
69       return order;
70     }
71   }
72
73   abstract public int compare(SelectResult result, int indexA, int indexB)
74     throws SQLException JavaDoc;
75
76   /**
77    * Sorts the database result.
78    */

79   public void sort(SelectResult result, IntArray rowArray)
80     throws SQLException JavaDoc
81   {
82     int []rows = rowArray.getArray();
83     int size = rowArray.size();
84
85     sort(result, rows, 0, size);
86   }
87
88   /**
89    * Sorts a subset of the database result.
90    */

91   private void sort(SelectResult result, int []rows, int offset, int length)
92     throws SQLException JavaDoc
93   {
94     if (length > 3) {
95       int head = offset;
96       int right = offset + length - 1;
97       int tail = right;
98       int pivot = offset + length / 2;
99       int pivotIndex = rows[pivot];
100
101       // swap(pivot, right)
102
int temp = rows[right];
103       rows[right] = pivotIndex;
104       rows[pivot] = temp;
105
106       --tail;
107
108       for (;;) {
109
110         while (head <= tail && compare(result, rows[head], pivotIndex) < 0) {
111           ++head;
112         }
113
114         while (head <= tail && compare(result, rows[tail], pivotIndex) > 0) {
115           --tail;
116         }
117
118         if (head > tail) {
119           break;
120         }
121
122         // swap(head++, tail--)
123
temp = rows[head];
124         rows[head++] = rows[tail];
125         rows[tail--] = temp;
126       }
127
128       if (compare(result, rows[head], pivotIndex) > 0) {
129         // swap(head, right)
130
temp = rows[head];
131         rows[head] = rows[right];
132         rows[right] = temp;
133       }
134
135       if (offset < head) {
136         sort(result, rows, offset, head - offset);
137       }
138
139       if (right > head) {
140         sort(result, rows, head+1, right - head);
141       }
142     }
143     else if (length == 3) {
144       int indexA = rows[offset + 0];
145       int indexB = rows[offset + 1];
146       int indexC = rows[offset + 2];
147
148       if (compare(result, indexB, indexA) < 0) {
149         int temp = indexA;
150         indexA = indexB;
151         indexB = temp;
152       }
153
154       // A <= B <= C
155
if (compare(result, indexB, indexC) <= 0) {
156       }
157       // C < A <= B
158
else if (compare(result, indexC, indexA) < 0) {
159         int temp = indexC;
160         indexC = indexB;
161         indexB = indexA;
162         indexA = temp;
163       }
164       // A <= C < B
165
else {
166         int temp = indexC;
167         indexC = indexB;
168         indexB = temp;
169       }
170
171       rows[offset + 0] = indexA;
172       rows[offset + 1] = indexB;
173       rows[offset + 2] = indexC;
174     }
175     else if (length == 2) {
176       int indexA = rows[offset];
177       int indexB = rows[offset + 1];
178
179       if (compare(result, indexB, indexA) < 0) {
180         int temp = indexB;
181         indexB = indexA;
182         indexA = temp;
183       }
184
185       rows[offset + 0] = indexA;
186       rows[offset + 1] = indexB;
187     }
188     else {
189       // nothing to do
190
}
191   }
192 }
193
Popular Tags