Skip to content

Commit

Permalink
Code refinements (#11851)
Browse files Browse the repository at this point in the history
* Refine JavaDoc

* OpenRewrite: OptionalNotEmptyToIsPresent and OptionalNotPresentToIsEmpty
  • Loading branch information
koppor authored Sep 29, 2024
1 parent b44f30e commit c93a47c
Show file tree
Hide file tree
Showing 20 changed files with 29 additions and 35 deletions.
2 changes: 2 additions & 0 deletions rewrite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ recipeList:
- org.openrewrite.java.migrate.io.ReplaceFileInOrOutputStreamFinalizeWithClose
- org.openrewrite.java.migrate.net.JavaNetAPIs
- org.openrewrite.java.migrate.net.URLConstructorsToURIRecipes
- org.openrewrite.java.migrate.util.OptionalNotEmptyToIsPresent
- org.openrewrite.java.migrate.util.OptionalNotPresentToIsEmpty
- org.openrewrite.java.migrate.util.SequencedCollection
- org.openrewrite.java.migrate.lang.StringFormatted

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected SequencedSet<Field> determineFieldsToShow(BibEntry entry) {
BibDatabaseMode mode = databaseContext.getMode();
Optional<BibEntryType> entryType = entryTypesManager.enrich(entry.getType(), mode);
if (entryType.isPresent()) {
return entryType.get().getDeprecatedFields(mode).stream().filter(field -> !entry.getField(field).isEmpty()).collect(Collectors.toCollection(LinkedHashSet::new));
return entryType.get().getDeprecatedFields(mode).stream().filter(field -> entry.getField(field).isPresent()).collect(Collectors.toCollection(LinkedHashSet::new));
} else {
// Entry type unknown -> treat all fields as required (thus no optional fields)
return new LinkedHashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public String getAPIUrl(String entry_point, BibEntry entry) {

@Override
public List<BibEntry> searchCitedBy(BibEntry entry) throws FetcherException {
if (!entry.getDOI().isPresent()) {
if (entry.getDOI().isEmpty()) {
return List.of();
}

Expand All @@ -59,7 +59,7 @@ public List<BibEntry> searchCitedBy(BibEntry entry) throws FetcherException {

@Override
public List<BibEntry> searchCiting(BibEntry entry) throws FetcherException {
if (!entry.getDOI().isPresent()) {
if (entry.getDOI().isEmpty()) {
return List.of();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void resolveTags(AuxParserResult result) {
List<BibEntry> entriesToInsert = new ArrayList<>();

for (String key : result.getUniqueKeys()) {
if (!result.getGeneratedBibDatabase().getEntryByCitationKey(key).isPresent()) {
if (result.getGeneratedBibDatabase().getEntryByCitationKey(key).isEmpty()) {
Optional<BibEntry> entry = masterDatabase.getEntryByCitationKey(key);
if (entry.isPresent()) {
entriesToInsert.add(entry.get());
Expand Down Expand Up @@ -155,7 +155,7 @@ private void resolveCrossReferences(List<BibEntry> entries, AuxParserResult resu
List<BibEntry> entriesToInsert = new ArrayList<>();
for (BibEntry entry : entries) {
entry.getField(StandardField.CROSSREF).ifPresent(crossref -> {
if (!result.getGeneratedBibDatabase().getEntryByCitationKey(crossref).isPresent()) {
if (result.getGeneratedBibDatabase().getEntryByCitationKey(crossref).isEmpty()) {
Optional<BibEntry> refEntry = masterDatabase.getEntryByCitationKey(crossref);

if (refEntry.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private BibDatabaseDiff(BibDatabaseContext originalDatabase, BibDatabaseContext
}

private boolean isEmpty() {
return !metaDataDiff.isPresent() && !preambleDiff.isPresent() && bibStringDiffs.isEmpty() && entryDiffs.isEmpty();
return metaDataDiff.isEmpty() && preambleDiff.isEmpty() && bibStringDiffs.isEmpty() && entryDiffs.isEmpty();
}

private List<BibEntryDiff> getBibEntryDiffs(BibDatabaseContext originalDatabase, BibDatabaseContext newDatabase) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public List<FieldChange> cleanup(BibEntry entry) {
Field oldField = alias.getKey();
Field newField = alias.getValue();
entry.getField(oldField).ifPresent(oldValue -> {
if (!oldValue.isEmpty() && (!entry.getField(newField).isPresent())) {
if (!oldValue.isEmpty() && (entry.getField(newField).isEmpty())) {
// There is content in the old field and no value in the new, so just copy
entry.setField(newField, oldValue).ifPresent(changes::add);
entry.clearField(oldField).ifPresent(changes::add);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public List<FieldChange> cleanup(BibEntry entry) {
Field oldField = alias.getValue();
Field newField = alias.getKey();
entry.getField(oldField).ifPresent(oldValue -> {
if (!oldValue.isEmpty() && (!entry.getField(newField).isPresent())) {
if (!oldValue.isEmpty() && (entry.getField(newField).isEmpty())) {
// There is content in the old field and no value in the new, so just copy
entry.setField(newField, oldValue).ifPresent(changes::add);
entry.clearField(oldField).ifPresent(changes::add);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class FileLinksCleanup implements CleanupJob {
@Override
public List<FieldChange> cleanup(BibEntry entry) {
Optional<String> oldValue = entry.getField(StandardField.FILE);
if (!oldValue.isPresent()) {
if (oldValue.isEmpty()) {
return Collections.emptyList();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/logic/cleanup/ISSNCleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ISSNCleanup implements CleanupJob {
@Override
public List<FieldChange> cleanup(BibEntry entry) {
Optional<String> issnString = entry.getField(StandardField.ISSN);
if (!issnString.isPresent()) {
if (issnString.isEmpty()) {
return Collections.emptyList();
}

Expand Down
16 changes: 4 additions & 12 deletions src/main/java/org/jabref/logic/importer/fetcher/ACS.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,15 @@ public class ACS implements FulltextFetcher {
private static final String SOURCE = "https://pubs.acs.org/doi/abs/%s";

/**
* Tries to find a fulltext URL for a given BibTex entry.
* <p>
* Currently only uses the DOI if found.
*
* @param entry The Bibtex entry
* @return The fulltext PDF URL Optional, if found, or an empty Optional if not found.
* @throws NullPointerException if no BibTex entry is given
* @throws java.io.IOException
* Tries to find a fulltext URL for a given BibTeX entry.
* Requires the entry to have a DOI field.
* In case no DOI is present, an empty Optional is returned.
*/
@Override
public Optional<URL> findFullText(BibEntry entry) throws IOException {
Objects.requireNonNull(entry);

// DOI search
Optional<DOI> doi = entry.getField(StandardField.DOI).flatMap(DOI::parse);

if (!doi.isPresent()) {
if (doi.isEmpty()) {
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException {

Optional<DOI> doi = entry.getField(StandardField.DOI).flatMap(DOI::parse);

if (!doi.isPresent()) {
if (doi.isEmpty()) {
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public String getName() {
@Override
public List<BibEntry> performSearch(BibEntry entry) throws FetcherException {
Optional<String> title = entry.getFieldLatexFree(StandardField.TITLE);
if (!title.isPresent()) {
if (title.isEmpty()) {
// without a title there is no reason to ask MrDLib
return List.of();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Optional<URL> findFullText(BibEntry entry) throws IOException {
Optional<DOI> doi = entry.getField(StandardField.DOI)
.flatMap(DOI::parse);

if (!doi.isPresent()) {
if (doi.isEmpty()) {
return Optional.empty();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/logic/integrity/DateChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public Optional<String> checkValue(String value) {
}

Optional<Date> parsedDate = Date.parse(value);
if (!parsedDate.isPresent()) {
if (parsedDate.isEmpty()) {
return Optional.of(Localization.lang("incorrect format"));
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/logic/integrity/FileChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Optional<String> checkValue(String value) {

for (LinkedFile file : linkedFiles) {
Optional<Path> linkedFile = file.findIn(context, filePreferences);
if ((!linkedFile.isPresent()) || !Files.exists(linkedFile.get())) {
if ((linkedFile.isEmpty()) || !Files.exists(linkedFile.get())) {
return Optional.of(Localization.lang("link should refer to a correct file path"));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/logic/msbib/MSBibConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static MSBibEntry convert(BibEntry entry) {
result.day = entry.getFieldOrAliasLatexFree(StandardField.DAY).orElse(null);
result.month = entry.getMonth().map(Month::getFullName).orElse(null);

if (!entry.getFieldLatexFree(StandardField.YEAR).isPresent()) {
if (entry.getFieldLatexFree(StandardField.YEAR).isEmpty()) {
result.year = entry.getFieldOrAliasLatexFree(StandardField.YEAR).orElse(null);
}
result.journalName = entry.getFieldOrAliasLatexFree(StandardField.JOURNAL).orElse(null);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/logic/shared/DBMSSynchronizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public void pullChanges() {
* Synchronizes local BibEntries only if last entry changes still remain
*/
public void pullLastEntryChanges() {
if (!lastEntryChanged.isEmpty()) {
if (lastEntryChanged.isPresent()) {
if (!checkCurrentConnection()) {
return;
}
Expand All @@ -342,7 +342,7 @@ public void pullLastEntryChanges() {
* Synchronizes local BibEntries and pulls remaining last entry changes
*/
private void pullWithLastEntry() {
if (!lastEntryChanged.isEmpty() && isPresentLocalBibEntry(lastEntryChanged.get())) {
if (lastEntryChanged.isPresent() && isPresentLocalBibEntry(lastEntryChanged.get())) {
synchronizeSharedEntry(lastEntryChanged.get());
}
lastEntryChanged = Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ConvertLegacyExplicitGroups implements PostOpenMigration {
@Override
public void performMigration(ParserResult parserResult) {
Objects.requireNonNull(parserResult);
if (!parserResult.getMetaData().getGroups().isPresent()) {
if (parserResult.getMetaData().getGroups().isEmpty()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void performMigration(ParserResult parserResult) {
markingGroup.addEntriesToGroup(markingMatchedEntries);
}

if (!parserResult.getMetaData().getGroups().isPresent()) {
if (parserResult.getMetaData().getGroups().isEmpty()) {
parserResult.getMetaData().setGroups(GroupTreeNode.fromGroup(DefaultGroupsFactory.getAllEntriesGroup()));
}
GroupTreeNode root = parserResult.getMetaData().getGroups().get();
Expand All @@ -68,7 +68,7 @@ private Multimap<String, BibEntry> getMarkingWithEntries(List<BibEntry> entries)

for (BibEntry entry : entries) {
Optional<String> marking = entry.getField(InternalField.MARKED_INTERNAL);
if (!marking.isPresent()) {
if (marking.isEmpty()) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/model/util/OptionalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
public class OptionalUtil {

public static <T, U> boolean equals(Optional<T> left, Optional<U> right, BiPredicate<T, U> equality) {
if (!left.isPresent()) {
return !right.isPresent();
if (left.isEmpty()) {
return right.isEmpty();
} else {
if (right.isPresent()) {
return equality.test(left.get(), right.get());
Expand Down

0 comments on commit c93a47c

Please sign in to comment.