KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > query > VariableSorter


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo.query;
13
14 import com.versant.core.common.SortableBase;
15
16 import java.util.Vector JavaDoc;
17
18 /**
19  */

20 public class VariableSorter extends SortableBase{
21         private Vector JavaDoc variables;
22
23         public VariableSorter(final Vector JavaDoc variables) {
24             this.variables = variables;
25         }
26
27
28         public Vector JavaDoc getvariables() {
29             return variables;
30         }
31
32         public void sort() {
33             size = variables.size();
34             super.sort();
35         }
36
37         public void setvariables(final Vector JavaDoc variables) {
38             this.variables = variables;
39         }
40
41         protected int compare(final int a,final int b) {
42             return ((VarNode)variables.get(a)).getIdentifier().compareTo(((VarNode)variables.get(b)).getIdentifier());
43         }
44
45         protected void swap(final int index1, final int index2) {
46             Object JavaDoc a = variables.get(index1);
47             Object JavaDoc b = variables.get(index2);
48
49             variables.remove(index1);
50             variables.insertElementAt(b,index1);
51
52             variables.remove(index2);
53             variables.insertElementAt(a,index2);
54         }
55
56         public VarNode search(String JavaDoc name){
57             int low = 0;
58             int high = variables.size() - 1;
59             int mid, cmp;
60             VarNode node;
61             while(low <= high){
62                 mid = (low + high) / 2;
63                 node = (VarNode)variables.get(mid);
64                 cmp = node.getIdentifier().compareTo(name);
65                 if( cmp < 0) low = mid + 1;
66                 else if(cmp > 0) high = mid - 1;
67                 else return node;
68             }
69             return null;
70         }
71     }
72
Popular Tags