Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@

import org.jabref.gui.mergeentries.threewaymerge.fieldsmerger.FieldMerger;
import org.jabref.gui.mergeentries.threewaymerge.fieldsmerger.FieldMergerFactory;
import org.jabref.logic.bibtex.comparator.ComparisonResult;
import org.jabref.logic.bibtex.comparator.YearFieldValuePlausibilityComparator;
import org.jabref.logic.bibtex.comparator.plausibility.PlausibilityComparatorFactory;
import org.jabref.logic.util.strings.StringUtil;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldTextMapper;
import org.jabref.model.entry.field.InternalField;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.EntryTypeFactory;
import org.jabref.model.entry.types.StandardEntryType;

import com.tobiasdiez.easybind.EasyBind;
import org.slf4j.Logger;
Expand Down Expand Up @@ -131,19 +128,18 @@ public void autoSelectBetterValue() {
String leftValue = getLeftFieldValue();
String rightValue = getRightFieldValue();

if (StandardField.YEAR == field) {
YearFieldValuePlausibilityComparator comparator = new YearFieldValuePlausibilityComparator();
ComparisonResult comparison = comparator.compare(leftValue, rightValue);
if (ComparisonResult.RIGHT_BETTER == comparison) {
selectRightValue();
} else if (ComparisonResult.LEFT_BETTER == comparison) {
selectLeftValue();
}
} else if (InternalField.TYPE_HEADER == field) {
if (leftValue.equalsIgnoreCase(StandardEntryType.Misc.getName())) {
selectRightValue();
}
}
PlausibilityComparatorFactory.INSTANCE
.getPlausibilityComparator(field)
.map(comparator -> comparator.compare(leftValue, rightValue))
.ifPresent(result -> {
switch (result) {
case RIGHT_BETTER ->
selectRightValue();
case LEFT_BETTER ->
selectLeftValue();
default -> { /* nothing */ }
}
});
}

public void selectNonEmptyValue() {
Expand Down
1 change: 1 addition & 0 deletions jablib/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
exports org.jabref.logic.git.merge.planning;
exports org.jabref.logic.git.merge.execution;
exports org.jabref.model.sciteTallies;
exports org.jabref.logic.bibtex.comparator.plausibility;

requires java.base;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jabref.logic.bibtex.comparator.plausibility;

import org.jabref.logic.bibtex.comparator.ComparisonResult;
import org.jabref.model.entry.types.StandardEntryType;

/// If the left entry type is misc then prefer the the right value
public class EntryTypePlausibilityComparator implements FieldValuePlausibilityComparator {

// Only the factory may instantiate this
EntryTypePlausibilityComparator() {
}

@Override
public ComparisonResult compare(String leftValue, String rightValue) {
if (leftValue.equalsIgnoreCase(StandardEntryType.Misc.getName())) {
return ComparisonResult.RIGHT_BETTER;
}
return ComparisonResult.UNDETERMINED;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.jabref.logic.bibtex.comparator;
package org.jabref.logic.bibtex.comparator.plausibility;

public abstract class FieldValuePlausibilityComparator {
import org.jabref.logic.bibtex.comparator.ComparisonResult;

public interface FieldValuePlausibilityComparator {
/**
* Compares the plausibility of two field values.
*
* @param leftValue value from the library (or candidate)
* @param rightValue value from the fetcher (or existing record)
* @return ComparisonResult indicating which field is more plausible: RIGHT_BETTER, LEFT_BETTER, or UNDETERMINED
*/
public abstract ComparisonResult compare(String leftValue, String rightValue);
ComparisonResult compare(String leftValue, String rightValue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jabref.logic.bibtex.comparator.plausibility;

import java.util.Optional;

import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldProperty;
import org.jabref.model.entry.field.InternalField;

public enum PlausibilityComparatorFactory {

// Single instance ensured by Java compiler
INSTANCE;

public Optional<FieldValuePlausibilityComparator> getPlausibilityComparator(Field field) {
// Similar code as [org.jabref.gui.fieldeditors.FieldEditors.getForField]
if (field.getProperties().contains(FieldProperty.YEAR)) {
return Optional.of(new YearFieldValuePlausibilityComparator());
}
if (InternalField.TYPE_HEADER == field) {
return Optional.of(new EntryTypePlausibilityComparator());
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package org.jabref.logic.bibtex.comparator;
package org.jabref.logic.bibtex.comparator.plausibility;

import java.time.Year;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jabref.logic.bibtex.comparator.ComparisonResult;
import org.jabref.logic.integrity.YearChecker;
import org.jabref.logic.util.strings.StringUtil;

public class YearFieldValuePlausibilityComparator extends FieldValuePlausibilityComparator {
public class YearFieldValuePlausibilityComparator implements FieldValuePlausibilityComparator {

private static final Pattern YEAR_PATTERN = Pattern.compile("(\\d{4})");

// Only the factor may instantiate this
YearFieldValuePlausibilityComparator() {
}

/**
* Compares the plausibility of two field values.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public enum FieldProperty {

MONTH,

YEARDIVISION,

MULTILINE_TEXT,
NUMERIC,
PAGINATION,
Expand All @@ -36,5 +34,8 @@ public enum FieldProperty {
// Field content should be treated as data
VERBATIM,

// Contains a year value
YEAR,

YES_NO
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public enum StandardField implements Field {
VERSION("version"),
VOLUME("volume", FieldProperty.NUMERIC),
VOLUMES("volumes", FieldProperty.NUMERIC),
YEAR("year", FieldProperty.NUMERIC),
YEARDIVISION("yeardivision", FieldProperty.YEARDIVISION),
YEARFILED("yearfiled"),
YEAR("year", FieldProperty.NUMERIC, FieldProperty.YEAR),
YEARDIVISION("yeardivision"),
YEARFILED("yearfiled", FieldProperty.NUMERIC, FieldProperty.YEAR),
MR_NUMBER("mrnumber"),
ZBL_NUMBER("zbl"), // needed for fetcher
XDATA("xdata", FieldProperty.MULTIPLE_ENTRY_LINK),
Expand Down