Skip to content

Commit

Permalink
created PagingMethodDeserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
yvasyliev committed Feb 28, 2024
1 parent ef7c311 commit 6c1ab2a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 52 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<T extends Pageable> extends AbstractPagingMethodDeserializer<T, PagingMethod<T>, Page<T>> {
private final Map<Pattern, Function<Long, ? extends PagingMethod<? extends Pageable>>> pagingMethodFactories;

@SuppressWarnings("unchecked")
@Override
protected PagingMethod<T> createPagingMethod(String path, Map<String, String> queryParams) {
for (Map.Entry<Pattern, Function<Long, ? extends PagingMethod<? extends Pageable>>> 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<T>) pagingMethodFactory.getValue().apply(objectId);
}
}
throw new PagingMethodDeserializeException(path, queryParams);
}
}

0 comments on commit 6c1ab2a

Please sign in to comment.