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

Some minor code cleanups #1689

Merged
merged 1 commit into from
Aug 7, 2016
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,6 +16,7 @@
package net.sf.jabref.logic.layout.format;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import net.sf.jabref.logic.layout.LayoutFormatter;
Expand All @@ -27,14 +28,14 @@
*/
public class CompositeFormat implements LayoutFormatter {

private List<LayoutFormatter> formatters;
private final List<LayoutFormatter> formatters;


/**
* If called with this constructor, this formatter does nothing.
*/
public CompositeFormat() {
// Nothing
formatters = Collections.emptyList();
}

public CompositeFormat(LayoutFormatter first, LayoutFormatter second) {
Expand All @@ -48,10 +49,8 @@ public CompositeFormat(LayoutFormatter[] formatters) {
@Override
public String format(String fieldText) {
String result = fieldText;
if (formatters != null) {
for (LayoutFormatter formatter : formatters) {
result = formatter.format(result);
}
for (LayoutFormatter formatter : formatters) {
result = formatter.format(result);
}
return result;
}
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/net/sf/jabref/model/DuplicateCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -85,31 +86,29 @@ public static boolean isDuplicate(BibEntry one, BibEntry two, BibDatabaseMode bi
EntryType type = EntryTypes.getTypeOrDefault(one.getType(), bibDatabaseMode);

// The check if they have the same required fields:
java.util.List<String> var = type.getRequiredFieldsFlat();
String[] fields = var.toArray(new String[var.size()]);
List<String> var = type.getRequiredFieldsFlat();
double[] req;
if (fields == null) {
if (var == null) {
req = new double[]{0., 0.};
} else {
req = DuplicateCheck.compareFieldSet(fields, one, two);
req = DuplicateCheck.compareFieldSet(var, one, two);
}

if (Math.abs(req[0] - DuplicateCheck.duplicateThreshold) > DuplicateCheck.DOUBT_RANGE) {
// Far from the threshold value, so we base our decision on the req. fields only
return req[0] >= DuplicateCheck.duplicateThreshold;
}
// Close to the threshold value, so we take a look at the optional fields, if any:
java.util.List<String> optionalFields = type.getOptionalFields();
fields = optionalFields.toArray(new String[optionalFields.size()]);
if (fields != null) {
double[] opt = DuplicateCheck.compareFieldSet(fields, one, two);
List<String> optionalFields = type.getOptionalFields();
if (optionalFields != null) {
double[] opt = DuplicateCheck.compareFieldSet(optionalFields, one, two);
double totValue = ((DuplicateCheck.REQUIRED_WEIGHT * req[0] * req[1]) + (opt[0] * opt[1])) / ((req[1] * DuplicateCheck.REQUIRED_WEIGHT) + opt[1]);
return totValue >= DuplicateCheck.duplicateThreshold;
}
return req[0] >= DuplicateCheck.duplicateThreshold;
}

private static double[] compareFieldSet(String[] fields, BibEntry one, BibEntry two) {
private static double[] compareFieldSet(List<String> fields, BibEntry one, BibEntry two) {
double res = 0;
double totWeights = 0.;
for (String field : fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@
public class CompositeFormatTest {

@Test
public void testComposite() {

{
LayoutFormatter f = new CompositeFormat();
Assert.assertEquals("No Change", f.format("No Change"));
}
{
LayoutFormatter f = new CompositeFormat(new LayoutFormatter[] {
fieldText -> fieldText + fieldText,
fieldText -> "A" + fieldText,
fieldText -> "B" + fieldText});

Assert.assertEquals("BAff", f.format("f"));
}

LayoutFormatter f = new CompositeFormat(new AuthorOrgSci(),
new NoSpaceBetweenAbbreviations());
public void testEmptyComposite() {
LayoutFormatter f = new CompositeFormat();
Assert.assertEquals("No Change", f.format("No Change"));
}

@Test
public void testArrayComposite() {
LayoutFormatter f = new CompositeFormat(new LayoutFormatter[] {fieldText -> fieldText + fieldText,
fieldText -> "A" + fieldText, fieldText -> "B" + fieldText});

Assert.assertEquals("BAff", f.format("f"));
}

@Test
public void testDoubleComposite() {

LayoutFormatter f = new CompositeFormat(new AuthorOrgSci(), new NoSpaceBetweenAbbreviations());
LayoutFormatter first = new AuthorOrgSci();
LayoutFormatter second = new NoSpaceBetweenAbbreviations();

Assert.assertEquals(second.format(first.format("John Flynn and Sabine Gartska")), f.format("John Flynn and Sabine Gartska"));
Assert.assertEquals(second.format(first.format("Sa Makridakis and Sa Ca Wheelwright and Va Ea McGee")), f.format("Sa Makridakis and Sa Ca Wheelwright and Va Ea McGee"));
Assert.assertEquals(second.format(first.format("John Flynn and Sabine Gartska")),
f.format("John Flynn and Sabine Gartska"));
Assert.assertEquals(second.format(first.format("Sa Makridakis and Sa Ca Wheelwright and Va Ea McGee")),
f.format("Sa Makridakis and Sa Ca Wheelwright and Va Ea McGee"));
}

}