01: /*
02: * Copyright (c) 1998 - 2005 Versant Corporation
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Versant Corporation - initial API and implementation
10: */
11: package com.versant.core.jdo.query;
12:
13: import com.versant.core.common.SortableBase;
14:
15: import java.util.Vector;
16:
17: /**
18: */
19: public class VariableSorter extends SortableBase {
20: private Vector variables;
21:
22: public VariableSorter(final Vector variables) {
23: this .variables = variables;
24: }
25:
26: public Vector getvariables() {
27: return variables;
28: }
29:
30: public void sort() {
31: size = variables.size();
32: super .sort();
33: }
34:
35: public void setvariables(final Vector variables) {
36: this .variables = variables;
37: }
38:
39: protected int compare(final int a, final int b) {
40: return ((VarNode) variables.get(a)).getIdentifier().compareTo(
41: ((VarNode) variables.get(b)).getIdentifier());
42: }
43:
44: protected void swap(final int index1, final int index2) {
45: Object a = variables.get(index1);
46: Object b = variables.get(index2);
47:
48: variables.remove(index1);
49: variables.insertElementAt(b, index1);
50:
51: variables.remove(index2);
52: variables.insertElementAt(a, index2);
53: }
54:
55: public VarNode search(String name) {
56: int low = 0;
57: int high = variables.size() - 1;
58: int mid, cmp;
59: VarNode node;
60: while (low <= high) {
61: mid = (low + high) / 2;
62: node = (VarNode) variables.get(mid);
63: cmp = node.getIdentifier().compareTo(name);
64: if (cmp < 0)
65: low = mid + 1;
66: else if (cmp > 0)
67: high = mid - 1;
68: else
69: return node;
70: }
71: return null;
72: }
73: }
|