1 7 8 package org.jdesktop.swing.decorator; 9 10 15 public class ShuttleSorter extends Sorter { 16 private int[] toPrevious, fromPrevious; 17 18 public ShuttleSorter() { 19 this(0, true); 20 } 21 22 public ShuttleSorter(int col, boolean ascending) { 23 super(col, ascending); 24 } 25 26 protected void init() { 27 toPrevious = new int[0]; 28 fromPrevious = new int[0]; 29 } 30 31 36 protected void adopt(Sorter oldSorter) { 37 if (oldSorter != null) { 38 toPrevious = (int[]) ((ShuttleSorter) oldSorter).toPrevious.clone(); 39 40 fromPrevious = (int[]) ((ShuttleSorter) oldSorter).fromPrevious.clone(); 41 42 } 43 } 44 45 48 protected void reset() { 49 int inputSize = getInputSize(); 50 toPrevious = new int[inputSize]; 51 fromPrevious = new int[inputSize]; 52 for (int i = 0; i < inputSize; i++) { 53 toPrevious[i] = i; } 55 } 56 57 60 protected void generateMappingFromPrevious() { 61 for (int i = 0; i < toPrevious.length; i++) { 62 fromPrevious[toPrevious[i]] = i; 63 } 64 } 65 66 69 protected void filter() { 70 sort((int[]) toPrevious.clone(), toPrevious, 0, toPrevious.length); 71 } 72 73 public int getSize() { 74 return toPrevious.length; 75 } 76 77 88 protected int translateFromPreviousFilter(int row) { 89 return fromPrevious[row]; 90 } 91 92 103 protected int translateToPreviousFilter(int row) { 104 return toPrevious[row]; 105 } 106 107 111 protected void sort(int from[], int to[], int low, int high) { 119 if (high - low < 2) { 120 return; 122 } 123 int middle = (low + high) >> 1; 124 125 sort(to, from, low, middle); 126 sort(to, from, middle, high); 127 128 int p = low; 129 int q = middle; 130 131 145 146 if (high - low >= 4 && compare(from[middle - 1], from[middle]) <= 0) { 147 for (int i = low; i < high; i++) { 148 to[i] = from[i]; 149 } 150 return; 151 } 152 153 155 for (int i = low; i < high; i++) { 156 if (q >= high || (p < middle && compare(from[p], from[q]) <= 0)) { 157 to[i] = from[p++]; 158 } 159 else { 160 to[i] = from[q++]; 161 } 162 } 163 } 164 } | Popular Tags |