KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > expressions > util > ToStringSorter


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.core.internal.expressions.util;
12
13 /**
14  * The SortOperation takes a collection of objects and returns a sorted
15  * collection of these objects. The sorting of these objects is based on their
16  * toString(). They are sorted in alphabetical order.
17  * <p>
18  * This is a copy from JDT/Core. The copy is necessary to get an LRU cache which
19  * is independent from JDK 1.4
20  * </p>
21  */

22 public class ToStringSorter {
23     Object JavaDoc[] sortedObjects;
24     String JavaDoc[] sortedStrings;
25
26     /**
27      * Returns true if stringTwo is 'greater than' stringOne This is the
28      * 'ordering' method of the sort operation.
29      * @param stringOne
30      * @param stringTwo
31      * @return a boolean
32      */

33     public boolean compare(String JavaDoc stringOne, String JavaDoc stringTwo) {
34         return stringOne.compareTo(stringTwo) < 0;
35     }
36
37     /**
38      * Sort the objects in sorted collection and return that collection.
39      * @param left
40      * @param right
41      */

42     private void quickSort(int left, int right) {
43         int originalLeft= left;
44         int originalRight= right;
45         int midIndex= (left + right) / 2;
46         String JavaDoc midToString= this.sortedStrings[midIndex];
47
48         do {
49             while (compare(this.sortedStrings[left], midToString))
50                 left++;
51             while (compare(midToString, this.sortedStrings[right]))
52                 right--;
53             if (left <= right) {
54                 Object JavaDoc tmp= this.sortedObjects[left];
55                 this.sortedObjects[left]= this.sortedObjects[right];
56                 this.sortedObjects[right]= tmp;
57                 String JavaDoc tmpToString= this.sortedStrings[left];
58                 this.sortedStrings[left]= this.sortedStrings[right];
59                 this.sortedStrings[right]= tmpToString;
60                 left++;
61                 right--;
62             }
63         } while (left <= right);
64
65         if (originalLeft < right)
66             quickSort(originalLeft, right);
67         if (left < originalRight)
68             quickSort(left, originalRight);
69     }
70
71     /**
72      * Return a new sorted collection from this unsorted collection. Sort using
73      * quick sort.
74      * @param unSortedObjects
75      * @param unsortedStrings
76      */

77     public void sort(Object JavaDoc[] unSortedObjects, String JavaDoc[] unsortedStrings) {
78         int size= unSortedObjects.length;
79         this.sortedObjects= new Object JavaDoc[size];
80         this.sortedStrings= new String JavaDoc[size];
81
82         // copy the array so can return a new sorted collection
83
System.arraycopy(unSortedObjects, 0, this.sortedObjects, 0, size);
84         System.arraycopy(unsortedStrings, 0, this.sortedStrings, 0, size);
85         if (size > 1)
86             quickSort(0, size - 1);
87     }
88 }
89
Popular Tags