Refactors ListHelper's method not to be static.

This commit is contained in:
gkolokythas 2019-10-25 13:57:51 +03:00
parent 1438f6bdca
commit 2d35a3d665
4 changed files with 16 additions and 13 deletions

View File

@ -25,10 +25,12 @@ public class FunderManager {
private ApiContext apiContext; private ApiContext apiContext;
private RemoteFetcher remoteFetcher; private RemoteFetcher remoteFetcher;
private ListHelper listHelper;
public FunderManager(ApiContext apiContext, RemoteFetcher remoteFetcher) { public FunderManager(ApiContext apiContext, RemoteFetcher remoteFetcher, ListHelper listHelper) {
this.apiContext = apiContext; this.apiContext = apiContext;
this.remoteFetcher = remoteFetcher; this.remoteFetcher = remoteFetcher;
this.listHelper = listHelper;
} }
public List<Funder> getCriteriaWithExternal(FunderCriteriaRequest funderCriteria, Principal principal) throws HugeResultSet, NoURLFound { public List<Funder> getCriteriaWithExternal(FunderCriteriaRequest funderCriteria, Principal principal) throws HugeResultSet, NoURLFound {
@ -55,7 +57,7 @@ public class FunderManager {
funders.add(funder); funders.add(funder);
} }
funders.sort(Comparator.comparing(Funder::getLabel)); funders.sort(Comparator.comparing(Funder::getLabel));
funders = funders.stream().filter(ListHelper.distinctByKey(Funder::getLabel)).collect(Collectors.toList()); funders = funders.stream().filter(listHelper.distinctByKey(Funder::getLabel)).collect(Collectors.toList());
return funders; return funders;
} }
} }

View File

@ -4,18 +4,14 @@ import eu.eudat.data.dao.criteria.FunderCriteria;
import eu.eudat.data.entities.Funder; import eu.eudat.data.entities.Funder;
import eu.eudat.data.query.items.table.grant.GrantTableRequest; import eu.eudat.data.query.items.table.grant.GrantTableRequest;
import eu.eudat.exceptions.grant.GrantWithDMPsDeleteException; import eu.eudat.exceptions.grant.GrantWithDMPsDeleteException;
import eu.eudat.logic.builders.entity.ContentBuilder;
import eu.eudat.logic.builders.model.models.GrantBuilder; import eu.eudat.logic.builders.model.models.GrantBuilder;
import eu.eudat.data.dao.entities.GrantDao; import eu.eudat.data.dao.entities.GrantDao;
import eu.eudat.data.entities.Content;
import eu.eudat.data.entities.DMP; import eu.eudat.data.entities.DMP;
import eu.eudat.exceptions.files.TempFileNotFoundException;
import eu.eudat.logic.proxy.config.ExternalUrlCriteria; import eu.eudat.logic.proxy.config.ExternalUrlCriteria;
import eu.eudat.logic.services.operations.DatabaseRepository; import eu.eudat.logic.services.operations.DatabaseRepository;
import eu.eudat.logic.utilities.helpers.ListHelper; import eu.eudat.logic.utilities.helpers.ListHelper;
import eu.eudat.models.data.external.ExternalSourcesItemModel; import eu.eudat.models.data.external.ExternalSourcesItemModel;
import eu.eudat.models.data.external.GrantsExternalSourcesModel; import eu.eudat.models.data.external.GrantsExternalSourcesModel;
import eu.eudat.models.data.files.ContentFile;
import eu.eudat.models.data.grant.Grant; import eu.eudat.models.data.grant.Grant;
import eu.eudat.models.data.helpers.common.DataTableData; import eu.eudat.models.data.helpers.common.DataTableData;
import eu.eudat.data.query.items.item.grant.GrantCriteriaRequest; import eu.eudat.data.query.items.item.grant.GrantCriteriaRequest;
@ -29,8 +25,6 @@ import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.services.helpers.FileStorageService; import eu.eudat.logic.services.helpers.FileStorageService;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException;
import java.text.ParseException;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -45,12 +39,14 @@ public class GrantManager {
private DatabaseRepository databaseRepository; private DatabaseRepository databaseRepository;
private FileStorageService fileStorageService; private FileStorageService fileStorageService;
private RemoteFetcher remoteFetcher; private RemoteFetcher remoteFetcher;
private ListHelper listHelper;
public GrantManager(ApiContext apiContext) { public GrantManager(ApiContext apiContext, ListHelper listHelper) {
this.apiContext = apiContext; this.apiContext = apiContext;
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository(); this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
this.fileStorageService = apiContext.getOperationsContext().getFileStorageService(); this.fileStorageService = apiContext.getOperationsContext().getFileStorageService();
this.remoteFetcher = apiContext.getOperationsContext().getRemoteFetcher(); this.remoteFetcher = apiContext.getOperationsContext().getRemoteFetcher();
this.listHelper = listHelper;
} }
public DataTableData<eu.eudat.models.data.grant.GrantListingModel> getPaged(GrantTableRequest grantTableRequest, Principal principal, String fieldsGroup) throws Exception { public DataTableData<eu.eudat.models.data.grant.GrantListingModel> getPaged(GrantTableRequest grantTableRequest, Principal principal, String fieldsGroup) throws Exception {
@ -142,7 +138,7 @@ public class GrantManager {
grants.add(grant); grants.add(grant);
} }
grants.sort(Comparator.comparing(Grant::getLabel)); grants.sort(Comparator.comparing(Grant::getLabel));
grants = grants.stream().filter(ListHelper.distinctByKey(Grant::getLabel)).collect(Collectors.toList()); grants = grants.stream().filter(listHelper.distinctByKey(Grant::getLabel)).collect(Collectors.toList());
return grants; return grants;
} }

View File

@ -25,10 +25,12 @@ public class ProjectManager {
private ApiContext apiContext; private ApiContext apiContext;
private RemoteFetcher remoteFetcher; private RemoteFetcher remoteFetcher;
private ListHelper listHelper;
public ProjectManager(ApiContext apiContext) { public ProjectManager(ApiContext apiContext, ListHelper listHelper) {
this.apiContext = apiContext; this.apiContext = apiContext;
this.remoteFetcher = apiContext.getOperationsContext().getRemoteFetcher(); this.remoteFetcher = apiContext.getOperationsContext().getRemoteFetcher();
this.listHelper = listHelper;
} }
public List<Project> getCriteriaWithExternal(ProjectCriteriaRequest projectCriteria, Principal principal) throws HugeResultSet, NoURLFound { public List<Project> getCriteriaWithExternal(ProjectCriteriaRequest projectCriteria, Principal principal) throws HugeResultSet, NoURLFound {
@ -52,7 +54,7 @@ public class ProjectManager {
projects.add(project); projects.add(project);
} }
projects.sort(Comparator.comparing(Project::getLabel)); projects.sort(Comparator.comparing(Project::getLabel));
projects = projects.stream().filter(ListHelper.distinctByKey(Project::getLabel)).collect(Collectors.toList()); projects = projects.stream().filter(listHelper.distinctByKey(Project::getLabel)).collect(Collectors.toList());
return projects; return projects;
} }
} }

View File

@ -1,13 +1,16 @@
package eu.eudat.logic.utilities.helpers; package eu.eudat.logic.utilities.helpers;
import org.springframework.stereotype.Component;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
@Component
public class ListHelper { public class ListHelper {
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) { public <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>(); Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null; return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
} }