Verified Owner
Top Description Fields Constructors Methods
org.jabref.model.database

public Class BibDatabase

extends Object
Class Inheritance
Annotations
@AllowedToUseLogic:Uses StringUtil temporarily
Imports
java.math.BigInteger, java.security.SecureRandom, java.util.ArrayList, .Arrays, .Collection, .Collections, .Comparator, .HashMap, .HashSet, .Iterator, .List, .Map, .Objects, .Optional, .Set, .TreeSet, java.util.concurrent.ConcurrentHashMap, java.util.function.Consumer, java.util.regex.Pattern, java.util.stream.Collectors, javafx.collections.FXCollections, .ObservableList, org.jabref.architecture.AllowedToUseLogic, org.jabref.logic.bibtex.FieldWriter, org.jabref.logic.util.strings.StringUtil, org.jabref.model.database.event.EntriesAddedEvent, .EntriesRemovedEvent, org.jabref.model.entry.BibEntry, .BibtexString, .Month, .ParsedEntryLink, org.jabref.model.entry.event.EntriesEventSource, .FieldChangedEvent, org.jabref.model.entry.field.Field, .FieldFactory, .FieldProperty, .StandardField, com.google.common.eventbus.EventBus, .Subscribe, org.jspecify.annotations.NonNull, .Nullable, org.slf4j.Logger, .LoggerFactory

A bibliography database. This is the "bib" file (or the library stored in a shared SQL database)

Field Summary

Modifier and TypeField and Description
private static final int
private final Map<String, Set<BibEntry>>
private final ObservableList<BibEntry>
entries

State attributes

private static final Comparator<BibEntry>
private final Map<String, BibEntry>
private String
private final EventBus
private static final Logger
private String
private @Nullable String
private static final Pattern
private @Nullable String
private Map<String, BibtexString>

Constructor Summary

AccessConstructor and Description
public
BibDatabase(List<BibEntry> entries, String newLineSeparator)

public
public

Method Summary

Modifier and TypeMethod and Description
public synchronized void
addString(BibtexString string)

Inserts a Bibtex String.

public void
public boolean
containsEntryWithId(String id)

Returns whether an entry with the given ID exists (-> entry_type + hashcode).

public void
copyPreamble(BibDatabase
another BibDatabase
database
)

Copies the preamble of another BibDatabase.

public boolean
equals(Object o)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

private void
public String

Returns:

The generated sharedDatabaseID
generateSharedDatabaseID
()

Generates and sets a random ID which is globally unique.

public Set<Field>

Returns:

set of fieldnames, that are visible
getAllVisibleFields
()

Returns a set of Strings, that contains all field names that are visible.

public ObservableList<BibEntry>
public synchronized List<BibEntry>

Returns:

list of entries that contains the given key
getEntriesByCitationKey
(String key)

Collects entries having the specified citation key and returns these entries as list.

public Set<BibEntry>
public synchronized List<BibEntry>
getEntriesSnapshot()

Returns an immutable, stable snapshot of the current entries.

public List<BibEntry>
getEntriesSorted(Comparator<BibEntry> comparator)

Returns the list of entries sorted by the given comparator.

public synchronized Optional<BibEntry>
getEntryByCitationKey(String key)

Returns the entry with the given citation key.

public Optional<BibEntry>
public int
getEntryCount()

Returns the number of entries.

public String
public String
getNewLineSeparator()

Returns the string used to indicate a linebreak

public long
getNumberOfCitationKeyOccurrences(String key)

Returns the number of occurrences of the given citation key in this database.

public synchronized @NonNull Optional<String>
getPreamble()

Returns the database's preamble.

private Set<String>
public Optional<BibEntry>
public Optional<String>
public Optional<BibtexString>
getString(String id)

Returns the string with the given id.

public Optional<BibtexString>
getStringByName(String name)

Returns the string with the given name/label

public int
getStringCount()

Returns the number of strings.

public Set<String>
getStringKeySet()

Returns a Set of keys to all BibtexString objects in the database.

public Collection<BibtexString>
getStringValues()

Returns a Collection of all BibtexString objects in the database.

public List<BibtexString>
getUsedStrings(Collection<BibEntry> entries)

Get all strings used in the entries.

public boolean
hasEntries()

Checks if the database contains entries.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public boolean
hasNoStrings()

Check if there are strings.

public synchronized boolean
hasStringByName(String label)

Returns true if a string with the given label already exists.

private void
public int
indexOf(@NonNull BibEntry bibEntry)

Returns: The index of the given entry in the list of entries, or -1 if the entry is not in the list.

public synchronized void
insertEntries(BibEntry... entries)

public synchronized void
public synchronized void
public synchronized void
public synchronized void
insertEntry(BibEntry
entry to insert
entry
,
EntriesEventSource
source the event is sent from
eventSource
)

