|
1 | 1 | package com.retailsvc.http.internal; |
2 | 2 |
|
| 3 | +import java.util.Arrays; |
3 | 4 | import java.util.Map; |
| 5 | +import java.util.Objects; |
4 | 6 |
|
5 | 7 | /** |
6 | 8 | * Immutable per-request data populated by {@link RequestPreparationFilter} and read by handlers via |
7 | 9 | * {@link com.retailsvc.http.Request}. Bound to a {@link ScopedValue} for the duration of a single |
8 | 10 | * request — never written to the {@code HttpExchange}'s context-shared attribute map. |
| 11 | + * |
| 12 | + * <p>{@code equals}, {@code hashCode}, and {@code toString} are overridden because the record |
| 13 | + * carries a {@code byte[]} component: the auto-generated implementations use reference equality on |
| 14 | + * arrays, which would treat structurally-equal contexts as different. |
9 | 15 | */ |
10 | 16 | public record RequestContext( |
11 | | - byte[] body, Object parsedBody, String operationId, Map<String, String> pathParameters) {} |
| 17 | + byte[] body, Object parsedBody, String operationId, Map<String, String> pathParameters) { |
| 18 | + |
| 19 | + @Override |
| 20 | + public boolean equals(Object o) { |
| 21 | + if (this == o) { |
| 22 | + return true; |
| 23 | + } |
| 24 | + if (!(o instanceof RequestContext other)) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + return Arrays.equals(body, other.body) |
| 28 | + && Objects.equals(parsedBody, other.parsedBody) |
| 29 | + && Objects.equals(operationId, other.operationId) |
| 30 | + && Objects.equals(pathParameters, other.pathParameters); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public int hashCode() { |
| 35 | + return Objects.hash(Arrays.hashCode(body), parsedBody, operationId, pathParameters); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public String toString() { |
| 40 | + return "RequestContext[body=byte[" |
| 41 | + + (body == null ? 0 : body.length) |
| 42 | + + "], parsedBody=" |
| 43 | + + parsedBody |
| 44 | + + ", operationId=" |
| 45 | + + operationId |
| 46 | + + ", pathParameters=" |
| 47 | + + pathParameters |
| 48 | + + "]"; |
| 49 | + } |
| 50 | +} |
0 commit comments