1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*******************************************************************************
* Copyright (c) 2003, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.equinox.console.commands;
/**
* This class contains utility functions.
*/
public class Util {
/**
* Performs a quicksort of the given objects
* by their string representation in ascending order.
* <p>
*
* @param array The array of objects to sort
*/
public static void sortByString(Object[] array) {
qSortByString(array, 0, array.length - 1);
}
/**
* Sorts the array of objects by their string representation
* in ascending order.
* <p>
* This is a version of C.A.R Hoare's Quick Sort algorithm.
*
* @param array the array of objects to sort
* @param start the start index to begin sorting
* @param stop the end index to stop sorting
*
* @exception ArrayIndexOutOfBoundsException when <code>start < 0</code>
* or <code>end >= array.length</code>
*/
public static void qSortByString(Object[] array, int start, int stop) {
if (start >= stop)
return;
int left = start; // left index
int right = stop; // right index
Object temp; // for swapping
// arbitrarily establish a partition element as the midpoint of the array
String mid = String.valueOf(array[(start + stop) / 2]);
// loop through the array until indices cross
while (left <= right) {
// find the first element that is smaller than the partition element from the left
while ((left < stop) && (String.valueOf(array[left]).compareTo(mid) < 0)) {
++left;
}
// find an element that is smaller than the partition element from the right
while ((right > start) && (mid.compareTo(String.valueOf(array[right])) < 0)) {
--right;
}
// if the indices have not crossed, swap
if (left <= right) {
temp = array[left];
array[left] = array[right];
array[right] = temp;
++left;
--right;
}
}
// sort the left partition, if the right index has not reached the left side of array
if (start < right) {
qSortByString(array, start, right);
}
// sort the right partition, if the left index has not reached the right side of array
if (left < stop) {
qSortByString(array, left, stop);
}
}
/**
* Sorts the specified range in the array in ascending order.
*
* @param array the Object array to be sorted
* @param start the start index to sort
* @param end the last + 1 index to sort
*
* @exception ClassCastException when an element in the array does not
* implement Comparable or elements cannot be compared to each other
* @exception IllegalArgumentException when <code>start > end</code>
* @exception ArrayIndexOutOfBoundsException when <code>start < 0</code>
* or <code>end > array.size()</code>
*/
@SuppressWarnings("unchecked")
public static void sort(Object[] array, int start, int end) {
int middle = (start + end) / 2;
if (start + 1 < middle)
sort(array, start, middle);
if (middle + 1 < end)
sort(array, middle, end);
if (start + 1 >= end)
return; // this case can only happen when this method is called by the user
if (((Comparable<Object>) array[middle - 1]).compareTo(array[middle]) <= 0)
return;
if (start + 2 == end) {
Object temp = array[start];
array[start] = array[middle];
array[middle] = temp;
return;
}
int i1 = start, i2 = middle, i3 = 0;
Object[] merge = new Object[end - start];
while (i1 < middle && i2 < end) {
merge[i3++] = ((Comparable<Object>) array[i1]).compareTo(array[i2]) <= 0 ? array[i1++] : array[i2++];
}
if (i1 < middle)
System.arraycopy(array, i1, merge, i3, middle - i1);
System.arraycopy(merge, 0, array, start, i2 - start);
}
/**
* Sorts the specified range in the array in descending order.
*
* @param array the Object array to be sorted
* @param start the start index to sort
* @param end the last + 1 index to sort
*
* @exception ClassCastException when an element in the array does not
* implement Comparable or elements cannot be compared to each other
* @exception IllegalArgumentException when <code>start > end</code>
* @exception ArrayIndexOutOfBoundsException when <code>start < 0</code>
* or <code>end > array.size()</code>
*/
public static void dsort(Object[] array, int start, int end) {
// first sort in ascending order
sort(array, start, end);
// then swap the elements in the array
swap(array);
}
/**
* Reverse the elements in the array.
*
* @param array the Object array to be reversed
*/
public static void swap(Object[] array) {
int start = 0;
int end = array.length - 1;
while (start < end) {
Object temp = array[start];
array[start++] = array[end];
array[end--] = temp;
}
}
/**
* Returns a string representation of the object
* in the given length.
* If the string representation of the given object
* is longer then it is truncated.
* If it is shorter then it is padded with the blanks
* to the given total length.
* If the given object is a number then the padding
* is done on the left, otherwise on the right.
*
* @param object the object to convert
* @param length the length the output string
*/
public static String toString(Object object, int length) {
boolean onLeft = object instanceof Number;
return toString(object, length, ' ', onLeft);
}
/**
* Returns a string representation of the object
* in the given length.
* If the string representation of the given object
* is longer then it is truncated.
* If it is shorter then it is padded to the left or right
* with the given character to the given total length.
*
* @param object the object to convert
* @param length the length the output string
* @param pad the pad character
* @param onLeft if <code>true</code> pad on the left, otherwise an the right
*/
public static String toString(Object object, int length, char pad, boolean onLeft) {
String input = String.valueOf(object);
int size = input.length();
if (size >= length) {
int start = (onLeft) ? size - length : 0;
return input.substring(start, length);
}
StringBuffer padding = new StringBuffer(length - size);
for (int i = size; i < length; i++)
padding.append(pad);
StringBuffer stringBuffer = new StringBuffer(length);
if (onLeft)
stringBuffer.append(padding.toString());
stringBuffer.append(input);
if (!onLeft)
stringBuffer.append(padding.toString());
return stringBuffer.toString();
}
}
|