XCollectors { public static <T> Collector<T, ?, ArrayDeque<T>> last(final int limit) { return Collector.of( ArrayDeque<T>::new, (l, e) -> { if (l.size() >= limit) { l.removeFirst(); } l.addLast(e); }, (l1, l2) -> { throw new UnsupportedOperationException("Limiting collectors do not support combining"); } ); } private  XCollectors(); static Collector<T, ?, ArrayDeque<T>> last(final int limit); static Collector<T, ArrayDeque<T>,
         ArrayDeque<T>> last(final int limit, final X addIfLimited); static Collector<T, D, D> last(final Supplier<D> dqSupp,
          final int limit, final X addIfLimited); static void limitDequeue(final Deque<T> l1, final int limit, final T addIfLimited); static Collector<T, ?, R> filtering(final Predicate<? super T> predicate,
                  final Collector<? super T, A, R> downstream);  }
@Test public void testLimit() { ArrayDeque<Integer> collect = java.util.Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0) .stream().collect(XCollectors.last(5)); Assert.assertThat(collect, Matchers.contains(6, 7, 8, 9, 0)); Assert.assertEquals(5, collect.size()); }
@Test public void testLimit2() { ArrayDeque<Integer> collect = java.util.Arrays.asList(1, 2, 3, 4, 5).stream().collect(XCollectors.last(10)); Assert.assertThat(collect, Matchers.contains(1, 2, 3, 4, 5)); Assert.assertEquals(5, collect.size()); }
@Test public void testLimit3() { ArrayDeque<Integer> collect = java.util.Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0).stream() .collect(XCollectors.last(5, -1)); Assert.assertThat(collect, Matchers.contains(-1, 7, 8, 9, 0)); Assert.assertEquals(5, collect.size()); }
@Test public void testLimit4() { ArrayDeque<Integer> collect = java.util.Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0).stream() .collect(XCollectors.last(10, -1)); Assert.assertThat(collect, Matchers.contains(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)); Assert.assertEquals(10, collect.size()); }