@Test public void testSortedListHandlesSortEvents() { Comparator artistComparator = GlazedLists.beanPropertyComparator(Song.class, "artist"); Comparator songComparator = GlazedLists.beanPropertyComparator(Song.class, "song"); List<Comparable> expectedOrder; sortedList.setComparator(null); SortedList sortedAgain = new SortedList<Comparable>(sortedList, artistComparator); ListConsistencyListener.install(sortedAgain); unsortedList.add(new Song("Limp Bizkit", "Nookie")); unsortedList.add(new Song("Limp Bizkit", "Eat You Alive")); unsortedList.add(new Song("Limp Bizkit", "Rearranged")); unsortedList.add(new Song("Limp Bizkit", "The Unquestionable Truth")); unsortedList.add(new Song("Filter", "Welcome to the Fold")); unsortedList.add(new Song("Filter", "Take a Picture")); unsortedList.add(new Song("Filter", "Miss Blue")); unsortedList.add(new Song("Slipknot", "Wait and Bleed")); unsortedList.add(new Song("Slipknot", "Duality")); unsortedList.add(new Song("Godsmack", "Whatever")); unsortedList.add(new Song("Godsmack", "Running Blind")); expectedOrder = new ArrayList<>(unsortedList); Collections.sort(expectedOrder, artistComparator); assertEquals(expectedOrder, sortedAgain); sortedList.setComparator(songComparator); expectedOrder = new ArrayList<>(unsortedList); Collections.sort(expectedOrder, GlazedLists.chainComparators((List)Arrays.asList(artistComparator, songComparator))); assertEquals(expectedOrder, sortedAgain); sortedList.setComparator(null); expectedOrder = new ArrayList<>(unsortedList); Collections.sort(expectedOrder, artistComparator); assertEquals(expectedOrder, sortedAgain); unsortedList.clear(); Random dice = new Random(0); List<String> artists = GlazedListsTests.stringToList("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); for(int a = 0; a < 200; a++) { String randomArtist = artists.get(dice.nextInt(artists.size())); String randomSong = artists.get(dice.nextInt(artists.size())); unsortedList.add(new Song(randomArtist, randomSong)); } sortedList.setComparator(songComparator); expectedOrder = new ArrayList<>(unsortedList); Collections.sort(expectedOrder, GlazedLists.chainComparators((List)Arrays.asList(artistComparator, songComparator))); assertEquals(expectedOrder, sortedAgain); sortedList.setComparator(null); expectedOrder = new ArrayList<>(unsortedList); Collections.sort(expectedOrder, artistComparator); assertEquals(expectedOrder, sortedAgain); }
public void setComparator(Comparator<? super E> comparator) { this.comparator = comparator; SimpleTree previousSorted = sorted; final Comparator treeComparator; if(comparator != null) treeComparator = new ElementComparator(comparator); else treeComparator = new ElementRawOrderComparator(); sorted = new SimpleTree<Element>(treeComparator); if(previousSorted == null && unsorted == null) { unsorted = new SimpleTree<>(); for(int i = 0, n = source.size(); i < n; i++) { Element unsortedNode = unsorted.add(i, EMPTY_ELEMENT, 1); insertByUnsortedNode(unsortedNode); } return; } if(source.isEmpty()) return; for(SimpleTreeIterator<Element> i = new SimpleTreeIterator<>(unsorted); i.hasNext(); ) { i.next(); Element unsortedNode = i.node(); insertByUnsortedNode(unsortedNode); } int[] reorderMap = new int[size()]; int oldSortedIndex = 0; for(SimpleTreeIterator<Element> i = new SimpleTreeIterator<Element>(previousSorted); i.hasNext(); oldSortedIndex++) { i.next(); Element oldSortedNode = i.node(); Element unsortedNode = (Element)oldSortedNode.get(); Element newSortedNode = (Element)unsortedNode.get(); int newSortedIndex = sorted.indexOfNode(newSortedNode, ALL_COLORS); reorderMap[newSortedIndex] = oldSortedIndex; } updates.beginEvent(); updates.reorder(reorderMap); updates.commitEvent(); }
SortedList extends TransformedList<E,E> { public void setComparator(Comparator<? super E> comparator) { this.comparator = comparator; SimpleTree previousSorted = sorted; final Comparator treeComparator; if(comparator != null) treeComparator = new ElementComparator(comparator); else treeComparator = new ElementRawOrderComparator(); sorted = new SimpleTree<Element>(treeComparator); if(previousSorted == null && unsorted == null) { unsorted = new SimpleTree<>(); for(int i = 0, n = source.size(); i < n; i++) { Element unsortedNode = unsorted.add(i, EMPTY_ELEMENT, 1); insertByUnsortedNode(unsortedNode); } return; } if(source.isEmpty()) return; for(SimpleTreeIterator<Element> i = new SimpleTreeIterator<>(unsorted); i.hasNext(); ) { i.next(); Element unsortedNode = i.node(); insertByUnsortedNode(unsortedNode); } int[] reorderMap = new int[size()]; int oldSortedIndex = 0; for(SimpleTreeIterator<Element> i = new SimpleTreeIterator<Element>(previousSorted); i.hasNext(); oldSortedIndex++) { i.next(); Element oldSortedNode = i.node(); Element unsortedNode = (Element)oldSortedNode.get(); Element newSortedNode = (Element)unsortedNode.get(); int newSortedIndex = sorted.indexOfNode(newSortedNode, ALL_COLORS); reorderMap[newSortedIndex] = oldSortedIndex; } updates.beginEvent(); updates.reorder(reorderMap); updates.commitEvent(); } }
SortedList extends TransformedList<E,E> { public void setComparator(Comparator<? super E> comparator) { this.comparator = comparator; SimpleTree previousSorted = sorted; final Comparator treeComparator; if(comparator != null) treeComparator = new ElementComparator(comparator); else treeComparator = new ElementRawOrderComparator(); sorted = new SimpleTree<Element>(treeComparator); if(previousSorted == null && unsorted == null) { unsorted = new SimpleTree<>(); for(int i = 0, n = source.size(); i < n; i++) { Element unsortedNode = unsorted.add(i, EMPTY_ELEMENT, 1); insertByUnsortedNode(unsortedNode); } return; } if(source.isEmpty()) return; for(SimpleTreeIterator<Element> i = new SimpleTreeIterator<>(unsorted); i.hasNext(); ) { i.next(); Element unsortedNode = i.node(); insertByUnsortedNode(unsortedNode); } int[] reorderMap = new int[size()]; int oldSortedIndex = 0; for(SimpleTreeIterator<Element> i = new SimpleTreeIterator<Element>(previousSorted); i.hasNext(); oldSortedIndex++) { i.next(); Element oldSortedNode = i.node(); Element unsortedNode = (Element)oldSortedNode.get(); Element newSortedNode = (Element)unsortedNode.get(); int newSortedIndex = sorted.indexOfNode(newSortedNode, ALL_COLORS); reorderMap[newSortedIndex] = oldSortedIndex; } updates.beginEvent(); updates.reorder(reorderMap); updates.commitEvent(); } SortedList(EventList<E> source);  SortedList(EventList<E> source, Comparator<? super E> comparator); }
SortedList extends TransformedList<E,E> { public void setComparator(Comparator<? super E> comparator) { this.comparator = comparator; SimpleTree previousSorted = sorted; final Comparator treeComparator; if(comparator != null) treeComparator = new ElementComparator(comparator); else treeComparator = new ElementRawOrderComparator(); sorted = new SimpleTree<Element>(treeComparator); if(previousSorted == null && unsorted == null) { unsorted = new SimpleTree<>(); for(int i = 0, n = source.size(); i < n; i++) { Element unsortedNode = unsorted.add(i, EMPTY_ELEMENT, 1); insertByUnsortedNode(unsortedNode); } return; } if(source.isEmpty()) return; for(SimpleTreeIterator<Element> i = new SimpleTreeIterator<>(unsorted); i.hasNext(); ) { i.next(); Element unsortedNode = i.node(); insertByUnsortedNode(unsortedNode); } int[] reorderMap = new int[size()]; int oldSortedIndex = 0; for(SimpleTreeIterator<Element> i = new SimpleTreeIterator<Element>(previousSorted); i.hasNext(); oldSortedIndex++) { i.next(); Element oldSortedNode = i.node(); Element unsortedNode = (Element)oldSortedNode.get(); Element newSortedNode = (Element)unsortedNode.get(); int newSortedIndex = sorted.indexOfNode(newSortedNode, ALL_COLORS); reorderMap[newSortedIndex] = oldSortedIndex; } updates.beginEvent(); updates.reorder(reorderMap); updates.commitEvent(); } SortedList(EventList<E> source);  SortedList(EventList<E> source, Comparator<? super E> comparator); static SortedList<E> create(EventList<E> source); void setMode(int mode); int getMode(); @Override void listChanged(ListEvent<E> listChanges); Comparator<? super E> getComparator(); void setComparator(Comparator<? super E> comparator); @Override int indexOf(Object object); @Override int lastIndexOf(Object object); int sortIndex(Object object); int lastSortIndex(Object object); @Deprecated int indexOfSimulated(Object object); @Override boolean contains(Object object); @Override Iterator<E> iterator(); }
SortedList extends TransformedList<E,E> { public void setComparator(Comparator<? super E> comparator) { this.comparator = comparator; SimpleTree previousSorted = sorted; final Comparator treeComparator; if(comparator != null) treeComparator = new ElementComparator(comparator); else treeComparator = new ElementRawOrderComparator(); sorted = new SimpleTree<Element>(treeComparator); if(previousSorted == null && unsorted == null) { unsorted = new SimpleTree<>(); for(int i = 0, n = source.size(); i < n; i++) { Element unsortedNode = unsorted.add(i, EMPTY_ELEMENT, 1); insertByUnsortedNode(unsortedNode); } return; } if(source.isEmpty()) return; for(SimpleTreeIterator<Element> i = new SimpleTreeIterator<>(unsorted); i.hasNext(); ) { i.next(); Element unsortedNode = i.node(); insertByUnsortedNode(unsortedNode); } int[] reorderMap = new int[size()]; int oldSortedIndex = 0; for(SimpleTreeIterator<Element> i = new SimpleTreeIterator<Element>(previousSorted); i.hasNext(); oldSortedIndex++) { i.next(); Element oldSortedNode = i.node(); Element unsortedNode = (Element)oldSortedNode.get(); Element newSortedNode = (Element)unsortedNode.get(); int newSortedIndex = sorted.indexOfNode(newSortedNode, ALL_COLORS); reorderMap[newSortedIndex] = oldSortedIndex; } updates.beginEvent(); updates.reorder(reorderMap); updates.commitEvent(); } SortedList(EventList<E> source);  SortedList(EventList<E> source, Comparator<? super E> comparator); static SortedList<E> create(EventList<E> source); void setMode(int mode); int getMode(); @Override void listChanged(ListEvent<E> listChanges); Comparator<? super E> getComparator(); void setComparator(Comparator<? super E> comparator); @Override int indexOf(Object object); @Override int lastIndexOf(Object object); int sortIndex(Object object); int lastSortIndex(Object object); @Deprecated int indexOfSimulated(Object object); @Override boolean contains(Object object); @Override Iterator<E> iterator(); static final int STRICT_SORT_ORDER; static final int AVOID_MOVING_ELEMENTS; }