public void setSortKeys(List<? extends SortKey> keys) { if ((keys == null) || keys.isEmpty()) { setSorter(null); return; } SortKey sortKey = SortKey.getFirstSortingKey(keys); // only crappy unsorted... if (sortKey == null) return; Sorter sorter = getSorter(); if (sorter == null) { sorter = createDefaultSorter(); } sorter.setSortKey(sortKey); // technically, we could re-use the sorter // and only reset column, comparator and direction // need to detangle from TableColumn before going there... // so for now we only change the order if we have a sorter // for the given column, create a new default sorter if not // if ((currentSorter == null) || // (currentSorter.getColumnIndex() != sortKey.getColumn())) { // currentSorter = createDefaultSorter(sortKey); // } // if (currentSorter.isAscending() != sortKey.getSortOrder().isAscending()) { // currentSorter.setAscending(sortKey.getSortOrder().isAscending()); // } setSorter(sorter); }
public SortOrder getSortOrder(int column) { Sorter sorter = getSorter(); if ((sorter == null) || (sorter.getColumnIndex() != column)) { return SortOrder.UNSORTED; } return sorter.getSortOrder(); }
@SuppressWarnings("unchecked") public List<? extends SortKey> getSortKeys() { Sorter sorter = getSorter(); if (sorter == null) { return Collections.EMPTY_LIST; } return Collections.singletonList(sorter.getSortKey()); }
public void toggleSortOrder(int column, Comparator comparator) { Sorter currentSorter = getSorter(); if ((currentSorter != null) && (currentSorter.getColumnIndex() == column)) { // JW: think about logic - need to update comparator? currentSorter.toggle(); } else { setSorter(createDefaultSorter(new SortKey(SortOrder.ASCENDING, column, comparator))); } }
public void testSort(String infile) throws IOException { File ifile = new File(infile); File ofile = new File(infile + ".sorted"); ofile.deleteOnExit(); Sorter sorter = Sorter.getSorter(ifile, ofile); sorter.setMaxRecords(10); // <= force text of serialization sorter.run(); checkBedSorted(ofile); }
@Test public void should_not_change_order_of_input_rows() { // given List<Something> rows = TestUtils.load("1"); Sorter sorter = new Sorter(Arrays.asList(fooColumn), Arrays.asList(SortOrder.ASC)); // when sorter.process(rows); // then assertThat(rows).isEqualTo(TestUtils.load("1")); }
@Test public void should_sort_by_one_column_desc() { // given List<Something> rows = TestUtils.load("1"); Sorter sorter = new Sorter(Arrays.asList(fooColumn), Arrays.asList(SortOrder.DESC)); // when List<?> processedRows = sorter.process(rows); // then assertThat(processedRows).isEqualTo(TestUtils.load("1_foo_desc")); }
@Test public void should_preserve_order_of_elements_having_same_value_in_column() { // given List<Something> rows = TestUtils.load("2"); Sorter sorter = new Sorter(Arrays.asList(fooColumn), Arrays.asList(SortOrder.ASC)); // when List<?> processedRows = sorter.process(rows); // then assertThat(processedRows).isEqualTo(TestUtils.load("2_foo_asc")); }
public String getShortParam() { Sorter sorter = this.getSorter(); if (null != sorter) { if (sorter.name().contains("_")) { int _index = sorter.name().indexOf("_"); return sorter.name().substring(_index + 1); } else { return sorter.name(); } } return null; }
@Test @SuppressWarnings("unchecked") public void should_do_nothing_if_there_are_no_sortable_columns() { // given Sorter sorter = new Sorter(Collections.EMPTY_LIST, Collections.EMPTY_LIST); List<Something> rows = TestUtils.load("4"); // when List<?> processed = sorter.process(rows); // then assertThat(processed).isSameAs(rows); }
@Test public void should_sort_by_two_columns_both_asc() { // given List<Something> rows = TestUtils.load("2"); Sorter sorter = new Sorter( Arrays.asList(fooColumn, barColumn), Arrays.asList(SortOrder.ASC, SortOrder.ASC)); // when List<?> processedRows = sorter.process(rows); // then assertThat(processedRows).isEqualTo(TestUtils.load("2_foo_asc_bar_asc")); }
@Test public void should_accept_custom_value_accessor() { // given Sorter sorter = new Sorter( Arrays.asList(new Column(Types.text(), "foo", new ReversingValueAccessor())), Arrays.asList(SortOrder.ASC)); List<Something> rows = TestUtils.load("3"); // when List<?> processedRows = sorter.process(rows); // then assertThat(processedRows).isEqualTo(TestUtils.load("3_foo_asc_revacc")); }
/** * Flushes the pipeline by initiating a {@link org.jdesktop.swingx.decorator.Filter#refresh() * refresh} on the <em>first</em> {@link org.jdesktop.swingx.decorator.Filter filter}, if any, in * this pipeline. After that filter has refreshed itself, it sends a {@link * #filterChanged(org.jdesktop.swingx.decorator.Filter) filterChanged} notification to this * pipeline, and the pipeline responds by initiating a {@link * org.jdesktop.swingx.decorator.Filter#refresh() refresh} on the <em>next</em> {@link * org.jdesktop.swingx.decorator.Filter filter}, if any, in this pipeline. Eventualy, when there * are no more filters left in the pipeline, it broadcasts a {@link * org.jdesktop.swingx.decorator.PipelineEvent} signaling a {@link * org.jdesktop.swingx.decorator.PipelineEvent#CONTENTS_CHANGED} message to all {@link * org.jdesktop.swingx.decorator.PipelineListener} objects registered with this pipeline. */ public void flush() { // JW PENDING: use first! if ((filters != null) && (filters.length > 0)) { filters[0].refresh(); } else if (sorter != null) { sorter.refresh(); } }
/** * Apply a reproducable order to the list of extensions provided, such that the order will not * change as extensions are added or removed. */ protected IExtension[] orderExtensions(IExtension[] extensions) { // By default, the order is based on plugin id sorted // in ascending order. The order for a plugin providing // more than one extension for an extension point is // dependent in the order listed in the XML file. Sorter sorter = new Sorter() { public boolean compare(Object extension1, Object extension2) { String s1 = ((IExtension) extension1).getNamespace().toUpperCase(); String s2 = ((IExtension) extension2).getNamespace().toUpperCase(); // Return true if elementTwo is 'greater than' elementOne return s2.compareTo(s1) > 0; } }; Object[] sorted = sorter.sort(extensions); IExtension[] sortedExtension = new IExtension[sorted.length]; System.arraycopy(sorted, 0, sortedExtension, 0, sorted.length); return sortedExtension; }
public Object calculate(SeasonalAthlete seasonalAthlete) { switch (this.sorter) { case name: return calculateAthleteName(seasonalAthlete); case school: return calculateSchoolName(seasonalAthlete); case ga: return calculateGamesPlayed(seasonalAthlete); default: return calculateStat(sorter.getPropertyName(), seasonalAthlete, sorter.aggregator); } }
protected ActionGroup createActionGroup() { DefaultActionGroup result = new DefaultActionGroup(); Sorter[] sorters = myTreeModel.getSorters(); for (final Sorter sorter : sorters) { if (sorter.isVisible()) { result.add(new TreeActionWrapper(sorter, this)); } } if (sorters.length > 0) { result.addSeparator(); } Grouper[] groupers = myTreeModel.getGroupers(); for (Grouper grouper : groupers) { result.add(new TreeActionWrapper(grouper, this)); } Filter[] filters = myTreeModel.getFilters(); for (Filter filter : filters) { result.add(new TreeActionWrapper(filter, this)); } if (myTreeModel instanceof ProvidingTreeModel) { final Collection<NodeProvider> providers = ((ProvidingTreeModel) myTreeModel).getNodeProviders(); for (NodeProvider provider : providers) { result.add(new TreeActionWrapper(provider, this)); } } result.add(new ExpandAllAction(getTree())); result.add(new CollapseAllAction(getTree())); if (showScrollToFromSourceActions()) { result.addSeparator(); result.add(myAutoScrollToSourceHandler.createToggleAction()); result.add(myAutoScrollFromSourceHandler.createToggleAction()); } return result; }
public SortTester() { int[] array = {23, 12, 3, 7, 5, 4, 3, 1, 7}; System.out.println("The original array is"); for (int element : array) System.out.print(element + ", "); System.out.println(); // Sorter.bubbleSort(array); // System.out.println("After sorting with bubblesort"); // for(int element:array) // System.out.print(element + ", "); // Sorter.selectionSort(array); // System.out.println("After sorting with selection sort"); // for(int element:array) // System.out.print(element + ", "); Sorter.insertionSort(array); System.out.println("After sorting with insertion sort"); for (int element : array) System.out.print(element + ", "); }
/** * Assigns a {@link org.jdesktop.swingx.decorator.ComponentAdapter} to this pipeline if no adapter * has previously been assigned to the pipeline. Once an adapter has been assigned to this * pipeline, any attempt to change that will cause an exception to be thrown. * * @param adapter the <code>ComponentAdapter</code> to assign * @throws IllegalArgumentException if adapter is null * @throws IllegalStateException if an adapter is already assigned to this pipeline and the new * adapter is not the same the existing adapter */ public final void assign(ComponentAdapter adapter) { if (adapter == null) { throw new IllegalArgumentException("null adapter"); } // also assign individual filters when adapter is bound if (this.adapter == null) { this.adapter = adapter; for (int i = 0; i < filters.length; i++) { filters[i].assign(adapter); } if (sorter != null) { sorter.assign(adapter); } flush(); } else if (this.adapter != adapter) { throw new IllegalStateException("Can't bind to a different adapter"); } }
/** * Sets the sorter that the output of the filter pipeline is piped through. This is the sorter * that is installed interactively on a view by a user action. * * <p>This method is responsible for doing all the bookkeeping to assign/cleanup pipeline/adapter * assignments. * * @param sorter the interactive sorter, if any; null otherwise. */ protected void setSorter(Sorter sorter) { Sorter oldSorter = getSorter(); if (oldSorter == sorter) return; if (oldSorter != null) { oldSorter.assign((FilterPipeline) null); } this.sorter = sorter; if (sorter != null) { sorter.assign((FilterPipeline) null); sorter.assign(this); if (adapter != null) { sorter.assign(adapter); sorter.refresh(); } } if ((sorter == null) && isAssigned()) { fireContentsChanged(); } }
/** * Public method called by any class. * * @param <E> * @param a */ @Override public <E extends Comparable<? super E>> void sort(E[] a) { super.start(); defaultSort(a, 0, a.length - 1); super.end(a); }
@Test public void testSpillingSortWithIntermediateMergeIntPair() { try { // amount of pairs final int PAIRS = 50000000; // comparator final RandomIntPairGenerator generator = new RandomIntPairGenerator(12345678, PAIRS); final TypeSerializerFactory<IntPair> serializerFactory = new IntPairSerializer.IntPairSerializerFactory(); final TypeComparator<IntPair> comparator = new IntPairComparator(); // merge iterator LOG.debug("Initializing sortmerger..."); Sorter<IntPair> merger = new UnilateralSortMerger<IntPair>( this.memoryManager, this.ioManager, generator, this.parentTask, serializerFactory, comparator, (double) 64 / 78, 4, 0.7f); // emit data LOG.debug("Emitting data..."); // check order MutableObjectIterator<IntPair> iterator = merger.getIterator(); LOG.debug("Checking results..."); int pairsRead = 1; int nextStep = PAIRS / 20; IntPair rec1 = new IntPair(); IntPair rec2 = new IntPair(); Assert.assertTrue((rec1 = iterator.next(rec1)) != null); while ((rec2 = iterator.next(rec2)) != null) { final int k1 = rec1.getKey(); final int k2 = rec2.getKey(); pairsRead++; Assert.assertTrue(k1 - k2 <= 0); IntPair tmp = rec1; rec1 = rec2; rec2 = tmp; // log if (pairsRead == nextStep) { nextStep += PAIRS / 20; } } Assert.assertEquals("Not all pairs were read back in.", PAIRS, pairsRead); merger.close(); testSuccess = true; } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
@Test public void testSpillingSortWithIntermediateMerge() { try { // amount of pairs final int PAIRS = 10000000; // comparator final Comparator<TestData.Key> keyComparator = new TestData.KeyComparator(); final TestData.Generator generator = new TestData.Generator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM, ValueMode.FIX_LENGTH); final MutableObjectIterator<Record> source = new TestData.GeneratorIterator(generator, PAIRS); // merge iterator LOG.debug("Initializing sortmerger..."); Sorter<Record> merger = new UnilateralSortMerger<Record>( this.memoryManager, this.ioManager, source, this.parentTask, this.pactRecordSerializer, this.pactRecordComparator, (double) 64 / 78, 16, 0.7f); // emit data LOG.debug("Emitting data..."); // check order MutableObjectIterator<Record> iterator = merger.getIterator(); LOG.debug("Checking results..."); int pairsRead = 1; int nextStep = PAIRS / 20; Record rec1 = new Record(); Record rec2 = new Record(); Assert.assertTrue((rec1 = iterator.next(rec1)) != null); while ((rec2 = iterator.next(rec2)) != null) { final Key k1 = rec1.getField(0, TestData.Key.class); final Key k2 = rec2.getField(0, TestData.Key.class); pairsRead++; Assert.assertTrue(keyComparator.compare(k1, k2) <= 0); Record tmp = rec1; rec1 = rec2; k1.setKey(k2.getKey()); rec2 = tmp; // log if (pairsRead == nextStep) { nextStep += PAIRS / 20; } } Assert.assertEquals("Not all pairs were read back in.", PAIRS, pairsRead); merger.close(); testSuccess = true; } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
@Test public void testSpillingSort() { try { // comparator final Comparator<TestData.Key> keyComparator = new TestData.KeyComparator(); final TestData.Generator generator = new TestData.Generator( SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM, ValueMode.CONSTANT, VAL); final MutableObjectIterator<Record> source = new TestData.GeneratorIterator(generator, NUM_PAIRS); // merge iterator LOG.debug("Initializing sortmerger..."); Sorter<Record> merger = new UnilateralSortMerger<Record>( this.memoryManager, this.ioManager, source, this.parentTask, this.pactRecordSerializer, this.pactRecordComparator, (double) 16 / 78, 64, 0.7f); // emit data LOG.debug("Reading and sorting data..."); // check order MutableObjectIterator<Record> iterator = merger.getIterator(); LOG.debug("Checking results..."); int pairsEmitted = 1; Record rec1 = new Record(); Record rec2 = new Record(); Assert.assertTrue((rec1 = iterator.next(rec1)) != null); while ((rec2 = iterator.next(rec2)) != null) { final Key k1 = rec1.getField(0, TestData.Key.class); final Key k2 = rec2.getField(0, TestData.Key.class); pairsEmitted++; Assert.assertTrue(keyComparator.compare(k1, k2) <= 0); Record tmp = rec1; rec1 = rec2; k1.setKey(k2.getKey()); rec2 = tmp; } Assert.assertTrue(NUM_PAIRS == pairsEmitted); merger.close(); testSuccess = true; } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
public Object calculateStats(SeasonalAthlete seasonalAthlete) { return calculateStat(sorter.getPropertyName(), seasonalAthlete, sorter.aggregator); }
@Test public void sorterReturns0IfEqual() { assertEquals(0, sorter.sortCharacterArray(array2, array3)); }
/** * creates a Sorter initialized with sortKey * * @param sortKey the properties to use * @return <code>Sorter</code> initialized with the specified <code>sortKey</code> */ protected Sorter createDefaultSorter(SortKey sortKey) { Sorter sorter = createDefaultSorter(); sorter.setSortKey(sortKey); return sorter; }
@Test public void sorterReturns2IfInOrder() { assertEquals(1, sorter.sortCharacterArray(array1, array2)); }
@Test public void sorterReturns1IfOutOfOrder() { assertEquals(-1, sorter.sortCharacterArray(array2, array1)); }