Inserts the entry.

private void
insertSmallBatch(List<BibEntry> sortedNewEntries)

public boolean
isDuplicateCitationKeyExisting(String key)

Checks if there is more than one occurrence of the citation key.

public boolean
private boolean
private List<BibEntry>
mergeSortedEntries(List<BibEntry> sortedNewEntries)

public void
public void
registerListener(Object
listener (subscriber) to add
listener
)

Registers a listener object (subscriber) to the internal event bus.

private void
public synchronized void
removeEntries(List<BibEntry>
Entries to delete
toBeDeleted
)

Removes the given entries.

public synchronized void
removeEntries(@NonNull List<BibEntry>
Entry to delete
toBeDeleted
,
EntriesEventSource
Source the event is sent from
eventSource
)

Removes the given entries.

public synchronized void
public synchronized void
removeEntry(BibEntry bibEntry, EntriesEventSource eventSource)

private void
public void
removeString(String id)

Removes the string with the given id.

private String
resolveContent(String result, Set<String> usedIds, Set<String> allUsedIds)

public String
resolveForStrings(@NonNull String content)

Resolves any references to strings contained in this field content, if possible.

public List<BibEntry>

Returns:

a list of bibtexentries, with all strings resolved. It is dependent on the value of inPlace whether copies are made or the given BibtexEntries are modified.
resolveForStrings
(@NonNull Collection<BibEntry>
A collection of BibtexEntries in which all strings of the form #xxx# will be resolved against the hash map of string references stored in the database.
entriesToResolve
,
boolean
If inPlace is true then the given BibtexEntries will be modified, if false then copies of the BibtexEntries are made before resolving the strings.
inPlace
)

Take the given collection of BibEntry and resolve any string references.

public BibEntry

Returns:

a BibEntry with all string references resolved. It is dependent on the value of inPlace whether a copy is made or the given BibtexEntries is modified.
resolveForStrings
(BibEntry
A BibEntry in which all strings of the form #xxx# will be resolved against the hash map of string references stored in the database.
entry
,
boolean
If inPlace is true then the given BibEntry will be modified, if false then a copy is made using close made before resolving the strings.
inPlace
)

Take the given BibEntry and resolve any string references.

private @NonNull Optional<String>
resolveString(@NonNull String label, @NonNull Set<String> usedIds, @NonNull Set<String> allUsedIds)

If the label represents a string contained in this database, returns that string's content.

public void
setEpilog(String epilog)

public void
setNewLineSeparator(String newLineSeparator)

Set the newline separator.

public synchronized void
setPreamble(@Nullable String preamble)

Sets the database's preamble.

public void
setSharedDatabaseID(String sharedDatabaseID)

public void
setStrings(List<BibtexString>
The collection of strings to set
stringsToAdd
)

Replaces the existing lists of BibTexString with the given one Duplicates throw KeyCollisionException

private List<BibEntry>
public void
unregisterListener(Object
listener (subscriber) to remove
listener
)

Unregisters an listener object.

private void
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

BATCH_REPLACEMENT_THRESHOLDback to summary
private static final int BATCH_REPLACEMENT_THRESHOLD
citationIndexback to summary
private final Map<String, Set<BibEntry>> citationIndex
entriesback to summary
private final ObservableList<BibEntry> entries

State attributes

ENTRY_ID_COMPARATORback to summary
private static final Comparator<BibEntry> ENTRY_ID_COMPARATOR
entryIdToBibEntryback to summary
private final Map<String, BibEntry> entryIdToBibEntry
epilogback to summary
private String epilog
eventBusback to summary
private final EventBus eventBus
LOGGERback to summary
private static final Logger LOGGER
newLineSeparatorback to summary
private String newLineSeparator
preambleback to summary
private @Nullable String preamble
RESOLVE_CONTENT_PATTERNback to summary
private static final Pattern RESOLVE_CONTENT_PATTERN
sharedDatabaseIDback to summary
private @Nullable String sharedDatabaseID
stringToBibtexStringback to summary
private Map<String, BibtexString> stringToBibtexString

Constructor Detail

BibDatabaseback to summary
public BibDatabase(List<BibEntry> entries, String newLineSeparator)
BibDatabaseback to summary
public BibDatabase(List<BibEntry> entries)
BibDatabaseback to summary
public BibDatabase()

Method Detail

addStringback to summary
public synchronized void addString(BibtexString string) throws KeyCollisionException

Inserts a Bibtex String.

clearSharedDatabaseIDback to summary
public void clearSharedDatabaseID()
containsEntryWithIdback to summary
public boolean containsEntryWithId(String id)

Returns whether an entry with the given ID exists (-> entry_type + hashcode).

