/** Returns the reference to the last page. */ public PageRef getLastPageRef() { final long lastPageItems = totalCount % pageSize; if (lastPageItems == 0) { return new PageRef(totalCount <= pageSize ? 0 : totalCount - pageSize, pageSize); } return new PageRef(totalCount - lastPageItems, pageSize); }
private PagedIterator(PageSupplier<E, ?> supplier, int size) { this.supplier = supplier; this.size = size; fetchPage(0); }
private static void checkArgument(boolean expression, String error) throws ValidationException { if (!expression) { throw new ValidationException(error); } }
@Override public void update(ProfileImpl profile) throws NotFoundException, ServerException { throw new ServerException("Given operation doesn't supported on current configured storage."); }
private static <E, X extends Exception> List<E> eagerFetch(PageSupplier<E, X> supplier, int size) throws X { Page<? extends E> page = supplier.getPage(size, 0); ArrayList<E> container = new ArrayList<>(page.hasNextPage() ? page.getItemsCount() * 2 : page.getItemsCount()); while (page.hasNextPage()) { container.addAll(page.getItems()); PageRef next = page.getNextPageRef(); page = supplier.getPage(next.getPageSize(), next.getItemsBefore()); } container.addAll(page.getItems()); return container; }
private Workspace doUpdate(String id, Workspace update) throws BadRequestException, ConflictException, NotFoundException, ServerException { try { return workspaceManager.updateWorkspace(id, update); } catch (ValidationException x) { throw new BadRequestException(x.getMessage()); } }
/** * Checks object reference is not {@code null} * * @param object object reference to check * @param subject used as subject of exception message "{subject} required" * @throws BadRequestException when object reference is {@code null} */ private void requiredNotNull(Object object, String subject) throws BadRequestException { if (object == null) { throw new BadRequestException(subject + " required"); } }
@Override public void checkPermission(String domain, String instance, String action) throws ForbiddenException { throw new ForbiddenException( "User is not authorized to perform " + action + " of " + domain + " with id '" + instance + "'"); }
@Override public boolean hasNext() { if (delegate.hasNext()) { return true; } if (!page.hasNextPage()) { return false; } fetchPage(page.getNextPageRef().getItemsBefore()); return delegate.hasNext(); }
/** * Fetches elements like {@link #stream(PageSupplier, int)} method does using default page size * which is equal to {@value #DEFAULT_PAGE_SIZE}. */ public static <E, X extends Exception> Stream<E> stream(PageSupplier<E, X> supplier) throws X { return stream(supplier, DEFAULT_PAGE_SIZE); }
/** * Fetches elements like {@link #iterate(PageSupplier, int)} method does using default page size * which is equal to {@value #DEFAULT_PAGE_SIZE}. */ public static <E, X extends Exception> Iterable<E> iterate(PageSupplier<E, X> supplier) throws X { return iterate(supplier, DEFAULT_PAGE_SIZE); }
/** * Fetches elements like {@link #streamLazily(PageSupplier, int)} method does using default page * size which is equal to {@value #DEFAULT_PAGE_SIZE}. */ public static <E, X extends Exception> Stream<E> streamLazily(PageSupplier<E, X> supplier) { return streamLazily(supplier, DEFAULT_PAGE_SIZE); }
/** * Returns true when this page has the previous page, otherwise when the page is the first page * false will be returned. */ public boolean hasPreviousPage() { return getNumber() != -1 && itemsBefore != 0; }
@Override public Iterator<E> iterator() { return new PagedIterator<>(supplier, size); } }
/** * Returns an iterable like {@link #streamLazily(PageSupplier, int)} method does using default * page size which is equal to {@value #DEFAULT_PAGE_SIZE}. */ public static <E, X extends Exception> Iterable<E> iterateLazily(PageSupplier<E, X> supplier) { return iterateLazily(supplier, DEFAULT_PAGE_SIZE); }
@Override public void checkPermission(String domain, String instance, String action) throws ForbiddenException { throw new ForbiddenException("User is not authorized to perform operation"); }
/** * Checks that object reference is not null, throws {@link ValidationException} in the case of * null {@code object} with given {@code message}. */ private static void checkNotNull(Object object, String message) throws ValidationException { if (object == null) { throw new ValidationException(message); } }
/** * Returns a reference to the next page. * * <p>Note: This method was designed to be used in couple with {@link #hasNextPage()}. Returns * reference to the next page even when {@link #hasNextPage()} returns false. */ public PageRef getNextPageRef() { return new PageRef(itemsBefore + pageSize, pageSize); }
/** * Checks that expression is true, throws {@link ValidationException} otherwise. * * <p>Exception uses error message built from error message template and error message parameters. */ private static void check(boolean expression, String message) throws ValidationException { if (!expression) { throw new ValidationException(message); } } }
private static void checkArgument(boolean expression, String error) throws ValidationException { if (!expression) { throw new ValidationException(error); } }