Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to sorted set #5477

Merged
merged 3 commits into from
Oct 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.prefs.BackingStoreException;

import org.jabref.Globals;
Expand Down Expand Up @@ -533,7 +534,7 @@ private Optional<ParserResult> fetch(String fetchCommand) {
String engine = split[0];
String query = split[1];

List<SearchBasedFetcher> fetchers = WebFetchers.getSearchBasedFetchers(Globals.prefs.getImportFormatPreferences());
Set<SearchBasedFetcher> fetchers = WebFetchers.getSearchBasedFetchers(Globals.prefs.getImportFormatPreferences());
Optional<SearchBasedFetcher> selectedFetcher = fetchers.stream()
.filter(fetcher -> fetcher.getName().equalsIgnoreCase(engine))
.findFirst();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.importer.fetcher;

import java.util.List;
import java.util.SortedSet;

import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
Expand Down Expand Up @@ -38,7 +39,7 @@ public WebSearchPaneViewModel(ImportFormatPreferences importPreferences, JabRefF
this.frame = frame;
this.dialogService = dialogService;

List<SearchBasedFetcher> allFetchers = WebFetchers.getSearchBasedFetchers(importPreferences);
SortedSet<SearchBasedFetcher> allFetchers = WebFetchers.getSearchBasedFetchers(importPreferences);
fetchers.setAll(allFetchers);

// Choose last-selected fetcher as default
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/jabref/logic/importer/FulltextFetchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
Expand All @@ -34,7 +35,7 @@ public class FulltextFetchers {
// Timeout in seconds
private static final int FETCHER_TIMEOUT = 10;

private final List<FulltextFetcher> finders = new ArrayList<>();
private final Set<FulltextFetcher> finders = new HashSet<>();

private final Predicate<String> isPDF = url -> {
try {
Expand All @@ -49,7 +50,7 @@ public FulltextFetchers(ImportFormatPreferences importFormatPreferences) {
this(WebFetchers.getFullTextFetchers(importFormatPreferences));
}

FulltextFetchers(List<FulltextFetcher> fetcher) {
FulltextFetchers(Set<FulltextFetcher> fetcher) {
finders.addAll(fetcher);
}

Expand Down Expand Up @@ -108,7 +109,7 @@ private Callable<Optional<FetcherResult>> getCallable(BibEntry entry, FulltextFe
};
}

private List<Callable<Optional<FetcherResult>>> getCallables(BibEntry entry, List<FulltextFetcher> fetchers) {
private List<Callable<Optional<FetcherResult>>> getCallables(BibEntry entry, Set<FulltextFetcher> fetchers) {
return fetchers.stream()
.map(f -> getCallable(entry, f))
.collect(Collectors.toList());
Expand Down
124 changes: 57 additions & 67 deletions src/main/java/org/jabref/logic/importer/WebFetchers.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.jabref.logic.importer;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.HashSet;
import java.util.Optional;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.jabref.logic.importer.fetcher.ACMPortalFetcher;
import org.jabref.logic.importer.fetcher.ACS;
Expand Down Expand Up @@ -81,84 +79,76 @@ public static Optional<IdFetcher<? extends Identifier>> getIdFetcherForField(Fie
}

/**
* @return sorted list containing search based fetchers
* @return sorted set containing search based fetchers
*/
public static SortedList<SearchBasedFetcher> getSearchBasedFetchers(ImportFormatPreferences importFormatPreferences) {
ArrayList<SearchBasedFetcher> list = new ArrayList<>();
list.add(new ArXiv(importFormatPreferences));
list.add(new INSPIREFetcher(importFormatPreferences));
list.add(new GvkFetcher());
list.add(new MedlineFetcher());
list.add(new AstrophysicsDataSystem(importFormatPreferences));
list.add(new MathSciNet(importFormatPreferences));
list.add(new ZbMATH(importFormatPreferences));
list.add(new ACMPortalFetcher(importFormatPreferences));
list.add(new GoogleScholar(importFormatPreferences));
list.add(new DBLPFetcher(importFormatPreferences));
list.add(new SpringerFetcher());
list.add(new CrossRef());
list.add(new CiteSeer());
list.add(new DOAJFetcher(importFormatPreferences));
list.add(new IEEE(importFormatPreferences));

ObservableList<SearchBasedFetcher> observableList = FXCollections.observableList(list);
return new SortedList<>(observableList, Comparator.comparing(WebFetcher::getName));
public static SortedSet<SearchBasedFetcher> getSearchBasedFetchers(ImportFormatPreferences importFormatPreferences) {
SortedSet<SearchBasedFetcher> set = new TreeSet<>(Comparator.comparing(WebFetcher::getName));
set.add(new ArXiv(importFormatPreferences));
set.add(new INSPIREFetcher(importFormatPreferences));
set.add(new GvkFetcher());
set.add(new MedlineFetcher());
set.add(new AstrophysicsDataSystem(importFormatPreferences));
set.add(new MathSciNet(importFormatPreferences));
set.add(new ZbMATH(importFormatPreferences));
set.add(new ACMPortalFetcher(importFormatPreferences));
set.add(new GoogleScholar(importFormatPreferences));
set.add(new DBLPFetcher(importFormatPreferences));
set.add(new SpringerFetcher());
set.add(new CrossRef());
set.add(new CiteSeer());
set.add(new DOAJFetcher(importFormatPreferences));
set.add(new IEEE(importFormatPreferences));
return set;
}

/**
* @return sorted list containing id based fetchers
* @return sorted set containing id based fetchers
*/
public static SortedList<IdBasedFetcher> getIdBasedFetchers(ImportFormatPreferences importFormatPreferences) {
ArrayList<IdBasedFetcher> list = new ArrayList<>();
list.add(new ArXiv(importFormatPreferences));
list.add(new AstrophysicsDataSystem(importFormatPreferences));
list.add(new IsbnFetcher(importFormatPreferences));
list.add(new DiVA(importFormatPreferences));
list.add(new DoiFetcher(importFormatPreferences));
list.add(new MedlineFetcher());
list.add(new TitleFetcher(importFormatPreferences));
list.add(new MathSciNet(importFormatPreferences));
list.add(new CrossRef());
list.add(new LibraryOfCongress(importFormatPreferences));
list.add(new IacrEprintFetcher(importFormatPreferences));
list.add(new RfcFetcher(importFormatPreferences));

ObservableList<IdBasedFetcher> observableList = FXCollections.observableList(list);
return new SortedList<>(observableList, Comparator.comparing(WebFetcher::getName));
public static SortedSet<IdBasedFetcher> getIdBasedFetchers(ImportFormatPreferences importFormatPreferences) {
SortedSet<IdBasedFetcher> set = new TreeSet<>(Comparator.comparing(WebFetcher::getName));
set.add(new ArXiv(importFormatPreferences));
set.add(new AstrophysicsDataSystem(importFormatPreferences));
set.add(new IsbnFetcher(importFormatPreferences));
set.add(new DiVA(importFormatPreferences));
set.add(new DoiFetcher(importFormatPreferences));
set.add(new MedlineFetcher());
set.add(new TitleFetcher(importFormatPreferences));
set.add(new MathSciNet(importFormatPreferences));
set.add(new CrossRef());
set.add(new LibraryOfCongress(importFormatPreferences));
set.add(new IacrEprintFetcher(importFormatPreferences));
set.add(new RfcFetcher(importFormatPreferences));
return set;
}

/**
* @return sorted list containing entry based fetchers
* @return sorted set containing entry based fetchers
*/
public static SortedList<EntryBasedFetcher> getEntryBasedFetchers(ImportFormatPreferences importFormatPreferences) {
ArrayList<EntryBasedFetcher> list = new ArrayList<>();
list.add(new AstrophysicsDataSystem(importFormatPreferences));
list.add(new DoiFetcher(importFormatPreferences));
list.add(new IsbnFetcher(importFormatPreferences));
list.add(new MathSciNet(importFormatPreferences));
list.add(new CrossRef());

ObservableList<EntryBasedFetcher> observableList = FXCollections.observableList(list);
return new SortedList<>(observableList, Comparator.comparing(WebFetcher::getName));
public static SortedSet<EntryBasedFetcher> getEntryBasedFetchers(ImportFormatPreferences importFormatPreferences) {
SortedSet<EntryBasedFetcher> set = new TreeSet<>(Comparator.comparing(WebFetcher::getName));
set.add(new AstrophysicsDataSystem(importFormatPreferences));
set.add(new DoiFetcher(importFormatPreferences));
set.add(new IsbnFetcher(importFormatPreferences));
set.add(new MathSciNet(importFormatPreferences));
set.add(new CrossRef());
return set;
}

/**
* @return sorted list containing id fetchers
* @return sorted set containing id fetchers
*/
public static SortedList<IdFetcher> getIdFetchers(ImportFormatPreferences importFormatPreferences) {
ArrayList<IdFetcher> list = new ArrayList<>();
list.add(new CrossRef());
list.add(new ArXiv(importFormatPreferences));

ObservableList<IdFetcher> observableList = FXCollections.observableList(list);
return new SortedList<>(observableList, Comparator.comparing(WebFetcher::getName));
public static SortedSet<IdFetcher> getIdFetchers(ImportFormatPreferences importFormatPreferences) {
SortedSet<IdFetcher> set = new TreeSet<>(Comparator.comparing(WebFetcher::getName));
set.add(new CrossRef());
set.add(new ArXiv(importFormatPreferences));
return set;
}

/**
* @return unsorted list containing fulltext fetchers
* @return set containing fulltext fetchers
*/
public static List<FulltextFetcher> getFullTextFetchers(ImportFormatPreferences importFormatPreferences) {
List<FulltextFetcher> fetchers = new ArrayList<>();
public static Set<FulltextFetcher> getFullTextFetchers(ImportFormatPreferences importFormatPreferences) {
Set<FulltextFetcher> fetchers = new HashSet<>();
// Original
fetchers.add(new DoiResolution());
// Publishers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;

import org.jabref.logic.importer.fetcher.TrustLevel;
Expand Down Expand Up @@ -34,7 +35,7 @@ public void tearDown() {
public void acceptPdfUrls() throws MalformedURLException {
URL pdfUrl = new URL("http://docs.oasis-open.org/wsbpel/2.0/OS/wsbpel-v2.0-OS.pdf");
FulltextFetcher finder = (e) -> Optional.of(pdfUrl);
FulltextFetchers fetcher = new FulltextFetchers(Arrays.asList(finder));
FulltextFetchers fetcher = new FulltextFetchers(new HashSet<>(Arrays.asList(finder)));

assertEquals(Optional.of(pdfUrl), fetcher.findFullTextPDF(entry));
}
Expand All @@ -43,7 +44,7 @@ public void acceptPdfUrls() throws MalformedURLException {
public void rejectNonPdfUrls() throws MalformedURLException {
URL pdfUrl = new URL("https://github.com/JabRef/jabref/blob/master/README.md");
FulltextFetcher finder = (e) -> Optional.of(pdfUrl);
FulltextFetchers fetcher = new FulltextFetchers(Arrays.asList(finder));
FulltextFetchers fetcher = new FulltextFetchers(new HashSet<>(Arrays.asList(finder)));

assertEquals(Optional.empty(), fetcher.findFullTextPDF(entry));
}
Expand All @@ -52,7 +53,7 @@ public void rejectNonPdfUrls() throws MalformedURLException {
public void noTrustLevel() throws MalformedURLException {
URL pdfUrl = new URL("http://docs.oasis-open.org/wsbpel/2.0/OS/wsbpel-v2.0-OS.pdf");
FulltextFetcher finder = (e) -> Optional.of(pdfUrl);
FulltextFetchers fetcher = new FulltextFetchers(Arrays.asList(finder));
FulltextFetchers fetcher = new FulltextFetchers(new HashSet<>(Arrays.asList(finder)));

assertEquals(Optional.of(pdfUrl), fetcher.findFullTextPDF(entry));
}
Expand All @@ -69,7 +70,7 @@ public void higherTrustLevelWins() throws MalformedURLException, IOException, Fe
when(finderHigh.findFullText(entry)).thenReturn(Optional.of(highUrl));
when(finderLow.findFullText(entry)).thenReturn(Optional.of(lowUrl));

FulltextFetchers fetcher = new FulltextFetchers(Arrays.asList(finderLow, finderHigh));
FulltextFetchers fetcher = new FulltextFetchers(new HashSet<>(Arrays.asList(finderLow, finderHigh)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels ugly. Can't another constructor be added to FullTextFetchers so that the caller doesn't need to pass a sortedset, but something more easy? Maybe, even ... could be used so that fetcherLow and fetcherHigh can directly be passed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a new constructor is unnecessary. Just remembered that there is a Set.of method..


assertEquals(Optional.of(highUrl), fetcher.findFullTextPDF(entry));
}
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/org/jabref/logic/importer/WebFetchersTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.jabref.logic.importer;

import java.util.List;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -31,7 +31,7 @@ void setUp() throws Exception {

@Test
void getIdBasedFetchersReturnsAllFetcherDerivingFromIdBasedFetcher() throws Exception {
List<IdBasedFetcher> idFetchers = WebFetchers.getIdBasedFetchers(importFormatPreferences);
Set<IdBasedFetcher> idFetchers = WebFetchers.getIdBasedFetchers(importFormatPreferences);

try (ScanResult scanResult = classGraph.scan()) {

Expand All @@ -50,7 +50,7 @@ void getIdBasedFetchersReturnsAllFetcherDerivingFromIdBasedFetcher() throws Exce

@Test
void getEntryBasedFetchersReturnsAllFetcherDerivingFromEntryBasedFetcher() throws Exception {
List<EntryBasedFetcher> idFetchers = WebFetchers.getEntryBasedFetchers(importFormatPreferences);
Set<EntryBasedFetcher> idFetchers = WebFetchers.getEntryBasedFetchers(importFormatPreferences);

try (ScanResult scanResult = classGraph.scan()) {
ClassInfoList controlClasses = scanResult.getClassesImplementing(EntryBasedFetcher.class.getCanonicalName());
Expand All @@ -64,7 +64,7 @@ void getEntryBasedFetchersReturnsAllFetcherDerivingFromEntryBasedFetcher() throw

@Test
void getSearchBasedFetchersReturnsAllFetcherDerivingFromSearchBasedFetcher() throws Exception {
List<SearchBasedFetcher> searchBasedFetchers = WebFetchers.getSearchBasedFetchers(importFormatPreferences);
Set<SearchBasedFetcher> searchBasedFetchers = WebFetchers.getSearchBasedFetchers(importFormatPreferences);
try (ScanResult scanResult = classGraph.scan()) {
ClassInfoList controlClasses = scanResult.getClassesImplementing(SearchBasedFetcher.class.getCanonicalName());
Set<Class<?>> expected = controlClasses.loadClasses().stream().collect(Collectors.toSet());
Expand All @@ -76,7 +76,7 @@ void getSearchBasedFetchersReturnsAllFetcherDerivingFromSearchBasedFetcher() thr

@Test
void getFullTextFetchersReturnsAllFetcherDerivingFromFullTextFetcher() throws Exception {
List<FulltextFetcher> fullTextFetchers = WebFetchers.getFullTextFetchers(importFormatPreferences);
Set<FulltextFetcher> fullTextFetchers = WebFetchers.getFullTextFetchers(importFormatPreferences);

try (ScanResult scanResult = classGraph.scan()) {
ClassInfoList controlClasses = scanResult.getClassesImplementing(FulltextFetcher.class.getCanonicalName());
Expand All @@ -87,7 +87,7 @@ void getFullTextFetchersReturnsAllFetcherDerivingFromFullTextFetcher() throws Ex

@Test
void getIdFetchersReturnsAllFetcherDerivingFromIdFetcher() throws Exception {
List<IdFetcher> idFetchers = WebFetchers.getIdFetchers(importFormatPreferences);
Set<IdFetcher> idFetchers = WebFetchers.getIdFetchers(importFormatPreferences);

try (ScanResult scanResult = classGraph.scan()) {
ClassInfoList controlClasses = scanResult.getClassesImplementing(IdFetcher.class.getCanonicalName());
Expand All @@ -98,7 +98,7 @@ void getIdFetchersReturnsAllFetcherDerivingFromIdFetcher() throws Exception {
}
}

private Set<? extends Class<?>> getClasses(List<?> objects) {
private Set<? extends Class<?>> getClasses(Collection<?> objects) {
return objects.stream().map(Object::getClass).collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jabref.logic.importer.fetcher;

import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.jabref.logic.importer.FulltextFetcher;
import org.jabref.logic.importer.ImportFormatPreferences;
Expand All @@ -18,7 +18,7 @@
class FulltextFetcherTest {

@SuppressWarnings("unused")
private static List<FulltextFetcher> fetcherProvider() {
private static Set<FulltextFetcher> fetcherProvider() {
return WebFetchers.getFullTextFetchers(mock(ImportFormatPreferences.class));
}

Expand Down