IndexedTraverserSet extends TraverserSet<S> { public Collection<Traverser.Admin<S>> get(final I k) { final Collection<Traverser.Admin<S>> c = index.getCollection(k); return c != null && c.isEmpty() ? null : c; } IndexedTraverserSet(final Function<S, I> indexingFunction);  IndexedTraverserSet(final Function<S, I> indexingFunction, final Traverser.Admin<S> traverser); @Override void clear(); @Override boolean add(final Traverser.Admin<S> traverser); Collection<Traverser.Admin<S>> get(final I k); @Override boolean offer(final Traverser.Admin<S> traverser); @Override Traverser.Admin<S> remove(); @Override boolean remove(final Object traverser);  }
@Test public void shouldIndexTraversers() { final IndexedTraverserSet<String, String> ts = makeTraverserSet(); final List<Traverser.Admin<String>> testTraversers = new ArrayList<>(ts.get("test")); assertEquals(1, testTraversers.size()); assertEquals(10, testTraversers.get(0).bulk()); final List<Traverser.Admin<String>> nopeTraversers = new ArrayList<>(ts.get("nope")); assertEquals(1, nopeTraversers.size()); assertEquals(1, nopeTraversers.get(0).bulk()); }
@Test public void shouldMaintainIndexOfTraversersAfterShuffle() { final IndexedTraverserSet<String, String> ts = makeOtherTraverserSet(); final List<Traverser.Admin<String>> testTraversers = new ArrayList<>(ts.get("test")); assertEquals(1, testTraversers.size()); assertEquals(4, testTraversers.get(0).bulk()); final List<Traverser.Admin<String>> nopeTraversers = new ArrayList<>(ts.get("nope")); assertEquals(1, nopeTraversers.size()); assertEquals(1, nopeTraversers.get(0).bulk()); ts.shuffle(new Random()); final List<Traverser.Admin<String>> testTraversersAfterShuffle = new ArrayList<>(ts.get("test")); assertEquals(1, testTraversersAfterShuffle.size()); assertEquals(4, testTraversersAfterShuffle.get(0).bulk()); final List<Traverser.Admin<String>> nopeTraversersAfterShuffle = new ArrayList<>(ts.get("nope")); assertEquals(1, nopeTraversersAfterShuffle.size()); assertEquals(1, nopeTraversersAfterShuffle.get(0).bulk()); }
@Test public void shouldMaintainIndexOfTraversersAfterSort() { final IndexedTraverserSet<String, String> ts = makeOtherTraverserSet(); final List<Traverser.Admin<String>> testTraversers = new ArrayList<>(ts.get("test")); assertEquals(1, testTraversers.size()); assertEquals(4, testTraversers.get(0).bulk()); final List<Traverser.Admin<String>> nopeTraversers = new ArrayList<>(ts.get("nope")); assertEquals(1, nopeTraversers.size()); assertEquals(1, nopeTraversers.get(0).bulk()); final List<Traverser<String>> traversersBefore = IteratorUtils.asList(ts.iterator()); ts.sort(Comparator.comparing(Traverser::get)); final List<Traverser<String>> traversersAfter = IteratorUtils.asList(ts.iterator()); final List<Traverser.Admin<String>> testTraversersAfterSort = new ArrayList<>(ts.get("test")); assertEquals(1, testTraversersAfterSort.size()); assertEquals(4, testTraversersAfterSort.get(0).bulk()); final List<Traverser.Admin<String>> nopeTraversersAfterSort = new ArrayList<>(ts.get("nope")); assertEquals(1, nopeTraversersAfterSort.size()); assertEquals(1, nopeTraversersAfterSort.get(0).bulk()); assertNotEquals(traversersBefore, traversersAfter); }