copyPreambleback to summary
public void copyPreamble(BibDatabase database)

Copies the preamble of another BibDatabase.

Parameters
database:BibDatabase

another BibDatabase

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
forEachCitationKeyback to summary
private void forEachCitationKey(BibEntry entry, Consumer<String> keyConsumer)
generateSharedDatabaseIDback to summary
public String generateSharedDatabaseID()

Generates and sets a random ID which is globally unique.

Returns:String

The generated sharedDatabaseID

getAllVisibleFieldsback to summary
public Set<Field> getAllVisibleFields()

Returns a set of Strings, that contains all field names that are visible. This means that the fields are not internal fields. Internal fields are fields, that are starting with "_".

Returns:Set<Field>

set of fieldnames, that are visible

getEntriesback to summary
public ObservableList<BibEntry> getEntries()
getEntriesByCitationKeyback to summary
public synchronized List<BibEntry> getEntriesByCitationKey(String key)

Collects entries having the specified citation key and returns these entries as list. The order of the entries is the order they appear in the database.

Returns:List<BibEntry>

list of entries that contains the given key

getEntriesForCitationKeyback to summary
public Set<BibEntry> getEntriesForCitationKey(@Nullable String citationKey)
getEntriesSnapshotback to summary
public synchronized List<BibEntry> getEntriesSnapshot()

Returns an immutable, stable snapshot of the current entries.

getEntriesSortedback to summary
public List<BibEntry> getEntriesSorted(Comparator<BibEntry> comparator)

Returns the list of entries sorted by the given comparator.

getEntryByCitationKeyback to summary
public synchronized Optional<BibEntry> getEntryByCitationKey(String key)

Returns the entry with the given citation key.

getEntryByIdback to summary
public Optional<BibEntry> getEntryById(String id)
getEntryCountback to summary
public int getEntryCount()

Returns the number of entries.

getEpilogback to summary
public String getEpilog()
getNewLineSeparatorback to summary
public String getNewLineSeparator()

Returns the string used to indicate a linebreak

getNumberOfCitationKeyOccurrencesback to summary
public long getNumberOfCitationKeyOccurrences(String key)

Returns the number of occurrences of the given citation key in this database.

getPreambleback to summary
public synchronized @NonNull Optional<String> getPreamble()

Returns the database's preamble. If the preamble text consists only of whitespace, then also an empty optional is returned.

getReferencedCitationKeysback to summary
private Set<String> getReferencedCitationKeys(BibEntry entry)
getReferencedEntryback to summary
public Optional<BibEntry> getReferencedEntry(BibEntry entry)
getSharedDatabaseIDback to summary
public Optional<String> getSharedDatabaseID()
getStringback to summary
public Optional<BibtexString> getString(String id)

Returns the string with the given id.

getStringByNameback to summary
public Optional<BibtexString> getStringByName(String name)

Returns the string with the given name/label

getStringCountback to summary
public int getStringCount()

Returns the number of strings.

getStringKeySetback to summary
public Set<String> getStringKeySet()

Returns a Set of keys to all BibtexString objects in the database. These are in no sorted order.

getStringValuesback to summary
public Collection<BibtexString> getStringValues()

Returns a Collection of all BibtexString objects in the database. These are in no particular order.

getUsedStringsback to summary
public List<BibtexString> getUsedStrings(Collection<BibEntry> entries)

Get all strings used in the entries.

hasEntriesback to summary
public boolean hasEntries()

Checks if the database contains entries.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override
hasNoStringsback to summary
public boolean hasNoStrings()

Check if there are strings.

hasStringByNameback to summary
public synchronized boolean hasStringByName(String label)

Returns true if a string with the given label already exists.

indexEntryback to summary
private void indexEntry(BibEntry entry)
indexOfback to summary
public int indexOf(@NonNull BibEntry bibEntry)

Implementation Note

insertEntries keeps the list sorted by the ids handed out by IdGenerator, so binary search finds the index.

Implementation Note

IDs are zero-padded strings, so there is no need to convert them to integers for comparison.

Returns:int

The index of the given entry in the list of entries, or -1 if the entry is not in the list.

insertEntriesback to summary
public synchronized void insertEntries(BibEntry... entries)
insertEntriesback to summary
public synchronized void insertEntries(List<BibEntry> entries)
insertEntriesback to summary
public synchronized void insertEntries(@NonNull List<BibEntry> newEntries, EntriesEventSource eventSource)
insertEntryback to summary
public synchronized void insertEntry(BibEntry entry)
insertEntryback to summary
public synchronized void insertEntry(BibEntry entry, EntriesEventSource eventSource)

Inserts the entry.

Parameters
entry:BibEntry

entry to insert

eventSource:EntriesEventSource

