Skip to content

Commit

Permalink
Mark some methods as deprecated in BibEntry and BibDatabase (JabRef#1913
Browse files Browse the repository at this point in the history
)

* Mark some methods as deprecated in BibEntry and BibDatabase

* Rename getResolvedFieldOrAlias

* Use flatmap
  • Loading branch information
tobiasdiez authored and lenhard committed Sep 4, 2016
1 parent 357be5c commit a9eb978
Show file tree
Hide file tree
Showing 33 changed files with 188 additions and 184 deletions.
2 changes: 1 addition & 1 deletion src/jmh/java/net/sf/jabref/benchmarks/Benchmarks.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void init() throws Exception {
entry.setField("keyword", "testkeyword");
entry.setField("year", "1" + i);
entry.setField("rnd", "2" + randomizer.nextInt());
database.insertEntry(entry);
database.insertEntryWithDuplicationCheck(entry);
}
BibtexDatabaseWriter<StringSaveSession> databaseWriter = new BibtexDatabaseWriter<>(StringSaveSession::new);
StringSaveSession saveSession = databaseWriter.savePartOfDatabase(
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/collab/EntryAddChange.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public EntryAddChange(BibEntry diskEntry) {
@Override
public boolean makeChange(BasePanel panel, BibDatabase secondary, NamedCompound undoEdit) {
diskEntry.setId(IdGenerator.next());
panel.getDatabase().insertEntry(diskEntry);
secondary.insertEntry(diskEntry);
panel.getDatabase().insertEntryWithDuplicationCheck(diskEntry);
secondary.insertEntryWithDuplicationCheck(diskEntry);
undoEdit.addEdit(new UndoableInsertEntry(panel.getDatabase(), diskEntry, panel));
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ private boolean tryXmpImport(String fileName, ExternalFileType fileType, NamedCo

aXmpEntriesInFile.setId(IdGenerator.next());
edits.addEdit(new UndoableInsertEntry(panel.getDatabase(), aXmpEntriesInFile, panel));
panel.getDatabase().insertEntry(aXmpEntriesInFile);
panel.getDatabase().insertEntryWithDuplicationCheck(aXmpEntriesInFile);
doLink(aXmpEntriesInFile, fileType, destFilename, true, edits);

}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public void run() {
bes = entry;
// Store the old value:
oldvals.put(bes, bes.getCiteKeyOptional().orElse(null));
bibDatabaseContext.getDatabase().setCiteKeyForEntry(bes, null);
bes.clearCiteKey();
}
}

Expand Down Expand Up @@ -824,7 +824,7 @@ private void paste() {
// independently of the copied
// ones.
be.setId(IdGenerator.next());
bibDatabaseContext.getDatabase().insertEntry(be);
bibDatabaseContext.getDatabase().insertEntryWithDuplicationCheck(be);

ce.addEdit(new UndoableInsertEntry(bibDatabaseContext.getDatabase(), be, BasePanel.this));

Expand Down Expand Up @@ -1136,7 +1136,7 @@ public BibEntry newEntry(EntryType type) {
String id = IdGenerator.next();
final BibEntry be = new BibEntry(id, actualType.getName());
try {
bibDatabaseContext.getDatabase().insertEntry(be);
bibDatabaseContext.getDatabase().insertEntryWithDuplicationCheck(be);
// Set owner/timestamp if options are enabled:
List<BibEntry> list = new ArrayList<>();
list.add(be);
Expand Down Expand Up @@ -1290,7 +1290,7 @@ public void listen(EntryChangedEvent entryChangedEvent) {
public void insertEntry(final BibEntry bibEntry) {
if (bibEntry != null) {
try {
bibDatabaseContext.getDatabase().insertEntry(bibEntry);
bibDatabaseContext.getDatabase().insertEntryWithDuplicationCheck(bibEntry);
if (Globals.prefs.getBoolean(JabRefPreferences.USE_OWNER)) {
// Set owner field to default value
UpdateField.setAutomaticFields(bibEntry, true, true, Globals.prefs.getUpdateFieldPreferences());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/gui/DuplicateSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void run() {
// and adding merged entries:
if (!toAdd.isEmpty()) {
for (BibEntry entry : toAdd) {
panel.getDatabase().insertEntry(entry);
panel.getDatabase().insertEntryWithDuplicationCheck(entry);
ce.addEdit(new UndoableInsertEntry(panel.getDatabase(), entry, panel));
}
panel.markBaseChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public int addEntriesFromFiles(List<File> files,
if (!database.containsEntryWithId(entry.get().getId())) {
// Work around SIDE EFFECT of creator.createEntry. The EntryFromPDFCreator also creates the entry in the table
// Therefore, we only insert the entry if it is not already present
if (database.insertEntry(entry.get())) {
if (database.insertEntryWithDuplicationCheck(entry.get())) {
importGUIMessages.add("Problem importing " + f.getPath() + ": Insert into BibDatabase failed.");
} else {
count++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ private void generateKeyForEntry(BibEntry entry) {

entry.setId(IdGenerator.next());
// Add the entry to the database we are working with:
database.insertEntry(entry);
database.insertEntryWithDuplicationCheck(entry);

// Generate a unique key:
BibtexKeyPatternUtil.makeLabel(localMetaData, database, entry,
Expand Down Expand Up @@ -501,7 +501,7 @@ private void generateKeys() {
for (BibEntry entry : entries) {

entry.setId(IdGenerator.next());
database.insertEntry(entry);
database.insertEntryWithDuplicationCheck(entry);

BibtexKeyPatternUtil.makeLabel(localMetaData, database, entry,
Globals.prefs.getBibtexKeyPatternPreferences());
Expand Down Expand Up @@ -735,7 +735,7 @@ private void addSelectedEntries(NamedCompound ce, final List<BibEntry> selected)
}

entry.setId(IdGenerator.next());
panel.getDatabase().insertEntry(entry);
panel.getDatabase().insertEntryWithDuplicationCheck(entry);
ce.addEdit(new UndoableInsertEntry(panel.getDatabase(), entry, panel));

}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/gui/importer/ImportMenuItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private ParserResult mergeImportResults(List<ImportFormatReader.UnknownFormatImp

// Merge entries:
for (BibEntry entry : pr.getDatabase().getEntries()) {
database.insertEntry(entry);
database.insertEntryWithDuplicationCheck(entry);
}

// Merge strings:
Expand All @@ -233,7 +233,7 @@ private ParserResult mergeImportResults(List<ImportFormatReader.UnknownFormatImp
if (markEntries) {
EntryMarker.markEntry(entry, EntryMarker.IMPORT_MARK_LEVEL, false, new NamedCompound(""));
}
database.insertEntry(entry);
database.insertEntryWithDuplicationCheck(entry);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private static void mergeFromBibtex(JabRefFrame frame, BasePanel panel, ParserRe
be.setId(IdGenerator.next());
UpdateField.setAutomaticFields(be, overwriteOwner, overwriteTimeStamp,
Globals.prefs.getUpdateFieldPreferences());
database.insertEntry(be);
database.insertEntryWithDuplicationCheck(be);
appendedEntries.add(be);
originalEntries.add(originalEntry);
ce.addEdit(new UndoableInsertEntry(database, be, panel));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public Object getColumnValue(BibEntry entry) {

Optional<String> content = Optional.empty();
for (String field : bibtexFields) {
content = BibDatabase.getResolvedField(field, entry, database.orElse(null));
content = entry.getResolvedFieldOrAlias(field, database.orElse(null));
if (content.isPresent()) {
isNameColumn = InternalBibtexFields.getFieldProperties(field).contains(FieldProperty.PERSON_NAMES);
break;
Expand Down Expand Up @@ -127,7 +127,7 @@ public JLabel getHeaderLabel() {
* The reasons for being different are (combinations may also happen):
* - The entry has a crossref where the field content is obtained from
* - The field has a string in it (which getColumnValue() resolves)
* - There are some alias fields. For example, if the entry has a date field but no year field, getResolvedField()
* - There are some alias fields. For example, if the entry has a date field but no year field, getResolvedFieldOrAlias()
* will return the year value from the date field when queried for year
*
*
Expand All @@ -148,7 +148,7 @@ public boolean isResolved(BibEntry entry) {
return false;
} else {
plainFieldContent = entry.getField(field);
resolvedFieldContent = BibDatabase.getResolvedField(field, entry, database.orElse(null));
resolvedFieldContent = entry.getResolvedFieldOrAlias(field, database.orElse(null));
}

if (resolvedFieldContent.isPresent()) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/gui/openoffice/OOBibBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1366,13 +1366,13 @@ public BibDatabase generateDatabase(List<BibDatabase> databases)
BibEntry clonedEntry = (BibEntry) entry.get().clone();
clonedEntry.setId(IdGenerator.next());
// Insert a copy of the entry
resultDatabase.insertEntry(clonedEntry);
resultDatabase.insertEntryWithDuplicationCheck(clonedEntry);
// Check if the cloned entry has a crossref field
clonedEntry.getField(FieldName.CROSSREF).ifPresent(crossref -> {
// If the crossref entry is not already in the database
if (!resultDatabase.getEntryByKey(crossref).isPresent()) {
// Add it if it is in the current database
loopDatabase.getEntryByKey(crossref).ifPresent(resultDatabase::insertEntry);
loopDatabase.getEntryByKey(crossref).ifPresent(resultDatabase::insertEntryWithDuplicationCheck);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void undo() {
@Override
public void redo() {
super.redo();
base.insertEntry(entry);
base.insertEntryWithDuplicationCheck(entry);
}

}
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/logic/auxparser/AuxParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,6 @@ private void resolveCrossReferences(BibEntry entry, AuxParserResult result) {
private void insertEntry(BibEntry entry, AuxParserResult result) {
BibEntry clonedEntry = (BibEntry) entry.clone();
clonedEntry.setId(IdGenerator.next());
result.getGeneratedBibDatabase().insertEntry(clonedEntry);
result.getGeneratedBibDatabase().insertEntryWithDuplicationCheck(clonedEntry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public Document getDOMrepresentation() {
}

private String getField(BibEntry e, String field) {
return BibDatabase.getResolvedField(field, e, database).orElse("");
return e.getResolvedFieldOrAlias(field, database).orElse("");
}

private void addTableCell(Document doc, Element parent, String content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private void parseAndAddEntry(String type) {
// store complete parsed serialization (comments, type definition + type contents)
entry.setParsedSerialization(commentsAndEntryTypeDefinition + dumpTextReadSoFarToString());

boolean duplicateKey = database.insertEntry(entry);
boolean duplicateKey = database.insertEntryWithDuplicationCheck(entry);
if (duplicateKey) {
parserResult.addDuplicateKey(entry.getCiteKey());
} else if (!entry.getCiteKeyOptional().isPresent() || entry.getCiteKeyOptional().get().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public ParserResult importDatabase(Path filePath, Charset defaultEncoding) {
ParserResult parserResult = new ParserResult(result);
Optional<BibEntry> entry = DOItoBibTeX.getEntryFromDOI(doi.get().getDOI(), parserResult,
importFormatPreferences);
entry.ifPresent(parserResult.getDatabase()::insertEntry);
entry.ifPresent(parserResult.getDatabase()::insertEntryWithDuplicationCheck);
return parserResult;
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/net/sf/jabref/logic/layout/LayoutEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ 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).orElse("");
String value = bibtex.getResolvedFieldOrAlias(text, database).orElse("");

// If a post formatter has been set, call it:
if (postFormatter != null) {
Expand All @@ -212,7 +212,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).orElse(null);
return bibtex.getResolvedFieldOrAlias("encoding", database).orElse(null);
default:
return "";
}
Expand All @@ -231,8 +231,8 @@ private String handleOptionField(BibEntry bibtex, BibDatabase database) {
} else {
// changed section begin - arudert
// resolve field (recognized by leading backslash) or text
fieldEntry = text.startsWith("\\") ? BibDatabase
.getResolvedField(text.substring(1), bibtex, database)
fieldEntry = text.startsWith("\\") ? bibtex
.getResolvedFieldOrAlias(text.substring(1), database)
.orElse("") : BibDatabase.getText(text, database);
// changed section end - arudert
}
Expand All @@ -254,13 +254,13 @@ private String handleOptionField(BibEntry bibtex, BibDatabase database) {
private String handleFieldOrGroupStart(BibEntry bibtex, BibDatabase database, Optional<Pattern> highlightPattern) {
Optional<String> field;
if (type == LayoutHelper.IS_GROUP_START) {
field = BibDatabase.getResolvedField(text, bibtex, database);
field = bibtex.getResolvedFieldOrAlias(text, database);
} else if (text.matches(".*(;|(\\&+)).*")) {
// split the strings along &, && or ; for AND formatter
String[] parts = text.split("\\s*(;|(\\&+))\\s*");
field = Optional.empty();
for (String part : parts) {
field = BibDatabase.getResolvedField(part, bibtex, database);
field = bibtex.getResolvedFieldOrAlias(part, database);
if (!field.isPresent()) {
break;
}
Expand All @@ -270,7 +270,7 @@ private String handleFieldOrGroupStart(BibEntry bibtex, BibDatabase database, Op
String[] parts = text.split("\\s*(\\|+)\\s*");
field = Optional.empty();
for (String part : parts) {
field = BibDatabase.getResolvedField(part, bibtex, database);
field = bibtex.getResolvedFieldOrAlias(part, database);
if (field.isPresent()) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ private String getCitationMarkerField(BibEntry entry, BibDatabase database, Stri
String authorField = getStringCitProperty(AUTHOR_FIELD);
String[] fields = field.split(FieldName.FIELD_SEPARATOR);
for (String s : fields) {
Optional<String> content = BibDatabase.getResolvedField(s, entry, database);
Optional<String> content = entry.getResolvedFieldOrAlias(s, database);

if ((content.isPresent()) && !content.get().trim().isEmpty()) {
if (field.equals(authorField) && StringUtil.isInCurlyBrackets(content.get())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public static String getFieldAndFormat(String fieldAndFormat, BibEntry entry, Bi
}

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

if (fieldValue == null) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sf/jabref/logic/xmp/XMPSchemaBibtex.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public void setBibtexEntry(BibEntry entry, BibDatabase database, XMPPreferences
}

for (String field : fields) {
String value = BibDatabase.getResolvedField(field, entry, database).orElse("");
String value = entry.getResolvedFieldOrAlias(field, database).orElse("");
if (InternalBibtexFields.getFieldProperties(field).contains(FieldProperty.PERSON_NAMES)) {
setPersonList(field, value);
} else {
Expand Down
Loading

0 comments on commit a9eb978

Please sign in to comment.