A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / util / ToStringSorter.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.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  */
18 public class ToStringSorter {
19         Object[] sortedObjects;
20
21         String[] sortedStrings;
22
23         /**
24          * Returns true if stringTwo is 'greater than' stringOne This is the
25          * 'ordering' method of the sort operation.
26          */
27         public boolean compare(String stringOne, String stringTwo) {
28                 return stringOne.compareTo(stringTwo) < 0;
29         }
30
31         /**
32          * Sort the objects in sorted collection and return that collection.
33          */
34         private void quickSort(int left, int right) {
35                 int originalLeft = left;
36                 int originalRight = right;
37                 int midIndex = (left + right) / 2;
38                 String midToString = this.sortedStrings[midIndex];
39
40                 do {
41                         while (compare(this.sortedStrings[left], midToString))
42                                 left++;
43                         while (compare(midToString, this.sortedStrings[right]))
44                                 right--;
45                         if (left <= right) {
46                                 Object tmp = this.sortedObjects[left];
47                                 this.sortedObjects[left] = this.sortedObjects[right];
48                                 this.sortedObjects[right] = tmp;
49                                 String tmpToString = this.sortedStrings[left];
50                                 this.sortedStrings[left] = this.sortedStrings[right];
51                                 this.sortedStrings[right] = tmpToString;
52                                 left++;
53                                 right--;
54                         }
55                 } while (left <= right);
56
57                 if (originalLeft < right)
58                         quickSort(originalLeft, right);
59                 if (left < originalRight)
60                         quickSort(left, originalRight);
61         }
62
63         /**
64          * Return a new sorted collection from this unsorted collection. Sort using
65          * quick sort.
66          */
67         public void sort(Object[] unSortedObjects, String[] unsortedStrings) {
68                 int size = unSortedObjects.length;
69                 this.sortedObjects = new Object[size];
70                 this.sortedStrings = new String[size];
71
72                 // copy the array so can return a new sorted collection
73                 System.arraycopy(unSortedObjects, 0, this.sortedObjects, 0, size);
74                 System.arraycopy(unsortedStrings, 0, this.sortedStrings, 0, size);
75                 if (size > 1)
76                         quickSort(0, size - 1);
77         }
78 }