HTTPResponse { public <A> Optional<A> transform(final ThrowableFunction<Payload, A, IOException> f) { if (hasPayload()) { return payload.flatMap(p -> lift(p, f)); } return Optional.empty(); } HTTPResponse(Status status, Headers headers);  HTTPResponse(Optional<Payload> payload, Status status, Headers headers);  HTTPResponse(Optional<Payload> payload, StatusLine statusLine, Headers headers); @Internal HTTPResponse withHeaders(Headers headers); @Internal HTTPResponse withPayload(Payload payload); boolean hasPayload(); Status getStatus(); StatusLine getStatusLine(); Optional<Payload> getPayload(); Headers getHeaders(); boolean isCached(); Optional<A> transform(final ThrowableFunction<Payload, A, IOException> f); void consume(); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void transformShouldGiveUseSomethingUseful() { HTTPResponse response = new HTTPResponse(Optional.of(new StringPayload("Hello", MIMEType.valueOf("text/plain"))), Status.OK, new Headers()); Optional<String> result = response.transform(payload -> { assertEquals(MIMEType.valueOf("text/plain"), payload.getMimeType()); try(BufferedReader reader = new BufferedReader(new InputStreamReader(payload.getInputStream()))) { return reader.lines().collect(Collectors.joining("\n")); } catch (IOException e) { fail("Exception raised when parsing string"); throw new RuntimeException(e); } }); assertTrue(result.isPresent()); assertEquals("Hello", result.get()); }
@Test public void transformWithouthavingtoDealWithException() { HTTPResponse response = new HTTPResponse(Optional.of(new StringPayload("Hello", MIMEType.valueOf("text/plain"))), Status.OK, new Headers()); Optional<String> result = response.transform(payload -> { assertEquals(MIMEType.valueOf("text/plain"), payload.getMimeType()); try(BufferedReader reader = new BufferedReader(new InputStreamReader(payload.getInputStream()))) { return reader.lines().collect(Collectors.joining("\n")); } }); assertTrue(result.isPresent()); assertEquals("Hello", result.get()); }