source the event is sent from

insertSmallBatchback to summary
private void insertSmallBatch(List<BibEntry> sortedNewEntries)
isDuplicateCitationKeyExistingback to summary
public boolean isDuplicateCitationKeyExisting(String key)

Checks if there is more than one occurrence of the citation key.

isSharedback to summary
public boolean isShared()
isSortedByIdback to summary
private boolean isSortedById(List<BibEntry> entries)
mergeSortedEntriesback to summary
private List<BibEntry> mergeSortedEntries(List<BibEntry> sortedNewEntries)
postEventback to summary
public void postEvent(Object event)
registerListenerback to summary
public void registerListener(Object listener)

Registers a listener object (subscriber) to the internal event bus. The following events are posted:

Parameters
listener:Object

listener (subscriber) to add

relayEntryChangeEventback to summary
private void relayEntryChangeEvent(FieldChangedEvent event)
Annotations
@Subscribe
removeEntriesback to summary
public synchronized void removeEntries(List<BibEntry> toBeDeleted)

Removes the given entries. The entries removed based on the id BibEntry#getId()

Parameters
toBeDeleted:List<BibEntry>

Entries to delete

removeEntriesback to summary
public synchronized void removeEntries(@NonNull List<BibEntry> toBeDeleted, EntriesEventSource eventSource)

Removes the given entries. The entries are removed based on the id BibEntry#getId()

Parameters
toBeDeleted:@NonNull List<BibEntry>

Entry to delete

eventSource:EntriesEventSource

Source the event is sent from

removeEntryback to summary
public synchronized void removeEntry(BibEntry bibEntry)
removeEntryback to summary
public synchronized void removeEntry(BibEntry bibEntry, EntriesEventSource eventSource)
removeEntryFromIndexback to summary
private void removeEntryFromIndex(BibEntry entry)
removeStringback to summary
public void removeString(String id)

Removes the string with the given id.

resolveContentback to summary
private String resolveContent(String result, Set<String> usedIds, Set<String> allUsedIds)
resolveForStringsback to summary
public String resolveForStrings(@NonNull String content)

Resolves any references to strings contained in this field content, if possible.

resolveForStringsback to summary
public List<BibEntry> resolveForStrings(@NonNull Collection<BibEntry> entriesToResolve, boolean inPlace)

Take the given collection of BibEntry and resolve any string references.

Parameters
entriesToResolve:@NonNull Collection<BibEntry>

A collection of BibtexEntries in which all strings of the form #xxx# will be resolved against the hash map of string references stored in the database.

inPlace:boolean

If inPlace is true then the given BibtexEntries will be modified, if false then copies of the BibtexEntries are made before resolving the strings.

Returns:List<BibEntry>

a list of bibtexentries, with all strings resolved. It is dependent on the value of inPlace whether copies are made or the given BibtexEntries are modified.

resolveForStringsback to summary
public BibEntry resolveForStrings(BibEntry entry, boolean inPlace)

Take the given BibEntry and resolve any string references.

Parameters
entry:BibEntry

A BibEntry in which all strings of the form #xxx# will be resolved against the hash map of string references stored in the database.

inPlace:boolean

If inPlace is true then the given BibEntry will be modified, if false then a copy is made using close made before resolving the strings.

Returns:BibEntry

a BibEntry with all string references resolved. It is dependent on the value of inPlace whether a copy is made or the given BibtexEntries is modified.

resolveStringback to summary
private @NonNull Optional<String> resolveString(@NonNull String label, @NonNull Set<String> usedIds, @NonNull Set<String> allUsedIds)

If the label represents a string contained in this database, returns that string's content. Resolves references to other strings, taking care not to follow a circular reference pattern. If the string is undefined, returns null.

setEpilogback to summary
public void setEpilog(String epilog)
setNewLineSeparatorback to summary
public void setNewLineSeparator(String newLineSeparator)

Set the newline separator.

setPreambleback to summary
public synchronized void setPreamble(@Nullable String preamble)

Sets the database's preamble.

setSharedDatabaseIDback to summary
public void setSharedDatabaseID(String sharedDatabaseID)
setStringsback to summary
public void setStrings(List<BibtexString> stringsToAdd)

Replaces the existing lists of BibTexString with the given one Duplicates throw KeyCollisionException

Parameters
stringsToAdd:List<BibtexString>

The collection of strings to set

sortEntriesByIdback to summary
private List<BibEntry> sortEntriesById(List<BibEntry> newEntries)
unregisterListenerback to summary
public void unregisterListener(Object listener)

Unregisters an listener object.

Parameters
listener:Object

listener (subscriber) to remove

updateCitationIndexForFieldChangeback to summary
private void updateCitationIndexForFieldChange(FieldChangedEvent event)