Last active
February 26, 2026 19:30
-
-
Save MichalBrylka/0c9e08d8267f5b890bb64fe00b999e3e to your computer and use it in GitHub Desktop.
PagerResult
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| String inputJson = """ | |
| { | |
| "b": 2, | |
| "a": { | |
| "x": 10, | |
| "y": [1, 2, 3] | |
| } | |
| } | |
| """; | |
| ObjectMapper mapper = new ObjectMapper(); | |
| // Read as generic tree (schema agnostic) | |
| JsonNode node = mapper.readTree(inputJson); | |
| // Ensure compact output (no indentation, no newlines) | |
| mapper.disable(SerializationFeature.INDENT_OUTPUT); | |
| String compactJson = mapper.writeValueAsString(node); | |
| System.out.println(compactJson); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.net.URI; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.Objects; | |
| public record PagedResult<T>( | |
| List<T> content, | |
| int pageNumber, // 1-based | |
| int pageSize, | |
| long totalElements, | |
| int totalPages, | |
| Map<LinkRelation, URI> links | |
| ) { | |
| public PagedResult { | |
| Objects.requireNonNull(content, "content must not be null"); | |
| Objects.requireNonNull(links, "links must not be null"); | |
| if (pageNumber < 1) | |
| throw new IllegalArgumentException("pageNumber must be >= 1"); | |
| if (pageSize <= 0) | |
| throw new IllegalArgumentException("pageSize must be > 0"); | |
| if (totalElements < 0) | |
| throw new IllegalArgumentException("totalElements must be >= 0"); | |
| int calculatedTotalPages = calculateTotalPages(totalElements, pageSize); | |
| if (totalPages != calculatedTotalPages) | |
| throw new IllegalArgumentException( | |
| "totalPages must equal calculated value: " + calculatedTotalPages | |
| ); | |
| if (totalPages > 0 && pageNumber > totalPages) | |
| throw new IllegalArgumentException("pageNumber cannot exceed totalPages"); | |
| // Defensive copies for immutability | |
| content = List.copyOf(content); | |
| links = Map.copyOf(links); | |
| } | |
| private static int calculateTotalPages(long totalElements, int pageSize) { | |
| if (totalElements == 0) { | |
| return 0; | |
| } | |
| return (int) Math.ceil((double) totalElements / pageSize); | |
| } | |
| public boolean hasNext() { | |
| return pageNumber < totalPages; | |
| } | |
| public boolean hasPrevious() { | |
| return pageNumber > 1; | |
| } | |
| public enum LinkRelation { | |
| SELF, | |
| FIRST, | |
| PREVIOUS, | |
| NEXT, | |
| LAST | |
| } | |
| } | |
| PagedResult<String> result = new PagedResult<>( | |
| List.of("A", "B", "C"), | |
| 2, | |
| 3, | |
| 10, | |
| 4, | |
| Map.of( | |
| PagedResult.LinkRelation.SELF, URI.create("/items?page=2"), | |
| PagedResult.LinkRelation.NEXT, URI.create("/items?page=3"), | |
| PagedResult.LinkRelation.PREVIOUS, URI.create("/items?page=1") | |
| ) | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment