Skip to content

Commit bc9868f

Browse files
kopporSiedlerchr
andauthored
Introduce PlausibilityComparatorFactory (#14396)
* Introduce PlausibilityComparatorFactory * Update jablib/src/main/java/org/jabref/logic/bibtex/comparator/plausibility/EntryTypePlausibilityComparator.java Co-authored-by: Christoph <siedlerkiller@gmail.com> * Use Markdown comments --------- Co-authored-by: Christoph <siedlerkiller@gmail.com>
1 parent 5e08c82 commit bc9868f

File tree

8 files changed

+76
-27
lines changed

8 files changed

+76
-27
lines changed

jabgui/src/main/java/org/jabref/gui/mergeentries/threewaymerge/FieldRowViewModel.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@
1616

1717
import org.jabref.gui.mergeentries.threewaymerge.fieldsmerger.FieldMerger;
1818
import org.jabref.gui.mergeentries.threewaymerge.fieldsmerger.FieldMergerFactory;
19-
import org.jabref.logic.bibtex.comparator.ComparisonResult;
20-
import org.jabref.logic.bibtex.comparator.YearFieldValuePlausibilityComparator;
19+
import org.jabref.logic.bibtex.comparator.plausibility.PlausibilityComparatorFactory;
2120
import org.jabref.logic.util.strings.StringUtil;
2221
import org.jabref.model.entry.BibEntry;
2322
import org.jabref.model.entry.field.Field;
2423
import org.jabref.model.entry.field.FieldTextMapper;
2524
import org.jabref.model.entry.field.InternalField;
26-
import org.jabref.model.entry.field.StandardField;
2725
import org.jabref.model.entry.types.EntryTypeFactory;
28-
import org.jabref.model.entry.types.StandardEntryType;
2926

3027
import com.tobiasdiez.easybind.EasyBind;
3128
import org.slf4j.Logger;
@@ -131,19 +128,18 @@ public void autoSelectBetterValue() {
131128
String leftValue = getLeftFieldValue();
132129
String rightValue = getRightFieldValue();
133130

134-
if (StandardField.YEAR == field) {
135-
YearFieldValuePlausibilityComparator comparator = new YearFieldValuePlausibilityComparator();
136-
ComparisonResult comparison = comparator.compare(leftValue, rightValue);
137-
if (ComparisonResult.RIGHT_BETTER == comparison) {
138-
selectRightValue();
139-
} else if (ComparisonResult.LEFT_BETTER == comparison) {
140-
selectLeftValue();
141-
}
142-
} else if (InternalField.TYPE_HEADER == field) {
143-
if (leftValue.equalsIgnoreCase(StandardEntryType.Misc.getName())) {
144-
selectRightValue();
145-
}
146-
}
131+
PlausibilityComparatorFactory.INSTANCE
132+
.getPlausibilityComparator(field)
133+
.map(comparator -> comparator.compare(leftValue, rightValue))
134+
.ifPresent(result -> {
135+
switch (result) {
136+
case RIGHT_BETTER ->
137+
selectRightValue();
138+
case LEFT_BETTER ->
139+
selectLeftValue();
140+
default -> { /* nothing */ }
141+
}
142+
});
147143
}
148144

149145
public void selectNonEmptyValue() {

jablib/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
exports org.jabref.logic.git.merge.planning;
121121
exports org.jabref.logic.git.merge.execution;
122122
exports org.jabref.model.sciteTallies;
123+
exports org.jabref.logic.bibtex.comparator.plausibility;
123124

124125
requires java.base;
125126

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.jabref.logic.bibtex.comparator.plausibility;
2+
3+
import org.jabref.logic.bibtex.comparator.ComparisonResult;
4+
import org.jabref.model.entry.types.StandardEntryType;
5+
6+
/// If the left entry type is misc then prefer the the right value
7+
public class EntryTypePlausibilityComparator implements FieldValuePlausibilityComparator {
8+
9+
// Only the factory may instantiate this
10+
EntryTypePlausibilityComparator() {
11+
}
12+
13+
@Override
14+
public ComparisonResult compare(String leftValue, String rightValue) {
15+
if (leftValue.equalsIgnoreCase(StandardEntryType.Misc.getName())) {
16+
return ComparisonResult.RIGHT_BETTER;
17+
}
18+
return ComparisonResult.UNDETERMINED;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
package org.jabref.logic.bibtex.comparator;
1+
package org.jabref.logic.bibtex.comparator.plausibility;
22

3-
public abstract class FieldValuePlausibilityComparator {
3+
import org.jabref.logic.bibtex.comparator.ComparisonResult;
4+
5+
public interface FieldValuePlausibilityComparator {
46
/**
57
* Compares the plausibility of two field values.
68
*
79
* @param leftValue value from the library (or candidate)
810
* @param rightValue value from the fetcher (or existing record)
911
* @return ComparisonResult indicating which field is more plausible: RIGHT_BETTER, LEFT_BETTER, or UNDETERMINED
1012
*/
11-
public abstract ComparisonResult compare(String leftValue, String rightValue);
13+
ComparisonResult compare(String leftValue, String rightValue);
1214
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.jabref.logic.bibtex.comparator.plausibility;
2+
3+
import java.util.Optional;
4+
5+
import org.jabref.model.entry.field.Field;
6+
import org.jabref.model.entry.field.FieldProperty;
7+
import org.jabref.model.entry.field.InternalField;
8+
9+
public enum PlausibilityComparatorFactory {
10+
11+
// Single instance ensured by Java compiler
12+
INSTANCE;
13+
14+
public Optional<FieldValuePlausibilityComparator> getPlausibilityComparator(Field field) {
15+
// Similar code as [org.jabref.gui.fieldeditors.FieldEditors.getForField]
16+
if (field.getProperties().contains(FieldProperty.YEAR)) {
17+
return Optional.of(new YearFieldValuePlausibilityComparator());
18+
}
19+
if (InternalField.TYPE_HEADER == field) {
20+
return Optional.of(new EntryTypePlausibilityComparator());
21+
}
22+
return Optional.empty();
23+
}
24+
}

jablib/src/main/java/org/jabref/logic/bibtex/comparator/YearFieldValuePlausibilityComparator.java renamed to jablib/src/main/java/org/jabref/logic/bibtex/comparator/plausibility/YearFieldValuePlausibilityComparator.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
package org.jabref.logic.bibtex.comparator;
1+
package org.jabref.logic.bibtex.comparator.plausibility;
22

33
import java.time.Year;
44
import java.util.Optional;
55
import java.util.regex.Matcher;
66
import java.util.regex.Pattern;
77

8+
import org.jabref.logic.bibtex.comparator.ComparisonResult;
89
import org.jabref.logic.integrity.YearChecker;
910
import org.jabref.logic.util.strings.StringUtil;
1011

11-
public class YearFieldValuePlausibilityComparator extends FieldValuePlausibilityComparator {
12+
public class YearFieldValuePlausibilityComparator implements FieldValuePlausibilityComparator {
1213

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

16+
// Only the factor may instantiate this
17+
YearFieldValuePlausibilityComparator() {
18+
}
19+
1520
/**
1621
* Compares the plausibility of two field values.
1722
*

jablib/src/main/java/org/jabref/model/entry/field/FieldProperty.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ public enum FieldProperty {
2323

2424
MONTH,
2525

26-
YEARDIVISION,
27-
2826
MULTILINE_TEXT,
2927
NUMERIC,
3028
PAGINATION,
@@ -36,5 +34,8 @@ public enum FieldProperty {
3634
// Field content should be treated as data
3735
VERBATIM,
3836

37+
// Contains a year value
38+
YEAR,
39+
3940
YES_NO
4041
}

jablib/src/main/java/org/jabref/model/entry/field/StandardField.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ public enum StandardField implements Field {
126126
VERSION("version"),
127127
VOLUME("volume", FieldProperty.NUMERIC),
128128
VOLUMES("volumes", FieldProperty.NUMERIC),
129-
YEAR("year", FieldProperty.NUMERIC),
130-
YEARDIVISION("yeardivision", FieldProperty.YEARDIVISION),
131-
YEARFILED("yearfiled"),
129+
YEAR("year", FieldProperty.NUMERIC, FieldProperty.YEAR),
130+
YEARDIVISION("yeardivision"),
131+
YEARFILED("yearfiled", FieldProperty.NUMERIC, FieldProperty.YEAR),
132132
MR_NUMBER("mrnumber"),
133133
ZBL_NUMBER("zbl"), // needed for fetcher
134134
XDATA("xdata", FieldProperty.MULTIPLE_ENTRY_LINK),

0 commit comments

Comments
 (0)