KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > TwoArrayQuickSorter


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

11 package org.eclipse.ui.dialogs;
12
13 import java.util.Comparator JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17 /**
18  * Quick sort to sort key-value pairs. The keys and arrays are specified
19  * in separate arrays.
20  *
21  * @since 2.0
22  */

23 /* package */class TwoArrayQuickSorter {
24
25     private Comparator JavaDoc fComparator;
26
27     /**
28      * Default comparator.
29      */

30     public static final class StringComparator implements Comparator JavaDoc {
31         private boolean fIgnoreCase;
32
33         StringComparator(boolean ignoreCase) {
34             fIgnoreCase = ignoreCase;
35         }
36
37         public int compare(Object JavaDoc left, Object JavaDoc right) {
38             return fIgnoreCase ? ((String JavaDoc) left)
39                     .compareToIgnoreCase((String JavaDoc) right) : ((String JavaDoc) left)
40                     .compareTo((String JavaDoc) right);
41         }
42     }
43
44     /**
45      * Creates a sorter with default string comparator.
46      * The keys are assumed to be strings.
47      * @param ignoreCase specifies whether sorting is case sensitive or not.
48      */

49     public TwoArrayQuickSorter(boolean ignoreCase) {
50         fComparator = new StringComparator(ignoreCase);
51     }
52
53     /**
54      * Creates a sorter with a comparator.
55      * @param comparator the comparator to order the elements. The comparator must not be <code>null</code>.
56      */

57     public TwoArrayQuickSorter(Comparator JavaDoc comparator) {
58         fComparator = comparator;
59     }
60
61     /**
62      * Sorts keys and values in parallel.
63      * @param keys the keys to use for sorting.
64      * @param values the values associated with the keys.
65      */

66     public void sort(Object JavaDoc[] keys, Object JavaDoc[] values) {
67         if ((keys == null) || (values == null)) {
68             Assert.isTrue(false, "Either keys or values in null"); //$NON-NLS-1$
69
return;
70         }
71
72         if (keys.length <= 1) {
73             return;
74         }
75
76         internalSort(keys, values, 0, keys.length - 1);
77     }
78
79     private void internalSort(Object JavaDoc[] keys, Object JavaDoc[] values, int left,
80             int right) {
81         int original_left = left;
82         int original_right = right;
83
84         Object JavaDoc mid = keys[(left + right) / 2];
85         do {
86             while (fComparator.compare(keys[left], mid) < 0) {
87                 left++;
88             }
89
90             while (fComparator.compare(mid, keys[right]) < 0) {
91                 right--;
92             }
93
94             if (left <= right) {
95                 swap(keys, left, right);
96                 swap(values, left, right);
97                 left++;
98                 right--;
99             }
100         } while (left <= right);
101
102         if (original_left < right) {
103             internalSort(keys, values, original_left, right);
104         }
105
106         if (left < original_right) {
107             internalSort(keys, values, left, original_right);
108         }
109     }
110
111     /*
112      * Swaps x[a] with x[b].
113      */

114     private static final void swap(Object JavaDoc x[], int a, int b) {
115         Object JavaDoc t = x[a];
116         x[a] = x[b];
117         x[b] = t;
118     }
119
120 }
121
Popular Tags