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