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

Replaced output of getResolvedField to Optional<String> #1650

Merged
merged 1 commit into from
Aug 2, 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
7 changes: 2 additions & 5 deletions src/main/java/net/sf/jabref/external/RegExpFileSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,9 @@ public static String getFieldAndFormat(String fieldAndFormat, BibEntry entry, Bi
return "";
}

String fieldValue = BibDatabase.getResolvedField(beforeColon, entry, database);

// If no field value was found, try to interpret it as a key generator field marker:
if (fieldValue == null) {
fieldValue = LabelPatternUtil.makeLabel(entry, beforeColon);
}
String fieldValue = BibDatabase.getResolvedField(beforeColon, entry, database)
.orElse(LabelPatternUtil.makeLabel(entry, beforeColon));

if (fieldValue == null) {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ public Document getDOMrepresentation() {
}

private String getField(BibEntry e, String field) {
String s = BibDatabase.getResolvedField(field, e, database);
return s == null ? "" : s;
return BibDatabase.getResolvedField(field, e, database).orElse("");
}

private void addTableCell(Document doc, Element parent, String content) {
Expand Down
34 changes: 13 additions & 21 deletions src/main/java/net/sf/jabref/logic/layout/LayoutEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,8 @@ public String doLayout(BibEntry bibtex, BibDatabase database, Optional<Pattern>
case LayoutHelper.IS_LAYOUT_TEXT:
return text;
case LayoutHelper.IS_SIMPLE_FIELD:
String value = BibDatabase.getResolvedField(text, bibtex, database);
String value = BibDatabase.getResolvedField(text, bibtex, database).orElse("");

if (value == null) {
value = "";
}
// If a post formatter has been set, call it:
if (postFormatter != null) {
value = postFormatter.format(value);
Expand All @@ -229,7 +226,7 @@ public String doLayout(BibEntry bibtex, BibDatabase database, Optional<Pattern>
// Printing the encoding name is not supported in entry layouts, only
// in begin/end layouts. This prevents breakage if some users depend
// on a field called "encoding". We simply return this field instead:
return BibDatabase.getResolvedField("encoding", bibtex, database);
return BibDatabase.getResolvedField("encoding", bibtex, database).orElse(null);
default:
return "";
}
Expand All @@ -243,14 +240,10 @@ private String handleOptionField(BibEntry bibtex, BibDatabase database) {
} else {
// changed section begin - arudert
// resolve field (recognized by leading backslash) or text
String fieldText = text.startsWith("\\") ? BibDatabase.getResolvedField(text.substring(1), bibtex,
database) : BibDatabase.getText(text, database);
fieldEntry = text.startsWith("\\") ? BibDatabase
.getResolvedField(text.substring(1), bibtex, database)
.orElse("") : BibDatabase.getText(text, database);
// changed section end - arudert
if (fieldText == null) {
fieldEntry = "";
} else {
fieldEntry = fieldText;
}
}

if (option != null) {
Expand All @@ -268,38 +261,37 @@ private String handleOptionField(BibEntry bibtex, BibDatabase database) {
}

private String handleFieldOrGroupStart(BibEntry bibtex, BibDatabase database, Optional<Pattern> highlightPattern) {
String field;
Optional<String> field;
if (type == LayoutHelper.IS_GROUP_START) {
field = BibDatabase.getResolvedField(text, bibtex, database);
} else if (text.matches(".*(;|(\\&+)).*")) {
Copy link
Member

@Siedlerchr Siedlerchr Jul 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A great improvement would be to compile the regexes to pattern (Pattern.compile(regex) and then use simply
split() on that pattern object , otherwise java creates each regex pattern everytime again.
(I did not know that until I just found it in javadoc 😎 )
https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#split-java.lang.CharSequence-

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Create an issue in koppor/JabRef. ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

28 hits of matches("

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe that is time for another PR.

// split the strings along &, && or ; for AND formatter
String[] parts = text.split("\\s*(;|(\\&+))\\s*");
field = null;
field = Optional.empty();
for (String part : parts) {
field = BibDatabase.getResolvedField(part, bibtex, database);
if (field == null) {
if (!field.isPresent()) {
break;
}

}
} else {
// split the strings along |, || for OR formatter
String[] parts = text.split("\\s*(\\|+)\\s*");
field = null;
field = Optional.empty();
for (String part : parts) {
field = BibDatabase.getResolvedField(part, bibtex, database);
if (field != null) {
if (field.isPresent()) {
break;
}
}
}

if ((field == null) || ((type == LayoutHelper.IS_GROUP_START)
&& field.equalsIgnoreCase(LayoutHelper.getCurrentGroup()))) {
if ((!field.isPresent()) || ((type == LayoutHelper.IS_GROUP_START)
&& field.get().equalsIgnoreCase(LayoutHelper.getCurrentGroup()))) {
return null;
} else {
if (type == LayoutHelper.IS_GROUP_START) {
LayoutHelper.setCurrentGroup(field);
LayoutHelper.setCurrentGroup(field.get());
}
StringBuilder sb = new StringBuilder(100);
String fieldText;
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/net/sf/jabref/logic/openoffice/OOBibStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
Expand Down Expand Up @@ -731,13 +732,13 @@ private String getCitationMarkerField(BibEntry entry, BibDatabase database, Stri
String authorField = getStringCitProperty(AUTHOR_FIELD);
String[] fields = field.split(FieldName.FIELD_SEPARATOR);
for (String s : fields) {
String content = BibDatabase.getResolvedField(s, entry, database);
Optional<String> content = BibDatabase.getResolvedField(s, entry, database);

if ((content != null) && !content.trim().isEmpty()) {
if (field.equals(authorField) && StringUtil.isInCurlyBrackets(content)) {
return "{" + fieldFormatter.format(content) + "}";
if ((content.isPresent()) && !content.get().trim().isEmpty()) {
if (field.equals(authorField) && StringUtil.isInCurlyBrackets(content.get())) {
return "{" + fieldFormatter.format(content.get()) + "}";
}
return fieldFormatter.format(content);
return fieldFormatter.format(content.get());
}
}
// No luck? Return an empty string:
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/net/sf/jabref/logic/xmp/XMPSchemaBibtex.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,7 @@ public void setBibtexEntry(BibEntry entry, BibDatabase database) {
}

for (String field : fields) {
String value = BibDatabase.getResolvedField(field, entry, database);
if (value == null) {
value = "";
}
String value = BibDatabase.getResolvedField(field, entry, database).orElse("");
if (InternalBibtexFields.getFieldExtras(field).contains(FieldProperties.PERSON_NAMES)) {
setPersonList(field, value);
} else {
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/net/sf/jabref/model/database/BibDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ private String resolveContent(String result, Set<String> usedIds) {
return res;
}



/**
* Returns the text stored in the given field of the given bibtex entry
* which belongs to the given database.
Expand All @@ -497,15 +499,15 @@ private String resolveContent(String result, Set<String> usedIds) {
* unset fields in the entry linked by the "crossref" field, if any.
*
* @param field The field to return the value of.
* @param entry maybenull
* The bibtex entry which contains the field.
* @param entry The bibtex entry which contains the field.
* @param database maybenull
* The database of the bibtex entry.
* @return The resolved field value or null if not found.
*/
public static String getResolvedField(String field, BibEntry entry, BibDatabase database) {
public static Optional<String> getResolvedField(String field, BibEntry entry, BibDatabase database) {
Objects.requireNonNull(entry, "entry cannot be null");
if ("bibtextype".equals(field)) {
return EntryUtil.capitalizeFirst(entry.getType());
return Optional.of(EntryUtil.capitalizeFirst(entry.getType()));
}

// TODO: Changed this to also consider alias fields, which is the expected
Expand All @@ -527,7 +529,7 @@ public static String getResolvedField(String field, BibEntry entry, BibDatabase
}
}

return BibDatabase.getText(result.orElse(null), database);
return result.map(resultText -> BibDatabase.getText(resultText, database));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/sf/jabref/model/entry/BibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public boolean allFieldsPresent(List<String> allFields, BibDatabase database) {
return false;
}
} else {
if (BibDatabase.getResolvedField(fieldName, this, database) == null) {
if (!BibDatabase.getResolvedField(fieldName, this, database).isPresent()) {
return false;
}
}
Expand All @@ -433,8 +433,8 @@ private boolean atLeastOnePresent(String[] fieldsToCheck, BibDatabase database)
for (String field : fieldsToCheck) {
String fieldName = toLowerCase(field);

String value = BibDatabase.getResolvedField(fieldName, this, database);
if ((value != null) && !value.isEmpty()) {
Optional<String> value = BibDatabase.getResolvedField(fieldName, this, database);
if ((value.isPresent()) && !value.get().isEmpty()) {
return true;
}
}
Expand Down