1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 17 public class Util { 18 25 public static void sort(Object [] array) { 26 qsort(array, 0, array.length - 1); 27 } 28 29 42 public static void qsort(Object [] array, int start, int stop) { 43 if (start >= stop) 44 return; 45 46 int left = start; int right = stop; Object temp; 50 String mid = String.valueOf(array[(start + stop) / 2]); 52 53 while (left <= right) { 55 while ((left < stop) && (String.valueOf(array[left]).compareTo(mid) < 0)) { 57 ++left; 58 } 59 while ((right > start) && (mid.compareTo(String.valueOf(array[right])) < 0)) { 61 --right; 62 } 63 if (left <= right) { 65 temp = array[left]; 66 array[left] = array[right]; 67 array[right] = temp; 68 ++left; 69 --right; 70 } 71 } 72 if (start < right) { 74 qsort(array, start, right); 75 } 76 if (left < stop) { 78 qsort(array, left, stop); 79 } 80 } 81 82 95 public static void sort(Object [] 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; if (((Comparable ) array[middle - 1]).compareTo(array[middle]) <= 0) 104 return; 105 if (start + 2 == end) { 106 Object 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 [] merge = new Object [end - start]; 113 while (i1 < middle && i2 < end) { 114 merge[i3++] = ((Comparable ) 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 134 public static void dsort(Object [] array, int start, int end) { 135 sort(array, start, end); 137 swap(array); 139 } 140 141 146 public static void swap(Object [] array) { 147 int start = 0; 148 int end = array.length - 1; 149 while (start < end) { 150 Object temp = array[start]; 151 array[start++] = array[end]; 152 array[end--] = temp; 153 } 154 } 155 156 169 public static String toString(Object object, int length) { 170 boolean onLeft = object instanceof Number ; 171 return toString(object, length, ' ', onLeft); 172 } 173 174 187 public static String toString(Object object, int length, char pad, boolean onLeft) { 188 String 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 padding = new StringBuffer (length - size); 196 for (int i = size; i < length; i++) 197 padding.append(pad); 198 199 StringBuffer stringBuffer = new StringBuffer (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 |