KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > core > Util


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 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
12 package org.eclipse.osgi.framework.internal.core;
13
14 /**
15  * This class contains utility functions.
16  */

17 public class Util {
18     /**
19      * Performs a quicksort of the given objects
20      * by their string representation in ascending order.
21      * <p>
22      *
23      * @param array The array of objects to sort
24      */

25     public static void sort(Object JavaDoc[] array) {
26         qsort(array, 0, array.length - 1);
27     }
28
29     /**
30      * Sorts the array of objects by their string representation
31      * in ascending order.
32      * <p>
33      * This is a version of C.A.R Hoare's Quick Sort algorithm.
34      *
35      * @param array the array of objects to sort
36      * @param start the start index to begin sorting
37      * @param stop the end index to stop sorting
38      *
39      * @exception ArrayIndexOutOfBoundsException when <code>start < 0</code>
40      * or <code>end >= array.length</code>
41      */

42     public static void qsort(Object JavaDoc[] array, int start, int stop) {
43         if (start >= stop)
44             return;
45
46         int left = start; // left index
47
int right = stop; // right index
48
Object JavaDoc temp; // for swapping
49

50         // arbitrarily establish a partition element as the midpoint of the array
51
String JavaDoc mid = String.valueOf(array[(start + stop) / 2]);
52
53         // loop through the array until indices cross
54
while (left <= right) {
55             // find the first element that is smaller than the partition element from the left
56
while ((left < stop) && (String.valueOf(array[left]).compareTo(mid) < 0)) {
57                 ++left;
58             }
59             // find an element that is smaller than the partition element from the right
60
while ((right > start) && (mid.compareTo(String.valueOf(array[right])) < 0)) {
61                 --right;
62             }
63             // if the indices have not crossed, swap
64
if (left <= right) {
65                 temp = array[left];
66                 array[left] = array[right];
67                 array[right] = temp;
68                 ++left;
69                 --right;
70             }
71         }
72         // sort the left partition, if the right index has not reached the left side of array
73
if (start < right) {
74             qsort(array, start, right);
75         }
76         // sort the right partition, if the left index has not reached the right side of array
77
if (left < stop) {
78             qsort(array, left, stop);
79         }
80     }
81
82     /**
83      * Sorts the specified range in the array in ascending order.
84      *
85      * @param array the Object array to be sorted
86      * @param start the start index to sort
87      * @param end the last + 1 index to sort
88      *
89      * @exception ClassCastException when an element in the array does not
90      * implement Comparable or elements cannot be compared to each other
91      * @exception IllegalArgumentException when <code>start > end</code>
92      * @exception ArrayIndexOutOfBoundsException when <code>start < 0</code>
93      * or <code>end > array.size()</code>
94      */

95     public static void sort(Object JavaDoc[] array, int start, int end) {
96         int middle = (start + end) / 2;
97         if (start + 1 < middle)
98             sort(array, start, middle);
99         if (middle + 1 < end)
100             sort(array, middle, end);
101         if (start + 1 >= end)
102             return; // this case can only happen when this method is called by the user
103
if (((Comparable JavaDoc) array[middle - 1]).compareTo(array[middle]) <= 0)
104             return;
105         if (start + 2 == end) {
106             Object JavaDoc temp = array[start];
107             array[start] = array[middle];
108             array[middle] = temp;
109             return;
110         }
111         int i1 = start, i2 = middle, i3 = 0;
112         Object JavaDoc[] merge = new Object JavaDoc[end - start];
113         while (i1 < middle && i2 < end) {
114             merge[i3++] = ((Comparable JavaDoc) array[i1]).compareTo(array[i2]) <= 0 ? array[i1++] : array[i2++];
115         }
116         if (i1 < middle)
117             System.arraycopy(array, i1, merge, i3, middle - i1);
118         System.arraycopy(merge, 0, array, start, i2 - start);
119     }
120
121     /**
122      * Sorts the specified range in the array in descending order.
123      *
124      * @param array the Object array to be sorted
125      * @param start the start index to sort
126      * @param end the last + 1 index to sort
127      *
128      * @exception ClassCastException when an element in the array does not
129      * implement Comparable or elements cannot be compared to each other
130      * @exception IllegalArgumentException when <code>start > end</code>
131      * @exception ArrayIndexOutOfBoundsException when <code>start < 0</code>
132      * or <code>end > array.size()</code>
133      */

134     public static void dsort(Object JavaDoc[] array, int start, int end) {
135         // first sort in ascending order
136
sort(array, start, end);
137         // then swap the elements in the array
138
swap(array);
139     }
140
141     /**
142      * Reverse the elements in the array.
143      *
144      * @param array the Object array to be reversed
145      */

146     public static void swap(Object JavaDoc[] array) {
147         int start = 0;
148         int end = array.length - 1;
149         while (start < end) {
150             Object JavaDoc temp = array[start];
151             array[start++] = array[end];
152             array[end--] = temp;
153         }
154     }
155
156     /**
157      * Returns a string representation of the object
158      * in the given length.
159      * If the string representation of the given object
160      * is longer then it is truncated.
161      * If it is shorter then it is padded with the blanks
162      * to the given total length.
163      * If the given object is a number then the padding
164      * is done on the left, otherwise on the right.
165      *
166      * @param object the object to convert
167      * @param length the length the output string
168      */

169     public static String JavaDoc toString(Object JavaDoc object, int length) {
170         boolean onLeft = object instanceof Number JavaDoc;
171         return toString(object, length, ' ', onLeft);
172     }
173
174     /**
175      * Returns a string representation of the object
176      * in the given length.
177      * If the string representation of the given object
178      * is longer then it is truncated.
179      * If it is shorter then it is padded to the left or right
180      * with the given character to the given total length.
181      *
182      * @param object the object to convert
183      * @param length the length the output string
184      * @param pad the pad character
185      * @param onLeft if <code>true</code> pad on the left, otherwise an the right
186      */

187     public static String JavaDoc toString(Object JavaDoc object, int length, char pad, boolean onLeft) {
188         String JavaDoc input = String.valueOf(object);
189         int size = input.length();
190         if (size >= length) {
191             int start = (onLeft) ? size - length : 0;
192             return input.substring(start, length);
193         }
194
195         StringBuffer JavaDoc padding = new StringBuffer JavaDoc(length - size);
196         for (int i = size; i < length; i++)
197             padding.append(pad);
198
199         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc(length);
200         if (onLeft)
201             stringBuffer.append(padding.toString());
202         stringBuffer.append(input);
203         if (!onLeft)
204             stringBuffer.append(padding.toString());
205         return stringBuffer.toString();
206     }
207 }
208
Popular Tags