Skip to content

Commit

Permalink
Replace String[] with List<String> in BibtexEntryType
Browse files Browse the repository at this point in the history
  • Loading branch information
lenhard committed Sep 23, 2015
1 parent 9c5c0c2 commit 160bf6d
Show file tree
Hide file tree
Showing 13 changed files with 513 additions and 842 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -1319,8 +1319,8 @@ public void storeCustomEntryType(CustomEntryType tp, int number) {
String nr = "" + number;
put(JabRefPreferences.CUSTOM_TYPE_NAME + nr, tp.getName());
put(JabRefPreferences.CUSTOM_TYPE_REQ + nr, tp.getRequiredFieldsString());
putStringArray(JabRefPreferences.CUSTOM_TYPE_OPT + nr, tp.getOptionalFields());
putStringArray(JabRefPreferences.CUSTOM_TYPE_PRIOPT + nr, tp.getPrimaryOptionalFields());
putStringArray(JabRefPreferences.CUSTOM_TYPE_OPT + nr, tp.getOptionalFields().toArray(new String[0]));
putStringArray(JabRefPreferences.CUSTOM_TYPE_PRIOPT + nr, tp.getPrimaryOptionalFields().toArray(new String[0]));
}

/**
Expand Down
103 changes: 32 additions & 71 deletions src/main/java/net/sf/jabref/gui/EntryCustomizationDialog2.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public class EntryCustomizationDialog2 extends JDialog implements ListSelectionL
private boolean biblatexMode;


/** Creates a new instance of EntryCustomizationDialog2 */
/**
* Creates a new instance of EntryCustomizationDialog2
*/
public EntryCustomizationDialog2(JabRefFrame frame) {
super(frame, Localization.lang("Customize entry types"), false);

Expand All @@ -83,9 +85,9 @@ private void initGui() {
right.setLayout(new GridLayout(biblatexMode ? 2 : 1, 2));

java.util.List<String> entryTypes = new ArrayList<String>();
for (String s : BibtexEntryType.getAllTypes()) {
entryTypes.add(s);
}
for (String s : BibtexEntryType.getAllTypes()) {
entryTypes.add(s);
}

typeComp = new EntryTypeList(entryTypes);
typeComp.addListSelectionListener(this);
Expand Down Expand Up @@ -114,7 +116,7 @@ private void initGui() {
right.add(new JPanel());
right.add(optComp2);
}

//right.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), Globals.lang("Fields")));
right.setBorder(BorderFactory.createEtchedBorder());
ok = new JButton("Ok");
Expand Down Expand Up @@ -177,38 +179,18 @@ public void valueChanged(ListSelectionEvent e) {
}
List<String> rl = reqLists.get(s);
if (rl == null) {
BibtexEntryType type = BibtexEntryType.getType(s);
BibtexEntryType type = BibtexEntryType.getType(s);
if (type != null) {
String[] rf = type.getRequiredFieldsForCustomization();
List<String> req;
if (rf != null) {
req = java.util.Arrays.asList(rf);
} else {
req = new ArrayList<String>();
}
List<String> req = type.getRequiredFieldsForCustomization();

List<String> opt;
if (!biblatexMode) {
String[] of = type.getOptionalFields();
if (of != null) {
opt = java.util.Arrays.asList(of);
} else {
opt = new ArrayList<String>();
}
opt = type.getOptionalFields();
} else {
String[] priOf = type.getPrimaryOptionalFields();
if (priOf != null) {
opt = java.util.Arrays.asList(priOf);
} else {
opt = new ArrayList<String>();
}
String[] secOpt = type.getSecondaryOptionalFields();
List<String> opt2;
if (secOpt != null) {
opt2 = java.util.Arrays.asList(secOpt);
} else {
opt2 = new ArrayList<String>();
}
opt = type.getPrimaryOptionalFields();

List<String> opt2 = type.getSecondaryOptionalFields();

optComp2.setFields(opt2);
optComp2.setEnabled(true);
}
Expand All @@ -222,8 +204,7 @@ public void valueChanged(ListSelectionEvent e) {
reqComp.setEnabled(true);
optComp.setFields(new ArrayList<String>());
optComp.setEnabled(true);
if (biblatexMode)
{
if (biblatexMode) {
optComp2.setFields(new ArrayList<String>());
optComp2.setEnabled(true);
}
Expand Down Expand Up @@ -281,11 +262,11 @@ private void applyChanges() {

BibtexEntryType oldType = BibtexEntryType.getType(stringListEntry.getKey());
if (oldType != null) {
String[] oldReq = oldType.getRequiredFields();
String[] oldOpt = oldType.getOptionalFields();
String[] oldReq = oldType.getRequiredFields().toArray(new String[0]);
String[] oldOpt = oldType.getOptionalFields().toArray(new String[0]);
if (biblatexMode) {
String[] oldPriOpt = oldType.getPrimaryOptionalFields();
String[] oldSecOpt = oldType.getSecondaryOptionalFields();
String[] oldPriOpt = oldType.getPrimaryOptionalFields().toArray(new String[0]);
String[] oldSecOpt = oldType.getSecondaryOptionalFields().toArray(new String[0]);
if (equalArrays(oldReq, reqStr) && equalArrays(oldPriOpt, optStr) &&
equalArrays(oldSecOpt, opt2Str)) {
changesMade = false;
Expand Down Expand Up @@ -354,16 +335,13 @@ private void typeDeletion(String name) {
}

private boolean equalArrays(String[] one, String[] two) {
if ((one == null) && (two == null))
{
if ((one == null) && (two == null)) {
return true; // Both null.
}
if ((one == null) || (two == null))
{
if ((one == null) || (two == null)) {
return false; // One of them null, the other not.
}
if (one.length != two.length)
{
if (one.length != two.length) {
return false; // Different length.
}
// If we get here, we know that both are non-null, and that they have the same length.
Expand Down Expand Up @@ -438,36 +416,19 @@ public void actionPerformed(ActionEvent e) {

BibtexEntryType type = BibtexEntryType.getStandardType(lastSelected);
if (type != null) {
String[] rf = type.getRequiredFieldsForCustomization();
String[] of = type.getOptionalFields();
List<String> req;
List<String> opt1;
List<String> opt2;
if (rf != null) {
req = java.util.Arrays.asList(rf);
} else {
req = new ArrayList<String>();
}
List<String> of = type.getOptionalFields();
List<String> req = type.getRequiredFieldsForCustomization();;
List<String> opt1 = new ArrayList<>();
List<String> opt2 = new ArrayList<>();;

opt1 = new ArrayList<String>();
opt2 = new ArrayList<String>();
if (biblatexMode) {
if (of != null)
{
String[] priOptArray = type.getPrimaryOptionalFields();
String[] secOptArray = type.getSecondaryOptionalFields();
if (priOptArray != null) {
opt1 = java.util.Arrays.asList(priOptArray);
}
if (secOptArray != null) {
opt2 = java.util.Arrays.asList(secOptArray);
}
if (of.size() != 0) {
opt1 = type.getPrimaryOptionalFields();
opt2 = type.getSecondaryOptionalFields();
}
}
else
{
if (of != null) {
opt1 = java.util.Arrays.asList(of);
} else {
if (of.size() != 0) {
opt1 = of;
}
}

Expand Down
16 changes: 6 additions & 10 deletions src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,8 @@ public EntryEditor(JabRefFrame frame, BasePanel panel, BibtexEntry entry) {
private void setupFieldPanels() {
tabbed.removeAll();
tabs.clear();
String[] fields = entry.getRequiredFields();
List<String> fieldList = entry.getRequiredFields();

List<String> fieldList = null;
if (fields != null) {
fieldList = java.util.Arrays.asList(fields);
}
EntryEditorTab reqPan = new EntryEditorTab(frame, panel, fieldList, this, true, false, Localization.lang("Required fields"));
if (reqPan.fileListEditor != null) {
fileListEditor = reqPan.fileListEditor;
Expand All @@ -222,10 +218,10 @@ private void setupFieldPanels() {
.getPane(), Localization.lang("Show required fields"));
tabs.add(reqPan);

if (entry.getOptionalFields() != null && entry.getOptionalFields().length >= 1) {
if (entry.getOptionalFields() != null && entry.getOptionalFields().size() >= 1) {
EntryEditorTab optPan;
if (!prefs.getBoolean(JabRefPreferences.BIBLATEX_MODE)) {
optPan = new EntryEditorTab(frame, panel, java.util.Arrays.asList(entry.getOptionalFields()), this,
optPan = new EntryEditorTab(frame, panel, entry.getOptionalFields(), this,
false, false, Localization.lang("Optional fields"));
if (optPan.fileListEditor != null) {
fileListEditor = optPan.fileListEditor;
Expand All @@ -235,7 +231,7 @@ private void setupFieldPanels() {
tabs.add(optPan);
} else {
optPan = new EntryEditorTab(frame, panel,
java.util.Arrays.asList(entry.getType().getPrimaryOptionalFields()), this,
entry.getType().getPrimaryOptionalFields(), this,
false, true, Localization.lang("Optional fields"));
if (optPan.fileListEditor != null) {
fileListEditor = optPan.fileListEditor;
Expand All @@ -247,8 +243,8 @@ private void setupFieldPanels() {
Set<String> deprecatedFields = new HashSet<>(BibtexEntry.FIELD_ALIASES_OLD_TO_NEW.keySet());
deprecatedFields.add("year");
deprecatedFields.add("month");
String[] secondaryOptionalFields = entry.getType().getSecondaryOptionalFields();
String[] optionalFieldsNotPrimaryOrDeprecated = StringUtil.getRemainder(secondaryOptionalFields,
List<String> secondaryOptionalFields = entry.getType().getSecondaryOptionalFields();
String[] optionalFieldsNotPrimaryOrDeprecated = StringUtil.getRemainder((secondaryOptionalFields.toArray(new String[0])),
deprecatedFields.toArray(new String[deprecatedFields.size()]));

// Get list of all optional fields of this entry and their aliases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private String[] getRequiredFieldsSorted(BibtexEntry entry) {

// put into chache if necessary
if (sortedFields == null) {
sortedFields = entry.getRequiredFields().clone();
sortedFields = entry.getRequiredFields().stream().toArray(size -> new String[size]);

This comment has been minimized.

Copy link
@koppor

koppor Sep 23, 2015

Member

Why isn't a new Collection made and Collections.sort() used? - Is sortedFields still an Array instead of a List?

This comment has been minimized.

Copy link
@lenhard

lenhard Sep 23, 2015

Author Member

That is a very good point, thanks :) My primary aim for this commit was getting the code to compile again. I'll improve the sorting and make the cache use a list instead of an array as well.

Arrays.sort(sortedFields);
requiredFieldsSorted.put(entryTypeName, sortedFields);
}
Expand All @@ -170,7 +170,7 @@ private String[] getOptionalFieldsSorted(BibtexEntry entry) {

// put into chache if necessary
if (sortedFields == null) {
sortedFields = entry.getOptionalFields().clone();
sortedFields = entry.getOptionalFields().stream().toArray(size -> new String[size]);
Arrays.sort(sortedFields);
optionalFieldsSorted.put(entryTypeName, sortedFields);
}
Expand All @@ -195,7 +195,7 @@ private void writeRequiredFieldsFirstRemainingFieldsSecond(BibtexEntry entry, Wr
written.add(BibtexEntry.KEY_FIELD);
boolean hasWritten = false;
// Write required fields first.
String[] fields = entry.getRequiredFields();
List<String> fields = entry.getRequiredFields();
if (fields != null) {
for (String value : fields) {
hasWritten = hasWritten | writeField(entry, out, value, hasWritten);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/logic/bibtex/DuplicateCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static boolean isDuplicate(BibtexEntry one, BibtexEntry two) {
}

// The check if they have the same required fields:
String[] fields = one.getType().getRequiredFields();
String[] fields = one.getType().getRequiredFields().toArray(new String[0]);
double[] req;
if (fields == null) {
req = new double[]{0., 0.};
Expand All @@ -83,7 +83,7 @@ public static boolean isDuplicate(BibtexEntry one, BibtexEntry two) {
return req[0] >= DuplicateCheck.duplicateThreshold;
} else {
// Close to the threshold value, so we take a look at the optional fields, if any:
fields = one.getType().getOptionalFields();
fields = one.getType().getOptionalFields().toArray(new String[0]);
if (fields != null) {
double[] opt = DuplicateCheck.compareFieldSet(fields, one, two);
double totValue = (DuplicateCheck.reqWeight * req[0] * req[1] + opt[0] * opt[1]) / (req[1] * DuplicateCheck.reqWeight + opt[1]);
Expand Down

0 comments on commit 160bf6d

Please sign in to comment.