diff --git a/src/main/java/io/github/yvasyliev/deezer/json/PagingMethodDeserializer.java b/src/main/java/io/github/yvasyliev/deezer/json/PagingMethodDeserializer.java deleted file mode 100644 index 57e9780..0000000 --- a/src/main/java/io/github/yvasyliev/deezer/json/PagingMethodDeserializer.java +++ /dev/null @@ -1,52 +0,0 @@ -package io.github.yvasyliev.deezer.json; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; -import io.github.yvasyliev.deezer.objects.Pageable; -import io.github.yvasyliev.deezer.util.UrlUtil; -import io.github.yvasyliev.deezer.methods.PagingMethod; -import lombok.AllArgsConstructor; - -import java.lang.reflect.Type; -import java.net.URL; -import java.util.Map; -import java.util.function.Function; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -@AllArgsConstructor -public class PagingMethodDeserializer implements JsonDeserializer> { - private final Map>> pagingMethodFactories; - - @Override - public PagingMethod deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - URL url = context.deserialize(json, URL.class); - return setMethodParams( - createPagingMethod(url.getPath()), - UrlUtil.getQueryParams(url) - ); - } - - protected PagingMethod createPagingMethod(String path) { - for (Map.Entry>> pagingMethodFactory : pagingMethodFactories.entrySet()) { - Matcher matcher = pagingMethodFactory.getKey().matcher(path); - if (matcher.matches()) { - Long objectId = matcher.groupCount() > 0 ? Long.parseLong(matcher.group(1)) : null; - return pagingMethodFactory.getValue().apply(objectId); - } - } - throw new JsonParseException("Unhandled paging method: " + path); - } - - protected Integer integer(String value) { - return value != null ? Integer.parseInt(value) : null; - } - - protected PagingMethod setMethodParams(PagingMethod pagingMethod, Map queryParams) { - return pagingMethod - .index(integer(queryParams.get(PagingMethod.INDEX))) - .limit(integer(queryParams.get(PagingMethod.LIMIT))); - } -} diff --git a/src/main/java/io/github/yvasyliev/deezer/json/deserializers/PagingMethodDeserializer.java b/src/main/java/io/github/yvasyliev/deezer/json/deserializers/PagingMethodDeserializer.java new file mode 100644 index 0000000..2cf38bd --- /dev/null +++ b/src/main/java/io/github/yvasyliev/deezer/json/deserializers/PagingMethodDeserializer.java @@ -0,0 +1,30 @@ +package io.github.yvasyliev.deezer.json.deserializers; + +import io.github.yvasyliev.deezer.json.exceptions.PagingMethodDeserializeException; +import io.github.yvasyliev.deezer.methods.PagingMethod; +import io.github.yvasyliev.deezer.objects.Page; +import io.github.yvasyliev.deezer.objects.Pageable; +import lombok.AllArgsConstructor; + +import java.util.Map; +import java.util.function.Function; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +@AllArgsConstructor +public class PagingMethodDeserializer extends AbstractPagingMethodDeserializer, Page> { + private final Map>> pagingMethodFactories; + + @SuppressWarnings("unchecked") + @Override + protected PagingMethod createPagingMethod(String path, Map queryParams) { + for (Map.Entry>> pagingMethodFactory : pagingMethodFactories.entrySet()) { + Matcher matcher = pagingMethodFactory.getKey().matcher(path); + if (matcher.matches()) { + Long objectId = matcher.groupCount() > 0 ? Long.parseLong(matcher.group(1)) : null; + return (PagingMethod) pagingMethodFactory.getValue().apply(objectId); + } + } + throw new PagingMethodDeserializeException(path, queryParams); + } +}