public static <T> Pagination<T> build(final Long offset, final Long limit, final Collection<T> elements) { return build(offset, limit, elements.size(), elements); }
public static <T> Pagination<T> build(final Long offset, final Long limit, final Integer maxNbRecords, final Collection<T> elements) { final List<T> allResults = ImmutableList.<T>copyOf(elements); final List<T> results; if (offset >= allResults.size()) { results = ImmutableList.<T>of(); } else if (offset + limit > allResults.size()) { results = allResults.subList(offset.intValue(), allResults.size()); } else { results = allResults.subList(offset.intValue(), offset.intValue() + limit.intValue()); } return new DefaultPagination<T>(offset, limit, (long) results.size(), (long) maxNbRecords, results.iterator()); }
@Override public String toString() { final StringBuilder sb = new StringBuilder("DefaultPagination{"); sb.append("currentOffset=").append(currentOffset); sb.append(", nextOffset=").append(getNextOffset()); sb.append(", totalNbRecords=").append(totalNbRecords); sb.append(", maxNbRecords=").append(maxNbRecords); sb.append('}'); return sb.toString(); }
@Override public Pagination<Account> getAccounts(final Long offset, final Long limit, final TenantContext context) { return DefaultPagination.<Account>build(offset, limit, accounts); }
public static <E extends Entity, O, T extends BillingExceptionBase> Pagination<E> getEntityPagination(final Long limit, final SourcePaginationBuilder<O, T> sourcePaginationBuilder, final Function<O, E> function) throws T { final Pagination<O> modelsDao = sourcePaginationBuilder.build(); return new DefaultPagination<E>(modelsDao, limit, Iterators.<E>filter(Iterators.<O, E>transform(modelsDao.iterator(), function), Predicates.<E>notNull())); }
@Override public String toString() { final StringBuilder sb = new StringBuilder("DefaultPagination{"); sb.append("currentOffset=").append(currentOffset); sb.append(", nextOffset=").append(getNextOffset()); sb.append(", totalNbRecords=").append(totalNbRecords); sb.append(", maxNbRecords=").append(maxNbRecords); sb.append('}'); return sb.toString(); }
@Override public Pagination<M> get(final Long offset, final Long limit, final InternalTenantContext context) { return DefaultPagination.<M>build(offset, limit, ImmutableList.<M>copyOf(getAll(context))); }
private Pagination<Integer> expectedOf(final Long currentOffset, final Long totalNbRecords, final Long maxNbRecords, final List<Integer> delegate) { return new DefaultPagination<Integer>(currentOffset, Long.MAX_VALUE, totalNbRecords, maxNbRecords, delegate.iterator()); } }
@Test(groups = "fast", description = "Test Util: Pagination builder tests") public void testDefaultPaginationBuilder() throws Exception { Assert.assertEquals(DefaultPagination.<Integer>build(0L, 0L, ImmutableList.<Integer>of()), expectedOf(0L, 0L, 0L, ImmutableList.<Integer>of())); Assert.assertEquals(DefaultPagination.<Integer>build(0L, 10L, ImmutableList.<Integer>of()), expectedOf(0L, 0L, 0L, ImmutableList.<Integer>of())); Assert.assertEquals(DefaultPagination.<Integer>build(10L, 0L, ImmutableList.<Integer>of()), expectedOf(10L, 0L, 0L, ImmutableList.<Integer>of())); Assert.assertEquals(DefaultPagination.<Integer>build(10L, 10L, ImmutableList.<Integer>of()), expectedOf(10L, 0L, 0L, ImmutableList.<Integer>of())); Assert.assertEquals(DefaultPagination.<Integer>build(0L, 0L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(0L, 0L, 5L, ImmutableList.<Integer>of())); Assert.assertEquals(DefaultPagination.<Integer>build(0L, 10L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(0L, 5L, 5L, ImmutableList.<Integer>of(1, 2, 3, 4, 5))); Assert.assertEquals(DefaultPagination.<Integer>build(4L, 10L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(4L, 1L, 5L, ImmutableList.<Integer>of(5))); Assert.assertEquals(DefaultPagination.<Integer>build(0L, 3L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(0L, 3L, 5L, ImmutableList.<Integer>of(1, 2, 3))); Assert.assertEquals(DefaultPagination.<Integer>build(1L, 3L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(1L, 3L, 5L, ImmutableList.<Integer>of(2, 3, 4))); Assert.assertEquals(DefaultPagination.<Integer>build(2L, 3L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(2L, 3L, 5L, ImmutableList.<Integer>of(3, 4, 5))); Assert.assertEquals(DefaultPagination.<Integer>build(3L, 3L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(3L, 2L, 5L, ImmutableList.<Integer>of(4, 5))); Assert.assertEquals(DefaultPagination.<Integer>build(4L, 3L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(4L, 1L, 5L, ImmutableList.<Integer>of(5))); Assert.assertEquals(DefaultPagination.<Integer>build(5L, 3L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(5L, 0L, 5L, ImmutableList.<Integer>of())); }
return new DefaultPagination<AccountModelDao>(0L, 1L, accountModelDao == null ? 0L : 1L,
@Override public Pagination<SubscriptionBundleModelDao> searchSubscriptionBundles(final String searchKey, final Long offset, final Long limit, final InternalTenantContext context) { final List<SubscriptionBundleModelDao> results = new LinkedList<SubscriptionBundleModelDao>(); int maxNbRecords = 0; for (final SubscriptionBundleModelDao bundleModelDao : getAll(context)) { maxNbRecords++; if (bundleModelDao.getId().toString().equals(searchKey) || bundleModelDao.getExternalKey().equals(searchKey) || bundleModelDao.getAccountId().toString().equals(searchKey)) { results.add(bundleModelDao); } } return DefaultPagination.<SubscriptionBundleModelDao>build(offset, limit, maxNbRecords, results); }
@Override public Pagination<M> getAll(final InternalTenantContext context) { final List<M> result = new ArrayList<M>(); for (final Map<Long, M> cur : entities.values()) { result.add(cur.values().iterator().next()); } return new DefaultPagination<M>(getCount(context), result.iterator()); }
@Override public Pagination<Account> searchAccounts(final String searchKey, final Long offset, final Long limit, final TenantContext tenantContext) { final List<Account> results = new LinkedList<Account>(); for (final Account account : accounts) { if ((account.getName() != null && account.getName().contains(searchKey)) || (account.getEmail() != null && account.getEmail().contains(searchKey)) || (account.getExternalKey() != null && account.getExternalKey().contains(searchKey)) || (account.getCompanyName() != null && account.getCompanyName().contains(searchKey))) { results.add(account); } } return DefaultPagination.<Account>build(offset, limit, accounts.size(), results); }
@Override public Pagination<M> getAll(final InternalTenantContext context) { // We usually always want to wrap our queries in an EntitySqlDaoTransactionWrapper... except here. // Since we want to stream the results out, we don't want to auto-commit when this method returns. final EntitySqlDao<M, E> sqlDao = transactionalSqlDao.onDemandForStreamingResults(realSqlDao); // Note: we need to perform the count before streaming the results, as the connection // will be busy as we stream the results out. This is also why we cannot use // SQL_CALC_FOUND_ROWS / FOUND_ROWS (which may not be faster anyways). final Long count = sqlDao.getCount(context); final Iterator<M> results = sqlDao.getAll(context); return new DefaultPagination<M>(count, results); }
@Override public Pagination<AccountModelDao> searchAccounts(final String searchKey, final Long offset, final Long limit, final InternalTenantContext context) { final Collection<AccountModelDao> results = new LinkedList<AccountModelDao>(); int maxNbRecords = 0; for (final AccountModelDao account : getAll(context)) { maxNbRecords++; if ((account.getName() != null && account.getName().contains(searchKey)) || (account.getEmail() != null && account.getEmail().contains(searchKey)) || (account.getExternalKey() != null && account.getExternalKey().contains(searchKey)) || (account.getCompanyName() != null && account.getCompanyName().contains(searchKey))) { results.add(account); } } return DefaultPagination.<AccountModelDao>build(offset, limit, maxNbRecords, results); }
return new DefaultPagination<E>(offset, limit, totalNbRecords, maxNbRecords, allResults.iterator());
public static <T> Pagination<T> build(final Long offset, final Long limit, final Collection<T> elements) { return build(offset, limit, elements.size(), elements); }
return new DefaultPagination<M>(offset, limit, totalNbRecords, maxNbRecords, results);
@Override public Pagination<Account> getAccounts(final Long offset, final Long limit, final TenantContext context) { return DefaultPagination.<Account>build(offset, limit, accounts); }
public static <T> Pagination<T> build(final Long offset, final Long limit, final Integer maxNbRecords, final Collection<T> elements) { final List<T> allResults = ImmutableList.<T>copyOf(elements); final List<T> results; if (offset >= allResults.size()) { results = ImmutableList.<T>of(); } else if (offset + limit > allResults.size()) { results = allResults.subList(offset.intValue(), allResults.size()); } else { results = allResults.subList(offset.intValue(), offset.intValue() + limit.intValue()); } return new DefaultPagination<T>(offset, limit, (long) results.size(), (long) maxNbRecords, results.iterator()); }