1313import java .util .TreeSet ;
1414import java .util .concurrent .ConcurrentHashMap ;
1515import java .util .regex .Pattern ;
16+ import java .util .stream .Collectors ;
1617
1718import net .sf .jabref .event .source .EntryEventSource ;
1819import net .sf .jabref .model .EntryTypes ;
2122import net .sf .jabref .model .entry .EntryType ;
2223import net .sf .jabref .model .entry .EntryUtil ;
2324import net .sf .jabref .model .entry .FieldName ;
24- import net .sf .jabref .model .entry .InternalBibtexFields ;
2525import net .sf .jabref .model .entry .MonthUtil ;
2626import net .sf .jabref .model .event .EntryAddedEvent ;
2727import net .sf .jabref .model .event .EntryChangedEvent ;
@@ -59,7 +59,6 @@ public class BibDatabase {
5959 */
6060 private final Set <String > internalIDs = new HashSet <>();
6161
62-
6362 private final EventBus eventBus = new EventBus ();
6463
6564
@@ -100,22 +99,22 @@ public List<BibEntry> getEntries() {
10099 return Collections .unmodifiableList (entries );
101100 }
102101
102+ /**
103+ * Returns a set of Strings, that contains all field names that are visible. This means that the fields
104+ * are not internal fields. Internal fields are fields, that are starting with "_".
105+ *
106+ * @return set of fieldnames, that are visible
107+ */
103108 public Set <String > getAllVisibleFields () {
104109 Set <String > allFields = new TreeSet <>();
105110 for (BibEntry e : getEntries ()) {
106111 allFields .addAll (e .getFieldNames ());
107112 }
108- Set <String > toberemoved = new TreeSet <>();
109- for (String field : allFields ) {
110- if (InternalBibtexFields .isInternalField (field )) {
111- toberemoved .add (field );
112- }
113- }
113+ return allFields .stream ().filter (field -> !isInternalField (field )).collect (Collectors .toSet ());
114+ }
114115
115- for (String field : toberemoved ) {
116- allFields .remove (field );
117- }
118- return allFields ;
116+ public static boolean isInternalField (String field ) {
117+ return field .startsWith ("__" );
119118 }
120119
121120 /**
@@ -130,6 +129,13 @@ public synchronized Optional<BibEntry> getEntryByKey(String key) {
130129 return Optional .empty ();
131130 }
132131
132+ /**
133+ * Collects entries having the specified BibTeX key and returns these entries as list.
134+ * The order of the entries is the order they appear in the database.
135+ *
136+ * @param key
137+ * @return list of entries that contains the given key
138+ */
133139 public synchronized List <BibEntry > getEntriesByKey (String key ) {
134140 List <BibEntry > result = new ArrayList <>();
135141
@@ -163,8 +169,7 @@ public synchronized boolean insertEntry(BibEntry entry) throws KeyCollisionExcep
163169 * @param eventSource Source the event is sent from
164170 * @return false if the insert was done without a duplicate warning
165171 */
166- public synchronized boolean insertEntry (BibEntry entry , EntryEventSource eventSource )
167- throws KeyCollisionException {
172+ public synchronized boolean insertEntry (BibEntry entry , EntryEventSource eventSource ) throws KeyCollisionException {
168173 Objects .requireNonNull (entry );
169174
170175 String id = entry .getId ();
@@ -177,7 +182,7 @@ public synchronized boolean insertEntry(BibEntry entry, EntryEventSource eventSo
177182 entry .registerListener (this );
178183
179184 eventBus .post (new EntryAddedEvent (entry , eventSource ));
180- return duplicationChecker .checkForDuplicateKeyAndAdd (null , entry .getCiteKeyOptional (). orElse ( null ));
185+ return duplicationChecker .checkForDuplicateKeyAndAdd (null , entry .getCiteKey ( ));
181186 }
182187
183188 /**
@@ -192,13 +197,14 @@ public synchronized void removeEntry(BibEntry toBeDeleted) {
192197 /**
193198 * Removes the given entry.
194199 * The Entry is removed based on the id {@link BibEntry#id}
200+ *
195201 * @param toBeDeleted Entry to delete
196202 * @param eventSource Source the event is sent from
197203 */
198204 public synchronized void removeEntry (BibEntry toBeDeleted , EntryEventSource eventSource ) {
199205 Objects .requireNonNull (toBeDeleted );
200206
201- boolean anyRemoved = entries .removeIf (entry -> entry .getId ().equals (toBeDeleted .getId ()));
207+ boolean anyRemoved = entries .removeIf (entry -> entry .getId ().equals (toBeDeleted .getId ()));
202208 if (anyRemoved ) {
203209 internalIDs .remove (toBeDeleted .getId ());
204210 toBeDeleted .getCiteKeyOptional ().ifPresent (duplicationChecker ::removeKeyFromSet );
@@ -210,8 +216,14 @@ public int getNumberOfKeyOccurrences(String key) {
210216 return duplicationChecker .getNumberOfKeyOccurrences (key );
211217 }
212218
219+ /**
220+ * Sets the given key to the given entry.
221+ * If the key is null, the entry field will be cleared.
222+ *
223+ * @return true, if the entry contains the key, false if not
224+ */
213225 public synchronized boolean setCiteKeyForEntry (BibEntry entry , String key ) {
214- String oldKey = entry .getCiteKeyOptional (). orElse ( null );
226+ String oldKey = entry .getCiteKey ( );
215227 if (key == null ) {
216228 entry .clearField (BibEntry .KEY_FIELD );
217229 } else {
@@ -237,8 +249,7 @@ public synchronized Optional<String> getPreamble() {
237249 /**
238250 * Inserts a Bibtex String.
239251 */
240- public synchronized void addString (BibtexString string )
241- throws KeyCollisionException {
252+ public synchronized void addString (BibtexString string ) throws KeyCollisionException {
242253 if (hasStringLabel (string .getName ())) {
243254 throw new KeyCollisionException ("A string with that label already exists" );
244255 }
@@ -478,8 +489,6 @@ private String resolveContent(String result, Set<String> usedIds) {
478489 return res ;
479490 }
480491
481-
482-
483492 /**
484493 * Returns the text stored in the given field of the given bibtex entry
485494 * which belongs to the given database.
@@ -511,9 +520,9 @@ public static Optional<String> getResolvedField(String field, BibEntry entry, Bi
511520 return entry .getCiteKeyOptional ();
512521 }
513522
514- // TODO: Changed this to also consider alias fields, which is the expected
523+ // Changed this to also consider alias fields, which is the expected
515524 // behavior for the preview layout and for the check whatever all fields are present.
516- // But there might be unwanted side-effects?!
525+ // TODO: But there might be unwanted side-effects?!
517526 Optional <String > result = entry .getFieldOrAlias (field );
518527
519528 // If this field is not set, and the entry has a crossref, try to look up the
0 commit comments