RestUtils { public static <T extends Thing> Response createPaginatedThingResponse(UriInfo uriInfo, Set<T> things, IRI sortIRI, int offset, int limit, boolean asc, Function<T, Boolean> filterFunction, String type, SesameTransformer transformer, BNodeService bNodeService) { long start = System.currentTimeMillis(); try { if (offset > things.size()) { throw ErrorUtils.sendError("Offset exceeds total size", Response.Status.BAD_REQUEST); } Comparator<T> comparator = Comparator.comparing(dist -> dist.getProperty(sortIRI).get().stringValue()); Stream<T> stream = things.stream(); if (!asc) { comparator = comparator.reversed(); } if (filterFunction != null) { stream = stream.filter(filterFunction::apply); } List<T> filteredThings = stream.collect(Collectors.toList()); List<T> result = filteredThings.stream() .sorted(comparator) .skip(offset) .limit(limit) .collect(Collectors.toList()); return createPaginatedResponse(uriInfo, result, filteredThings.size(), limit, offset, type, transformer, bNodeService); } finally { LOG.trace("createPaginatedThingResponse took {}ms", System.currentTimeMillis() - start); } }  static RDFFormat getRDFFormat(String format); static String modelToString(Model model, RDFFormat format, SesameTransformer transformer); static String modelToString(Model model, String format, SesameTransformer transformer); static String modelToSkolemizedString(Model model, RDFFormat format, SesameTransformer transformer,
                                                 BNodeService service); static String modelToSkolemizedString(Model model, String format, SesameTransformer transformer,
                                                 BNodeService service); static String groupedModelToString(Model model, RDFFormat format, SesameTransformer transformer); static String groupedModelToString(Model model, String format, SesameTransformer transformer); static String groupedModelToSkolemizedString(Model model, RDFFormat format, SesameTransformer transformer,
                                                        BNodeService service); static String groupedModelToSkolemizedString(Model model, String format, SesameTransformer transformer,
                                                        BNodeService service); static Model jsonldToModel(String jsonld, SesameTransformer transformer); static Model jsonldToDeskolemizedModel(String jsonld, SesameTransformer transformer, BNodeService service); static String modelToJsonld(Model model, SesameTransformer transformer); static String modelToSkolemizedJsonld(Model model, SesameTransformer transformer, BNodeService service); static String modelToTrig(Model model, SesameTransformer transformer); static String getRDFFormatFileExtension(String format); static String getRDFFormatMimeType(String format); static User getActiveUser(ContainerRequestContext context, EngineManager engineManager); static String getActiveUsername(ContainerRequestContext context); static Optional<User> optActiveUser(ContainerRequestContext context, EngineManager engineManager); static Optional<String> optActiveUsername(ContainerRequestContext context); static void checkStringParam(@Nullable String param, String errorMessage); static JSONObject getObjectFromJsonld(String json); static ObjectNode getObjectNodeFromJsonld(String json); static JSONObject getTypedObjectFromJsonld(String json, String type); static JsonNode getTypedObjectNodeFromJsonld(String json, String type); static IRI createIRI(String requestId, ValueFactory vf); static JSONObject thingToJsonObject(Thing thing, String type, SesameTransformer transformer); static JsonNode thingToObjectNode(Thing thing, String type, SesameTransformer transformer); static JSONObject thingToSkolemizedJsonObject(Thing thing, String type, SesameTransformer transformer,
                                                         BNodeService bNodeService); static JsonNode thingToSkolemizedObjectNode(Thing thing, String type, SesameTransformer transformer,
                                                         BNodeService bNodeService); static Response createPaginatedThingResponse(UriInfo uriInfo, Set<T> things,
                                                                          IRI sortIRI, int offset, int limit,
                                                                          boolean asc,
                                                                          Function<T, Boolean> filterFunction,
                                                                          String type, SesameTransformer transformer,
                                                                          BNodeService bNodeService); static Response createPaginatedThingResponseJackson(UriInfo uriInfo, Set<T> things,
                                                                          IRI sortIRI, int offset, int limit,
                                                                          boolean asc,
                                                                          Function<T, Boolean> filterFunction,
                                                                          String type, SesameTransformer transformer,
                                                                          BNodeService bNodeService); static Response createPaginatedResponse(UriInfo uriInfo, Collection<T> items,
                                                                     int totalSize, int limit, int offset, String type,
                                                                     SesameTransformer transformer); static Response createPaginatedResponse(UriInfo uriInfo, Collection<T> items,
                                                                     int totalSize, int limit, int offset,
                                                                     String type, SesameTransformer transformer,
                                                                      BNodeService bNodeService); static Response createPaginatedResponseJackson(UriInfo uriInfo, Collection<T> items,
                                                                            int totalSize, int limit,
                                                                            int offset, String type,
                                                                            SesameTransformer transformer,
                                                                            BNodeService bNodeService); static Response createPaginatedResponseWithJson(UriInfo uriInfo, JSONArray items, int totalSize, int limit,
                                                           int offset); static Response createPaginatedResponseWithJsonNode(UriInfo uriInfo, ArrayNode items,
                                                               int totalSize, int limit, int offset); static void validatePaginationParams(String sortIRI, Set<String> sortResources, int limit, int offset);  }
@Test public void createPaginatedResponseTestFiltered() { Set<Thing> set = getTestThings(Collections.singletonMap(testPropIRI, "VALUE")); Function<Thing, Boolean> f; f = new Function<Thing, Boolean>() { @Override public Boolean apply(Thing t) { return !t.getProperties(testPropIRI).contains(vf.createLiteral("VALUE 2")); } }; Response response = RestUtils.createPaginatedThingResponse(uriInfo, set, testPropIRI, 0, 1, true, f, "http: Object object = response.getEntity(); assertTrue(object instanceof JSONArray); JSONArray array = (JSONArray) object; assertTrue(array.get(0) instanceof JSONObject); JSONObject jsonObject = (JSONObject) array.get(0); assertEquals(jsonObject.getJSONArray(testPropIRI.stringValue()).getJSONObject(0).getString("@value"), "VALUE 1"); assertEquals(response.getLinks().size(), 1); Link link = response.getLinks().iterator().next(); assertEquals(link.getRel(), "next"); assertTrue(link.getUri().getRawPath().equals("/rest/tests")); response = RestUtils.createPaginatedThingResponse(uriInfo, set, testPropIRI, 1, 1, true, f, "http: object = response.getEntity(); assertTrue(object instanceof JSONArray); array = (JSONArray) object; assertTrue(array.get(0) instanceof JSONObject); jsonObject = (JSONObject) array.get(0); assertEquals(jsonObject.getJSONArray(testPropIRI.stringValue()).getJSONObject(0).getString("@value"), "VALUE 3"); assertEquals(response.getLinks().size(), 1); link = response.getLinks().iterator().next(); assertEquals(link.getRel(), "prev"); assertTrue(link.getUri().getRawPath().equals("/rest/tests")); }