Use Records instead of normal classes

JDK-16 has support for Records which are much superior here
dev/jdk-16
Hammy 3 years ago
parent 3598994fcd
commit 21fdc8d077

@ -50,8 +50,8 @@ class APIMapper {
* *
*/ */
<T> Response<T> deserialize(Result result, Class<T> model) throws APIMapperException { <T> Response<T> deserialize(Result result, Class<T> model) throws APIMapperException {
Integer statusCode = result.getStatusCode(); Integer statusCode = result.statusCode();
String body = result.getBody(); String body = result.body();
T newModel = null; T newModel = null;
if (statusCode == 200) { if (statusCode == 200) {
@ -78,8 +78,8 @@ class APIMapper {
* *
*/ */
<T> Response<List<T>> deserializeToList(Result result, JavaType model) throws APIMapperException { <T> Response<List<T>> deserializeToList(Result result, JavaType model) throws APIMapperException {
Integer statusCode = result.getStatusCode(); Integer statusCode = result.statusCode();
String body = result.getBody(); String body = result.body();
List<T> listOfModels = null; List<T> listOfModels = null;
if (statusCode == 200) { if (statusCode == 200) {
@ -106,8 +106,8 @@ class APIMapper {
* *
*/ */
<T> Response<PaginationData<T>> deserializeToPaginationData(Result result, JavaType model) throws APIMapperException { <T> Response<PaginationData<T>> deserializeToPaginationData(Result result, JavaType model) throws APIMapperException {
Integer statusCode = result.getStatusCode(); Integer statusCode = result.statusCode();
String body = result.getBody(); String body = result.body();
PaginationData<T> newModel = null; PaginationData<T> newModel = null;
if (statusCode == 200) { if (statusCode == 200) {

@ -3,8 +3,6 @@ package me.goudham;
import me.goudham.domain.series.Series; import me.goudham.domain.series.Series;
import me.goudham.domain.waifu.Waifu; import me.goudham.domain.waifu.Waifu;
import java.util.Objects;
/** /**
* This is returned to the User when called by methods in {@link MyWaifuClient}. * This is returned to the User when called by methods in {@link MyWaifuClient}.
* E.g {@link MyWaifuClient#getWaifu(Integer)} * E.g {@link MyWaifuClient#getWaifu(Integer)}
@ -15,51 +13,9 @@ import java.util.Objects;
* will be populated to ensure the user has all the information for debugging or extra information within * will be populated to ensure the user has all the information for debugging or extra information within
* the {@link #body} * the {@link #body}
* *
* @param statusCode The status code returned by the API Response
* @param body The body returned by the the API Response
* @param <T> The type of model to be returned. E.g {@link Waifu} or {@link Series} * @param <T> The type of model to be returned. E.g {@link Waifu} or {@link Series}
* *
*/ */
public class Response<T> { public record Response<T>(Integer statusCode, String body, T model) { }
private final T model;
private final Integer statusCode;
private final String body;
Response(Integer statusCode, String body, T model) {
this.statusCode = statusCode;
this.body = body;
this.model = model;
}
public T getModel() {
return model;
}
public Integer getStatusCode() {
return statusCode;
}
public String getBody() {
return body;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Response<?> response = (Response<?>) o;
return Objects.equals(model, response.model) && Objects.equals(statusCode, response.statusCode) && Objects.equals(body, response.body);
}
@Override
public int hashCode() {
return Objects.hash(model, statusCode, body);
}
@Override
public String toString() {
return "Response{" +
"model=" + model +
", statusCode=" + statusCode +
", body='" + body + '\'' +
'}';
}
}

@ -1,47 +1,13 @@
package me.goudham; package me.goudham;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.util.Objects;
/** /**
* Represents a Result from a {@link HttpRequest} with the resulting * Represents a Result from a {@link HttpRequest} with the resulting
* {@code statusCode} and {@code body} * {@code statusCode} and {@code body}
*
* @param statusCode The status code returned by the API Response
* @param body The body returned by the the API Response
*
*/ */
class Result { record Result(Integer statusCode, String body) { }
private final Integer statusCode;
private final String body;
Result(Integer statusCode, String body) {
this.statusCode = statusCode;
this.body = body;
}
Integer getStatusCode() {
return statusCode;
}
String getBody() {
return body;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Result result = (Result) o;
return Objects.equals(statusCode, result.statusCode) && Objects.equals(body, result.body);
}
@Override
public int hashCode() {
return Objects.hash(statusCode, body);
}
@Override
public String toString() {
return "Result{" +
"statusCode=" + statusCode +
", body='" + body + '\'' +
'}';
}
}

@ -1,12 +1,8 @@
package me.goudham.domain.pagination; package me.goudham.domain.pagination;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.util.Objects;
/** /**
* {@link Links} * {@link Links}
* <p>Contains gallery API links for {@link Meta}</p> * <p>Contains gallery API links for {@link Meta}</p>
@ -27,99 +23,4 @@ import java.util.Objects;
"next", "next",
"prev" "prev"
}) })
public class Links { record Links(String first, String last, String next, String prev) { }
/**
* First page of the gallery
*
*/
@JsonProperty("first")
@JsonPropertyDescription("First page of the gallery")
private String first;
/**
* Last page of the gallery
*
*/
@JsonProperty("last")
@JsonPropertyDescription("Last page of the gallery")
private String last;
/**
* Next page of the gallery
*
*/
@JsonProperty("next")
@JsonPropertyDescription("Next page of the gallery")
private String next;
/**
* Previous page of the gallery
*
*/
@JsonProperty("prev")
@JsonPropertyDescription("Previous page of the gallery")
private String prev;
@JsonProperty("first")
public String getFirst() {
return first;
}
@JsonProperty("first")
public void setFirst(String first) {
this.first = first;
}
@JsonProperty("last")
public String getLast() {
return last;
}
@JsonProperty("last")
public void setLast(String last) {
this.last = last;
}
@JsonProperty("next")
public String getNext() {
return next;
}
@JsonProperty("next")
public void setNext(String next) {
this.next = next;
}
@JsonProperty("prev")
public String getPrev() {
return prev;
}
@JsonProperty("prev")
public void setPrev(String prev) {
this.prev = prev;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Links links = (Links) o;
return Objects.equals(first, links.first) && Objects.equals(last, links.last) && Objects.equals(next, links.next) && Objects.equals(prev, links.prev);
}
@Override
public int hashCode() {
return Objects.hash(first, last, next, prev);
}
@Override
public String toString() {
return "Links{" +
"first='" + first + '\'' +
", last='" + last + '\'' +
", next='" + next + '\'' +
", prev='" + prev + '\'' +
'}';
}
}

@ -2,11 +2,8 @@ package me.goudham.domain.pagination;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.util.Objects;
/** /**
* {@link Meta} * {@link Meta}
@ -34,144 +31,10 @@ import java.util.Objects;
"to", "to",
"total" "total"
}) })
public class Meta { record Meta(@JsonProperty("current_page") String currentPage,
/** Integer from,
* Current requested page @JsonProperty("last_page") Integer lastPage,
* String path,
*/ @JsonProperty("per_page") Integer perPage,
@JsonProperty("current_page") Integer to,
@JsonPropertyDescription("Current requested page") Integer total) { }
private Integer currentPage;
/**
* Starting gallery image number
*
*/
@JsonProperty("from")
@JsonPropertyDescription("Starting gallery image number")
private Integer from;
/**
* Last available page
*
*/
@JsonProperty("last_page")
@JsonPropertyDescription("Last available page")
private Integer lastPage;
/**
* API url for gallery
*
*/
@JsonProperty("path")
@JsonPropertyDescription("API url for gallery")
private String path;
/**
* Total number of items per page
*
*/
@JsonProperty("per_page")
@JsonPropertyDescription("Total number of items per page")
private Integer perPage;
/**
* Last gallery image number
*
*/
@JsonProperty("to")
@JsonPropertyDescription("Last gallery image number")
private Integer to;
/**
* Total number of items within the gallery
*
*/
@JsonProperty("total")
@JsonPropertyDescription("Total number of items")
private Integer total;
@JsonProperty("current_page")
public Integer getCurrentPage() {
return currentPage;
}
@JsonProperty("current_page")
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
@JsonProperty("from")
public Integer getFrom() { return from; }
@JsonProperty("from")
public void setFrom(Integer from) { this.from = from; }
@JsonProperty("last_page")
public Integer getLastPage() {
return lastPage;
}
@JsonProperty("last_page")
public void setLastPage(Integer lastPage) {
this.lastPage = lastPage;
}
@JsonProperty("path")
public String getPath() { return path; }
@JsonProperty("path")
public void setPath(String path) { this.path = path; }
@JsonProperty("per_page")
public Integer getPerPage() {
return perPage;
}
@JsonProperty("per_page")
public void setPerPage(Integer perPage) {
this.perPage = perPage;
}
@JsonProperty("to")
public Integer getTo() { return to; }
@JsonProperty("to")
public void setTo(Integer to) { this.to = to; }
@JsonProperty("total")
public Integer getTotal() {
return total;
}
@JsonProperty("total")
public void setTotal(Integer total) {
this.total = total;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Meta meta = (Meta) o;
return Objects.equals(currentPage, meta.currentPage) && Objects.equals(from, meta.from) && Objects.equals(lastPage, meta.lastPage) && Objects.equals(path, meta.path) && Objects.equals(perPage, meta.perPage) && Objects.equals(to, meta.to) && Objects.equals(total, meta.total);
}
@Override
public int hashCode() {
return Objects.hash(currentPage, from, lastPage, path, perPage, to, total);
}
@Override
public String toString() {
return "Meta{" +
"currentPage=" + currentPage +
", from=" + from +
", lastPage=" + lastPage +
", path=" + path +
", perPage=" + perPage +
", to=" + to +
", total=" + total +
'}';
}
}

@ -1,12 +1,9 @@
package me.goudham.domain.pagination; package me.goudham.domain.pagination;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.util.List; import java.util.List;
import java.util.Objects;
/** /**
* {@link PaginationData} * {@link PaginationData}
@ -26,81 +23,4 @@ import java.util.Objects;
"links", "links",
"meta" "meta"
}) })
public class PaginationData<T> { public record PaginationData<T>(List<T> data, Links links, Meta meta) { }
/**
* Data returned from the gallery
*
*/
@JsonProperty("data")
@JsonPropertyDescription("Data returned from the gallery")
private List<T> data;
/**
* {@link Links} to other data within the gallery
*
*/
@JsonProperty("links")
@JsonPropertyDescription("Links to other data within the gallery")
private Links links;
/**
* Extra pagination information
*
*/
@JsonProperty("meta")
@JsonPropertyDescription("Extra pagination information")
private Meta meta;
@JsonProperty("data")
public List<T> getData() {
return data;
}
@JsonProperty("data")
public void setData(List<T> data) {
this.data = data;
}
@JsonProperty("links")
public Links getLinks() {
return links;
}
@JsonProperty("links")
public void setLinks(Links links) {
this.links = links;
}
@JsonProperty("meta")
public Meta getMeta() {
return meta;
}
@JsonProperty("meta")
public void setMeta(Meta meta) {
this.meta = meta;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PaginationData<?> that = (PaginationData<?>) o;
return Objects.equals(data, that.data) && Objects.equals(links, that.links) && Objects.equals(meta, that.meta);
}
@Override
public int hashCode() {
return Objects.hash(data, links, meta);
}
@Override
public String toString() {
return "PaginationData{" +
"data=" + data +
", links=" + links +
", meta=" + meta +
'}';
}
}

@ -4,7 +4,7 @@ import me.goudham.APIWrapper;
import me.goudham.Response; import me.goudham.Response;
/** /**
* Thrown when {@link APIWrapper} fails to deserialize json into Java POJO's ({@link Response#getModel()}) * Thrown when {@link APIWrapper} fails to deserialize json into Java POJO's ({@link Response#model()})
* *
*/ */
public class APIMapperException extends Throwable { public class APIMapperException extends Throwable {

@ -46,8 +46,8 @@ class APIWrapperTest {
sut.setApiKey("InvalidAPIKey"); sut.setApiKey("InvalidAPIKey");
Result actualResult = sut.sendGetRequest("waifu/1"); Result actualResult = sut.sendGetRequest("waifu/1");
assertThat(actualResult.getStatusCode(), is(expectedStatusCode)); assertThat(actualResult.statusCode(), is(expectedStatusCode));
assertThat(actualResult.getBody(), is(expectedBody)); assertThat(actualResult.body(), is(expectedBody));
verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString()); verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
verifyNoMoreInteractions(httpClient); verifyNoMoreInteractions(httpClient);
} }
@ -63,8 +63,8 @@ class APIWrapperTest {
Result actualResult = sut.sendGetRequest("waifu/1"); Result actualResult = sut.sendGetRequest("waifu/1");
assertThat(actualResult.getStatusCode(), is(expectedStatusCode)); assertThat(actualResult.statusCode(), is(expectedStatusCode));
assertThat(actualResult.getBody(), is(expectedBody)); assertThat(actualResult.body(), is(expectedBody));
verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString()); verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
verifyNoMoreInteractions(httpClient); verifyNoMoreInteractions(httpClient);
} }

Loading…
Cancel
Save