LimitHandler implements OutputOperationHandler<Limit<T>, Iterable<? extends T>> { @Override public Iterable<? extends T> doOperation(final Limit<T> operation, final Context context, final Store store) throws OperationException { if (null == operation.getInput()) { return null; } if (null != operation.getResultLimit()) { return new LimitedCloseableIterable<>(operation.getInput(), 0, operation .getResultLimit(), operation.getTruncate()); } return operation.getInput(); }  @Override Iterable<? extends T> doOperation(final Limit<T> operation, final Context context, final Store store);  }
@Test public void shouldLimitResults() throws Exception { final List<Integer> input = Arrays.asList(1, 2, 3, 4, 5); final List<Integer> expectedResult = Arrays.asList(1, 2, 3); final Integer resultLimit = expectedResult.size(); final Limit<Integer> limit = new Limit.Builder<Integer>() .input(input) .resultLimit(resultLimit) .build(); final LimitHandler<Integer> handler = new LimitHandler<>(); final Iterable<? extends Integer> result = handler.doOperation(limit, null, null); assertTrue(result instanceof LimitedCloseableIterable); assertEquals(0, ((LimitedCloseableIterable) result).getStart()); assertEquals(resultLimit, ((LimitedCloseableIterable) result).getEnd()); assertEquals(expectedResult, Lists.newArrayList(result)); }
@Test public void shouldNotLimitResultsOfGetOperationWhenLimitIsNull() throws Exception { final CloseableIterable<Integer> input = new WrappedCloseableIterable<>(Arrays.asList(1, 2, 3, 4, 5)); final Integer resultLimit = null; final Limit<Integer> limit = new Limit.Builder<Integer>() .input(input) .resultLimit(resultLimit) .build(); final LimitHandler<Integer> handler = new LimitHandler<>(); final Iterable<? extends Integer> result = handler.doOperation(limit, null, null); assertSame(input, result); }
@Test public void shouldHandleNullInput() throws Exception { final CloseableIterable<Integer> input = null; final Limit<Integer> limit = new Limit.Builder<Integer>() .input(input) .build(); final LimitHandler<Integer> handler = new LimitHandler<>(); final Iterable<? extends Integer> result = handler.doOperation(limit, null, null); assertThat(result, is(nullValue())); }